Class: Rubypivot::PivotRows
- Inherits:
-
Object
- Object
- Rubypivot::PivotRows
- Defined in:
- lib/rubypivot/pivot_row.rb
Instance Attribute Summary collapse
-
#data_type ⇒ Object
readonly
Returns the value of attribute data_type.
-
#rows ⇒ Object
readonly
Returns the value of attribute rows.
Instance Method Summary collapse
- #get_row(row_title) ⇒ Object (also: #add_row)
-
#initialize(options = {}) ⇒ PivotRows
constructor
A new instance of PivotRows.
- #total(column_titles = [], show_grand_total = false) ⇒ Object
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 = @data_type = [:data_type] @rows = {} end |
Instance Attribute Details
#data_type ⇒ Object (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 |
#rows ⇒ Object (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 |