"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "lib/ruport/data/grouping.rb" between
ruport-1.7.1.tar.gz and ruport-1.8.0.tar.gz

About: Ruport (Ruby Reports) is a simple, extensible reporting system built for Rubyists. It provides tools for data acquisition, database interaction, formatting, and parsing/munging.

grouping.rb  (ruport-1.7.1):grouping.rb  (ruport-1.8.0)
skipping to change at line 102 skipping to change at line 102
# #
# main_group = Group.new :name => 'test', # main_group = Group.new :name => 'test',
# :data => [[1,2,3,4,5], [3,4,5,6,7]], # :data => [[1,2,3,4,5], [3,4,5,6,7]],
# :column_names => %w[a b c d e] # :column_names => %w[a b c d e]
# main_group.create_subgroups("a") # main_group.create_subgroups("a")
# #
def create_subgroups(group_column) def create_subgroups(group_column)
if @subgroups.empty? if @subgroups.empty?
@subgroups = grouped_data(group_column) @subgroups = grouped_data(group_column)
else else
@subgroups.each {|name,group| @subgroups.each {|_name,group|
group.send(:create_subgroups, group_column) group.send(:create_subgroups, group_column)
} }
end end
end end
def grouped_data(group_column) #:nodoc: def grouped_data(group_column) #:nodoc:
data = {} data = {}
group_names = column(group_column).uniq group_names = column(group_column).uniq
columns = column_names.dup columns = column_names.dup
columns.delete(group_column) columns.delete(group_column)
skipping to change at line 174 skipping to change at line 174
if data.kind_of?(Hash) if data.kind_of?(Hash)
@grouped_by = data[:by] @grouped_by = data[:by]
@order = data[:order] @order = data[:order]
@data = {} @data = {}
else else
@grouped_by = options[:by] @grouped_by = options[:by]
@order = options[:order] @order = options[:order]
cols = Array(options[:by]).dup cols = Array(options[:by]).dup
@data = data.to_group.send(:grouped_data, cols.shift) @data = data.to_group.send(:grouped_data, cols.shift)
cols.each do |col| cols.each do |col|
@data.each do |name,group| @data.each do |_name,group|
group.send(:create_subgroups, col) group.send(:create_subgroups, col)
end end
end end
end end
end end
# The grouping's data # The grouping's data
attr_accessor :data attr_accessor :data
# The name of the column used to group the data # The name of the column used to group the data
skipping to change at line 202 skipping to change at line 202
# #
def [](name) def [](name)
@data[name] or @data[name] or
raise(IndexError,"Group Not Found") raise(IndexError,"Group Not Found")
end end
# Iterates through the Grouping, yielding each group name and Group object # Iterates through the Grouping, yielding each group name and Group object
# #
def each def each
if @order.respond_to?(:call) if @order.respond_to?(:call)
@data.sort_by { |n,g| @order[g] }.each { |n,g| yield(n,g) } @data.sort_by { |_n,g| @order[g] }.each { |n,g| yield(n,g) }
elsif @order == :name elsif @order == :name
@data.sort_by { |n,g| n }.each { |name,group| yield(name,group) } @data.sort_by { |n,_g| n }.each { |name,group| yield(name,group) }
else else
@data.each { |name,group| yield(name,group) } @data.each { |name,group| yield(name,group) }
end end
end end
# Returns a new grouping with the specified sort order. # Returns a new grouping with the specified sort order.
# You can sort by Group name or an arbitrary block # You can sort by Group name or an arbitrary block
# #
# by_name = grouping.sort_grouping_by(:name) # by_name = grouping.sort_grouping_by(:name)
# by_size = grouping.sort_grouping_by { |g| g.size } # by_size = grouping.sort_grouping_by { |g| g.size }
def sort_grouping_by(type=nil,&block) def sort_grouping_by(type=nil,&block)
a = Grouping.new(:by => @grouped_by, :order => type || block) a = Grouping.new(:by => @grouped_by, :order => type || block)
each { |n,g| a << g } each { |_n,g| a << g }
return a return a
end end
# Applies the specified sort order to an existing Grouping object. # Applies the specified sort order to an existing Grouping object.
# #
# grouping.sort_grouping_by!(:name) # grouping.sort_grouping_by!(:name)
# grouping.sort_grouping_by! { |g| g.size } # grouping.sort_grouping_by! { |g| g.size }
def sort_grouping_by!(type=nil,&block) def sort_grouping_by!(type=nil,&block)
@order = type || block @order = type || block
end end
skipping to change at line 286 skipping to change at line 286
# :opened => lambda { |g| g.sigma(:opened) }, # :opened => lambda { |g| g.sigma(:opened) },
# :closed => lambda { |g| g.sigma(:closed) }, # :closed => lambda { |g| g.sigma(:closed) },
# :order => [:date,:opened,:closed] # :order => [:date,:opened,:closed]
# #
def summary(field,procs) def summary(field,procs)
if procs[:order].kind_of?(Array) if procs[:order].kind_of?(Array)
cols = procs.delete(:order) cols = procs.delete(:order)
else else
cols = procs.keys + [field] cols = procs.keys + [field]
end end
expected = Table.new(:column_names => cols) { |t| Table.new(:column_names => cols) { |t|
each do |name,group| each do |name,group|
t << procs.inject({field => name}) do |s,r| t << procs.inject({field => name}) do |s,r|
s.merge(r[0] => r[1].call(group)) s.merge(r[0] => r[1].call(group))
end end
end end
t.data.reorder(cols) t.data.reorder(cols)
} }
end end
# Uses Ruport's built-in text formatter to render this Grouping # Uses Ruport's built-in text formatter to render this Grouping
skipping to change at line 328 skipping to change at line 328
# Example: # Example:
# #
# table = [[1,2,3],[3,4,5],[5,6,7]].to_table(%w[col1 col2 col3]) # table = [[1,2,3],[3,4,5],[5,6,7]].to_table(%w[col1 col2 col3])
# grouping = Grouping(table, :by => "col1") # grouping = Grouping(table, :by => "col1")
# grouping.sigma("col2") #=> 12 # grouping.sigma("col2") #=> 12
# grouping.sigma(0) #=> 12 # grouping.sigma(0) #=> 12
# grouping.sigma { |r| r.col2 + r.col3 } #=> 27 # grouping.sigma { |r| r.col2 + r.col3 } #=> 27
# grouping.sigma { |r| r.col2 + 1 } #=> 15 # grouping.sigma { |r| r.col2 + 1 } #=> 15
# #
def sigma(column=nil) def sigma(column=nil)
inject(0) do |s, (group_name, group)| inject(0) do |s, (_group_name, group)|
if column if column
s + group.sigma(column) s + group.sigma(column)
else else
s + group.sigma do |r| s + group.sigma do |r|
yield(r) yield(r)
end end
end end
end end
end end
 End of changes. 7 change blocks. 
7 lines changed or deleted 7 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)