Sha256: 64f2ef3d1b047669a11dc59f00b3e6e01f552a1a3dd6f5350319f41506d76f61
Contents?: true
Size: 1.81 KB
Versions: 1
Compression:
Stored size: 1.81 KB
Contents
module PivotTable class Grid attr_accessor :source_data, :row_name, :column_name, :value_name attr_reader :columns, :rows, :data_grid def initialize(&block) yield(self) if block_given? end def build populate_grid build_rows build_columns self end 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 ) end end 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 ) end end def column_headers headers @column_name end def row_headers headers @row_name end def column_totals columns.map { |c| c.total } end def row_totals rows.map { |r| r.total } end def grand_total column_totals.inject(0) { |t, x| t + x } end def prepare_grid @data_grid = [] row_headers.count.times do @data_grid << column_headers.count.times.inject([]) { |col| col << nil } end @data_grid end def populate_grid prepare_grid row_headers.each_with_index do |row, row_index| current_row = [] column_headers.each_with_index do |col, col_index| current_row[col_index] = @source_data.find { |item| item.send(row_name) == row && item.send(column_name) == col } end @data_grid[row_index] = current_row end @data_grid end private def headers(method) @source_data.collect { |c| c.send method }.uniq.sort end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
pivot_table-0.1.4 | lib/pivot_table/grid.rb |