Class: PivotTable::Grid

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

Constant Summary collapse

DEFAULT_OPTIONS =
{
  :sort => true
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) {|_self| ... } ⇒ Grid

Returns a new instance of Grid.

Yields:

  • (_self)

Yield Parameters:



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_nameObject

Returns the value of attribute column_name.



4
5
6
# File 'lib/pivot_table/grid.rb', line 4

def column_name
  @column_name
end

#columnsObject (readonly)

Returns the value of attribute columns.



5
6
7
# File 'lib/pivot_table/grid.rb', line 5

def columns
  @columns
end

#configurationObject (readonly)

Returns the value of attribute configuration.



5
6
7
# File 'lib/pivot_table/grid.rb', line 5

def configuration
  @configuration
end

#data_gridObject (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_nameObject

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_nameObject

Returns the value of attribute row_name.



4
5
6
# File 'lib/pivot_table/grid.rb', line 4

def row_name
  @row_name
end

#rowsObject (readonly)

Returns the value of attribute rows.



5
6
7
# File 'lib/pivot_table/grid.rb', line 5

def rows
  @rows
end

#source_dataObject

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_nameObject

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

#buildObject



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_columnsObject



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_rowsObject



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_headersObject



47
48
49
# File 'lib/pivot_table/grid.rb', line 47

def column_headers
  @column_headers ||= headers column_name
end

#column_totalsObject



55
56
57
# File 'lib/pivot_table/grid.rb', line 55

def column_totals
  columns.map { |c| c.total }
end

#grand_totalObject



63
64
65
# File 'lib/pivot_table/grid.rb', line 63

def grand_total
  column_totals.inject(0) { |t, x| t + x }
end

#populate_gridObject



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_gridObject



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_headersObject



51
52
53
# File 'lib/pivot_table/grid.rb', line 51

def row_headers
  @row_headers ||= headers row_name
end

#row_totalsObject



59
60
61
# File 'lib/pivot_table/grid.rb', line 59

def row_totals
  rows.map { |r| r.total }
end