"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "lib/ruport/data/table.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.

table.rb  (ruport-1.7.1):table.rb  (ruport-1.8.0)
skipping to change at line 53 skipping to change at line 53
# Row is the first row in the pivoted table (without the group column) # Row is the first row in the pivoted table (without the group column)
def row def row
return @row if defined?(@row) return @row if defined?(@row)
pivot_column_grouping = Grouping(@table, :by => @pivot_column) pivot_column_grouping = Grouping(@table, :by => @pivot_column)
ordering = self.class.row_order_to_group_order(@pivot_order) ordering = self.class.row_order_to_group_order(@pivot_order)
pivot_column_grouping.sort_grouping_by!(ordering) if ordering pivot_column_grouping.sort_grouping_by!(ordering) if ordering
@row = pivot_column_grouping.map { |name,grouping| name } @row = pivot_column_grouping.map { |name,_grouping| name }
end end
# Column in the first column in the pivoted table (without the group colum n) # Column in the first column in the pivoted table (without the group colum n)
def column def column
@column ||= @table.map { |row| row[@group_column] }.uniq @column ||= @table.map { |row| row[@group_column] }.uniq
end end
def to_table def to_table
table = Table.new table = Table.new
create_header(table) create_header(table)
column.each do |column_entry| column.each do |column_entry|
row_values = row.map { |row_entry| values[column_entry][row_entry] } row_values = row.map { |row_entry| values[column_entry][row_entry] }
table << [column_entry] + row_values table << [column_entry] + row_values
end end
table table
end end
def values def values
@values ||= Hash.new do |values, column_entry| @values ||= Hash.new do |row_values, column_entry|
rows_group = rows_groups[column_entry] rows_group = rows_groups[column_entry]
values[column_entry] = row_values[column_entry] =
row.inject({}) do |values, row_entry| row.inject({}) do |values, row_entry|
matching_rows = rows_group.rows_with(@pivot_column => row_entry) matching_rows = rows_group.rows_with(@pivot_column => row_entry)
values[row_entry] = perform_operation(matching_rows) values[row_entry] = perform_operation(matching_rows)
values values
end end
end end
end end
private private
skipping to change at line 135 skipping to change at line 135
extend self extend self
def first(rows, summary_column) def first(rows, summary_column)
rows.first && rows.first[summary_column] rows.first && rows.first[summary_column]
end end
def sum(rows, summary_column) def sum(rows, summary_column)
rows && rows.inject(0) { |sum,row| sum+row[summary_column] } rows && rows.inject(0) { |sum,row| sum+row[summary_column] }
end end
def count(rows, summary_column) def count(rows, _summary_column)
rows && rows.length rows && rows.length
end end
def mean(rows, summary_column) def mean(rows, summary_column)
return if rows.length == 0 return if rows.length == 0
sum = rows && rows.inject(0) { |sum,row| sum+row[summary_column] } sum = rows && rows.inject(0) { |sum,row| sum+row[summary_column] }
sum / rows.length sum / rows.length
end end
def min(rows, summary_column) def min(rows, summary_column)
skipping to change at line 262 skipping to change at line 262
def get_table_from_csv(msg,param,options={},&block) #:nodoc: def get_table_from_csv(msg,param,options={},&block) #:nodoc:
options = {:has_names => true, options = {:has_names => true,
:csv_options => {} }.merge(options) :csv_options => {} }.merge(options)
adjust_options_for_fcsv_headers(options) adjust_options_for_fcsv_headers(options)
table = self.new(options) do |feeder| table = self.new(options) do |feeder|
first_line = true first_line = true
::CSV.send(msg,param,options[:csv_options]) do |row| ::CSV.send(msg,param, **options[:csv_options]) do |row|
if first_line if first_line
adjust_for_headers(feeder.data,row,options) adjust_for_headers(feeder.data,row,options)
first_line = false first_line = false
next if options[:has_names] next if options[:has_names]
end end
if block if block
handle_csv_row_proc(feeder,row,options,block) handle_csv_row_proc(feeder,row,options,block)
else else
feeder << row feeder << row
skipping to change at line 563 skipping to change at line 563
# Add multiple extra columns to the Table. See <tt>add_column</tt> for # Add multiple extra columns to the Table. See <tt>add_column</tt> for
# a list of available options. # a list of available options.
# #
# Example: # Example:
# #
# data = Table.new("a","b") { |t| t << [1,2] << [3,4] } # data = Table.new("a","b") { |t| t << [1,2] << [3,4] }
# #
# data.add_columns ['new_column_1','new_column_2'], :default => 1 # data.add_columns ['new_column_1','new_column_2'], :default => 1
# #
def add_columns(names,options={}) def add_columns(names,options={})
raise "Greg isn't smart enough to figure this out.\n"+ raise "Greg isn't smart enough to figure this out.\n" \
"Send ideas in at http://list.rubyreports.org" if block_given? "Send ideas in at github" if block_given?
need_reverse = !!(options[:after] || options[:position]) need_reverse = !!(options[:after] || options[:position])
names = names.reverse if need_reverse names = names.reverse if need_reverse
names.each { |n| add_column(n,options) } names.each { |n| add_column(n,options) }
self self
end end
# Removes the given column from the table. May use name or position. # Removes the given column from the table. May use name or position.
# #
# Example: # Example:
# #
# table.remove_column(0) #=> removes the first column # table.remove_column(0) #=> removes the first column
# table.remove_column("apple") #=> removes column named apple # table.remove_column("apple") #=> removes column named apple
# #
def remove_column(col) def remove_column(col)
col = column_names[col] if col.kind_of? Fixnum col = column_names[col] if col.kind_of? Integer
column_names.delete(col) column_names.delete(col)
each { |r| r.send(:delete,col) } each { |r| r.send(:delete,col) }
end end
# Removes multiple columns from the table. May use name or position # Removes multiple columns from the table. May use name or position
# Will autosplat arrays. # Will autosplat arrays.
# #
# Example: # Example:
# table.remove_columns('a','b','c') # table.remove_columns('a','b','c')
# table.remove_columns([0,1]) # table.remove_columns([0,1])
skipping to change at line 669 skipping to change at line 669
# >> a.swap_column("a","c") # >> a.swap_column("a","c")
# >> puts a # >> puts a
# +-----------+ # +-----------+
# | c | b | a | # | c | b | a |
# +-----------+ # +-----------+
# | 3 | 2 | 1 | # | 3 | 2 | 1 |
# | 6 | 5 | 4 | # | 6 | 5 | 4 |
# +-----------+ # +-----------+
# #
def swap_column(a,b) def swap_column(a,b)
if [a,b].all? { |r| r.kind_of? Fixnum } if [a,b].all? { |r| r.kind_of? Integer }
col_a,col_b = column_names[a],column_names[b] col_a,col_b = column_names[a],column_names[b]
column_names[a] = col_b column_names[a] = col_b
column_names[b] = col_a column_names[b] = col_a
else else
a_ind, b_ind = [column_names.index(a), column_names.index(b)] a_ind, b_ind = [column_names.index(a), column_names.index(b)]
column_names[b_ind] = a column_names[b_ind] = a
column_names[a_ind] = b column_names[a_ind] = b
end end
end end
 End of changes. 8 change blocks. 
9 lines changed or deleted 9 lines changed or added

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