Class: SimpleTables::PivotTable

Inherits:
Hash
  • Object
show all
Defined in:
lib/simple_tables/pivot_table.rb

Instance Method Summary collapse

Constructor Details

#initializePivotTable

Returns a new instance of PivotTable.



3
4
5
# File 'lib/simple_tables/pivot_table.rb', line 3

def initialize
  self.default=0
end

Instance Method Details

#add_column(name, &block) ⇒ Object



7
8
9
10
# File 'lib/simple_tables/pivot_table.rb', line 7

def add_column(name,&block)
  added_columns[name]=block
  self
end

#added_columnsObject



12
13
14
# File 'lib/simple_tables/pivot_table.rb', line 12

def added_columns
  @added_columns||=Hash.new
end

#as_arraysObject



36
37
38
39
40
41
42
# File 'lib/simple_tables/pivot_table.rb', line 36

def as_arrays
  result=[['#']+column_headers(true)]

  result << rows

  result
end

#column_headers(all = false) ⇒ Object



16
17
18
19
20
# File 'lib/simple_tables/pivot_table.rb', line 16

def column_headers(all=false)
  return ['#'] + self.keys.map{|k| k[0]}.uniq + added_columns.keys if all

  self.keys.map{|k| k[0]}.uniq
end

#row_headersObject



22
23
24
# File 'lib/simple_tables/pivot_table.rb', line 22

def row_headers
  self.map{|k,v| k[1]}.uniq
end

#rowsObject



26
27
28
29
30
31
32
33
34
# File 'lib/simple_tables/pivot_table.rb', line 26

def rows
  result=[]
  row_headers.each do |rh|
    row = column_headers.map{|ch| self[[ch,rh]]}
    result << [rh] + row + added_columns.values.map{|v| v.call row }
  end

  result
end