Class: PivotTable::Grid
- Inherits:
-
Object
- Object
- PivotTable::Grid
- Defined in:
- lib/pivot_table/grid.rb
Constant Summary collapse
- DEFAULT_OPTIONS =
{ :sort => true }
Instance Attribute Summary collapse
-
#column_name ⇒ Object
Returns the value of attribute column_name.
-
#columns ⇒ Object
readonly
Returns the value of attribute columns.
-
#configuration ⇒ Object
readonly
Returns the value of attribute configuration.
-
#data_grid ⇒ Object
readonly
Returns the value of attribute data_grid.
-
#field_name ⇒ Object
Returns the value of attribute field_name.
-
#row_name ⇒ Object
Returns the value of attribute row_name.
-
#rows ⇒ Object
readonly
Returns the value of attribute rows.
-
#source_data ⇒ Object
Returns the value of attribute source_data.
-
#value_name ⇒ Object
Returns the value of attribute value_name.
Instance Method Summary collapse
- #build ⇒ Object
- #build_columns ⇒ Object
- #build_rows ⇒ Object
- #column_headers ⇒ Object
- #column_totals ⇒ Object
- #grand_total ⇒ Object
-
#initialize(opts = {}) {|_self| ... } ⇒ Grid
constructor
A new instance of Grid.
- #populate_grid ⇒ Object
- #prepare_grid ⇒ Object
- #row_headers ⇒ Object
- #row_totals ⇒ Object
Constructor Details
#initialize(opts = {}) {|_self| ... } ⇒ Grid
Returns a new instance of Grid.
11 12 13 14 |
# File 'lib/pivot_table/grid.rb', line 11 def initialize(opts = {}, &block) yield(self) if block_given? @configuration = Configuration.new(DEFAULT_OPTIONS.merge(opts)) end |
Instance Attribute Details
#column_name ⇒ Object
Returns the value of attribute column_name.
4 5 6 |
# File 'lib/pivot_table/grid.rb', line 4 def column_name @column_name end |
#columns ⇒ Object (readonly)
Returns the value of attribute columns.
5 6 7 |
# File 'lib/pivot_table/grid.rb', line 5 def columns @columns end |
#configuration ⇒ Object (readonly)
Returns the value of attribute configuration.
5 6 7 |
# File 'lib/pivot_table/grid.rb', line 5 def configuration @configuration end |
#data_grid ⇒ Object (readonly)
Returns the value of attribute data_grid.
5 6 7 |
# File 'lib/pivot_table/grid.rb', line 5 def data_grid @data_grid end |
#field_name ⇒ Object
Returns the value of attribute field_name.
4 5 6 |
# File 'lib/pivot_table/grid.rb', line 4 def field_name @field_name end |
#row_name ⇒ Object
Returns the value of attribute row_name.
4 5 6 |
# File 'lib/pivot_table/grid.rb', line 4 def row_name @row_name end |
#rows ⇒ Object (readonly)
Returns the value of attribute rows.
5 6 7 |
# File 'lib/pivot_table/grid.rb', line 5 def rows @rows end |
#source_data ⇒ Object
Returns the value of attribute source_data.
4 5 6 |
# File 'lib/pivot_table/grid.rb', line 4 def source_data @source_data end |
#value_name ⇒ Object
Returns the value of attribute value_name.
4 5 6 |
# File 'lib/pivot_table/grid.rb', line 4 def value_name @value_name end |
Instance Method Details
#build ⇒ Object
16 17 18 19 20 21 |
# File 'lib/pivot_table/grid.rb', line 16 def build populate_grid build_rows build_columns self end |
#build_columns ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/pivot_table/grid.rb', line 35 def build_columns @columns = [] data_grid.transpose.each_with_index do |data, index| columns << Column.new( :header => column_headers[index], :data => data, :value_name => value_name, :orthogonal_headers => row_headers ) end end |
#build_rows ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/pivot_table/grid.rb', line 23 def build_rows @rows = [] data_grid.each_with_index do |data, index| rows << Row.new( :header => row_headers[index], :data => data, :value_name => value_name, :orthogonal_headers => column_headers ) end end |
#column_headers ⇒ Object
47 48 49 |
# File 'lib/pivot_table/grid.rb', line 47 def column_headers @column_headers ||= headers column_name end |
#column_totals ⇒ Object
55 56 57 |
# File 'lib/pivot_table/grid.rb', line 55 def column_totals columns.map { |c| c.total } end |
#grand_total ⇒ Object
63 64 65 |
# File 'lib/pivot_table/grid.rb', line 63 def grand_total column_totals.inject(0) { |t, x| t + x } end |
#populate_grid ⇒ Object
75 76 77 78 79 80 81 |
# File 'lib/pivot_table/grid.rb', line 75 def populate_grid prepare_grid row_headers.each_with_index do |row, row_index| data_grid[row_index] = build_data_row(row) end data_grid end |
#prepare_grid ⇒ Object
67 68 69 70 71 72 73 |
# File 'lib/pivot_table/grid.rb', line 67 def prepare_grid @data_grid = [] row_headers.count.times do data_grid << column_headers.count.times.inject([]) { |col| col << nil } end data_grid end |
#row_headers ⇒ Object
51 52 53 |
# File 'lib/pivot_table/grid.rb', line 51 def row_headers @row_headers ||= headers row_name end |
#row_totals ⇒ Object
59 60 61 |
# File 'lib/pivot_table/grid.rb', line 59 def row_totals rows.map { |r| r.total } end |