Class: Ruport::Data::Table::Pivot
Overview
:nodoc:
Instance Method Summary collapse
- #columns_from_pivot ⇒ Object
- #convert_row_order_to_group_order(row_order_spec) ⇒ Object
- #group_column_entries ⇒ Object
-
#initialize(table, group_col, pivot_col, summary_col, options = {}) ⇒ Pivot
constructor
A new instance of Pivot.
- #to_table ⇒ Object
Constructor Details
#initialize(table, group_col, pivot_col, summary_col, options = {}) ⇒ Pivot
Returns a new instance of Pivot.
29 30 31 32 33 34 35 |
# File 'lib/ruport/data/table.rb', line 29 def initialize(table, group_col, pivot_col, summary_col, = {}) @table = table @group_column = group_col @pivot_column = pivot_col @summary_column = summary_col @pivot_order = [:pivot_order] end |
Instance Method Details
#columns_from_pivot ⇒ Object
58 59 60 61 62 63 64 65 66 |
# File 'lib/ruport/data/table.rb', line 58 def columns_from_pivot ordering = convert_row_order_to_group_order(@pivot_order) pivot_column_grouping = Grouping(@table, :by => @pivot_column) pivot_column_grouping.each {|n,g| g.add_column(n) { n }} pivot_column_grouping.sort_grouping_by!(ordering) if ordering result = [] pivot_column_grouping.each {|name,_| result << name } result end |
#convert_row_order_to_group_order(row_order_spec) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/ruport/data/table.rb', line 37 def convert_row_order_to_group_order(row_order_spec) case row_order_spec when Array proc {|group| row_order_spec.map {|e| group[0][e].to_s } } when Proc proc {|group| if row_order_spec.arity == 2 row_order_spec.call(group[0], group.name) else row_order_spec.call(group[0]) end } when NilClass nil else proc {|group| group[0][row_order_spec].to_s } end end |
#group_column_entries ⇒ Object
68 69 70 |
# File 'lib/ruport/data/table.rb', line 68 def group_column_entries @table.map {|row| row[@group_column]}.uniq end |
#to_table ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/ruport/data/table.rb', line 72 def to_table result = Table() result.add_column(@group_column) pivoted_columns = columns_from_pivot pivoted_columns.each { |name| result.add_column(name) } outer_grouping = Grouping(@table, :by => @group_column) group_column_entries.each {|outer_group_name| outer_group = outer_grouping[outer_group_name] pivot_values = pivoted_columns.inject({}) do |hsh, e| matching_rows = outer_group.rows_with(@pivot_column => e) hsh[e] = matching_rows.first && matching_rows.first[@summary_column] hsh end result << [outer_group_name] + pivoted_columns.map {|e| pivot_values[e] } } result end |