Class: Rubypivot::PivotRow

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(title, data_type = nil) ⇒ PivotRow

Returns a new instance of PivotRow.



35
36
37
38
39
# File 'lib/rubypivot/pivot_row.rb', line 35

def initialize(title, data_type = nil)
  @title = title # Title of the row : String
  @data_type = data_type
  @data = {}
end

Instance Attribute Details

#titleObject (readonly)

Returns the value of attribute title.



34
35
36
# File 'lib/rubypivot/pivot_row.rb', line 34

def title
  @title
end

Instance Method Details

#add(column_title, value) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/rubypivot/pivot_row.rb', line 41

def add(column_title, value)
  return unless value
  case @data_type
  when :integer
    @data[column_title] = 0 unless @data[column_title]
    @data[column_title] += value.to_i
  when :float
    @data[column_title] = 0.0 unless @data[column_title]
    @data[column_title] += value.to_f
  when :string
    @data[column_title] = value.to_s
  else # raw data
    @data[column_title] = value
  end
end

#get(column_title) ⇒ Object



57
58
59
60
# File 'lib/rubypivot/pivot_row.rb', line 57

def get(column_title)
  return nil if column_title.nil?
  @data[column_title]
end

#to_a(column_titles) ⇒ Object



62
63
64
# File 'lib/rubypivot/pivot_row.rb', line 62

def to_a(column_titles)
  column_titles.map{|column_title| @data[column_title] }
end

#total(column_titles) ⇒ Object



66
67
68
69
70
71
72
73
# File 'lib/rubypivot/pivot_row.rb', line 66

def total(column_titles)
  return unless [:integer, :float].include?(@data_type)
  total = @data_type == :float ? 0.0 : 0
  column_titles.each do |column_title|
    total += @data[column_title] if @data[column_title]
  end
  total
end