Class: Rubypivot::PivotRows

Inherits:
Object
  • Object
show all
Defined in:
lib/rubypivot/pivot_row.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ PivotRows

Returns a new instance of PivotRows.



4
5
6
7
8
# File 'lib/rubypivot/pivot_row.rb', line 4

def initialize(options = {})
  @options = options
  @data_type = options[:data_type]
  @rows = {}
end

Instance Attribute Details

#data_typeObject (readonly)

Returns the value of attribute data_type.



3
4
5
# File 'lib/rubypivot/pivot_row.rb', line 3

def data_type
  @data_type
end

#rowsObject (readonly)

Returns the value of attribute rows.



3
4
5
# File 'lib/rubypivot/pivot_row.rb', line 3

def rows
  @rows
end

Instance Method Details

#get_row(row_title) ⇒ Object Also known as: add_row



10
11
12
# File 'lib/rubypivot/pivot_row.rb', line 10

def get_row(row_title)
  @rows[row_title] ||= PivotRow.new(row_title, @data_type)
end

#total(column_titles = [], show_grand_total = false) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/rubypivot/pivot_row.rb', line 15

def total(column_titles = [], show_grand_total = false)
  return ['Total', 'row', 'can', 'not', 'create', "type :#{@data_type}"] unless [:integer, :float].include?(@data_type)
  grand_total = @data_type == :float ? 0.0 : 0
  data_array = []
  column_titles.each do |column_title|
      total = @data_type == :float ? 0.0 : 0
      @rows.each do |row_title, row|
        v = row.get(column_title)
        total += v if v
      end
      grand_total += total if show_grand_total
      data_array << total
  end
  data_array << grand_total if show_grand_total
  data_array
end