Class: MagicReport::Report::Row
- Inherits:
-
Object
- Object
- MagicReport::Report::Row
- Defined in:
- lib/magic_report/report/row.rb
Instance Attribute Summary collapse
-
#data ⇒ Object
Returns the value of attribute data.
-
#inner_rows ⇒ Object
Returns the value of attribute inner_rows.
-
#nested_rows ⇒ Object
Returns the value of attribute nested_rows.
Instance Method Summary collapse
- #add(field:, value:) ⇒ Object
- #add_inner_row(field:, row:) ⇒ Object
- #add_nested_row(field:, row:) ⇒ Object
- #complex? ⇒ Boolean
- #each_inner_row(&block) ⇒ Object
- #each_nested_row(&block) ⇒ Object
-
#initialize ⇒ Row
constructor
A new instance of Row.
- #to_h ⇒ Object
- #values ⇒ Object
Constructor Details
#initialize ⇒ Row
8 9 10 11 12 |
# File 'lib/magic_report/report/row.rb', line 8 def initialize @data = {} @inner_rows = {} @nested_rows = {} end |
Instance Attribute Details
#data ⇒ Object
Returns the value of attribute data.
6 7 8 |
# File 'lib/magic_report/report/row.rb', line 6 def data @data end |
#inner_rows ⇒ Object
Returns the value of attribute inner_rows.
6 7 8 |
# File 'lib/magic_report/report/row.rb', line 6 def inner_rows @inner_rows end |
#nested_rows ⇒ Object
Returns the value of attribute nested_rows.
6 7 8 |
# File 'lib/magic_report/report/row.rb', line 6 def nested_rows @nested_rows end |
Instance Method Details
#add(field:, value:) ⇒ Object
14 15 16 |
# File 'lib/magic_report/report/row.rb', line 14 def add(field:, value:) data[field] = value end |
#add_inner_row(field:, row:) ⇒ Object
18 19 20 |
# File 'lib/magic_report/report/row.rb', line 18 def add_inner_row(field:, row:) inner_rows[field] = row end |
#add_nested_row(field:, row:) ⇒ Object
22 23 24 25 |
# File 'lib/magic_report/report/row.rb', line 22 def add_nested_row(field:, row:) nested_rows[field] ||= [] nested_rows[field].push(row) end |
#complex? ⇒ Boolean
31 32 33 |
# File 'lib/magic_report/report/row.rb', line 31 def complex? nested_rows.any? end |
#each_inner_row(&block) ⇒ Object
43 44 45 46 47 |
# File 'lib/magic_report/report/row.rb', line 43 def each_inner_row(&block) inner_rows.values.map do |inner_row| block.call(inner_row) end end |
#each_nested_row(&block) ⇒ Object
35 36 37 38 39 40 41 |
# File 'lib/magic_report/report/row.rb', line 35 def each_nested_row(&block) nested_rows.keys.flat_map do |key| nested_rows[key].map do |nested_row| block.call(nested_row) end end end |
#to_h ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/magic_report/report/row.rb', line 49 def to_h @to_h ||= begin original_hash = inner_rows.any? ? data.merge(each_inner_row { |inner_row| inner_row.to_h }.reduce({}, :merge)) : data if complex? each_nested_row do |nested_row| original_hash.merge(nested_row.to_h) end else original_hash end end end |
#values ⇒ Object
27 28 29 |
# File 'lib/magic_report/report/row.rb', line 27 def values data.values end |