Class: MagicReport::Report::Row

Inherits:
Object
  • Object
show all
Defined in:
lib/magic_report/report/row.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRow



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

#dataObject

Returns the value of attribute data.



6
7
8
# File 'lib/magic_report/report/row.rb', line 6

def data
  @data
end

#inner_rowsObject

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_rowsObject

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_hObject



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

#valuesObject



27
28
29
# File 'lib/magic_report/report/row.rb', line 27

def values
  data.values
end