lib/pivot_table/grid.rb in pivot_table-0.1.1 vs lib/pivot_table/grid.rb in pivot_table-0.1.3
- old
+ new
@@ -1,9 +1,9 @@
module PivotTable
class Grid
- attr_accessor :source_data, :row_name, :column_name
+ 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
@@ -16,18 +16,18 @@
end
def build_rows
@rows = []
@data_grid.each_with_index do |data, index|
- @rows << Row.new(:header => row_headers[index], :data => data)
+ @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)
+ @columns << Column.new(:header => column_headers[index], :data => data, :value_name => value_name)
end
end
def column_headers
headers @column_name
@@ -35,10 +35,22 @@
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
@@ -61,6 +73,6 @@
def headers method
@source_data.collect { |c| c.send method }.uniq.sort
end
end
-end
\ No newline at end of file
+end