Class: TableCreator::ResultGroup

Inherits:
Object
  • Object
show all
Defined in:
lib/table_creator/result_group.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(group_object, rows, total_levels = 0) ⇒ ResultGroup

Returns a new instance of ResultGroup.



7
8
9
10
11
12
# File 'lib/table_creator/result_group.rb', line 7

def initialize(group_object, rows, total_levels = 0)
  @group_object = (group_object.blank? && group_object != false) ? nil : group_object.to_s
  @rows  = rows
  @level = 0
  @total_levels = total_levels
end

Instance Attribute Details

#group_objectObject

Returns the value of attribute group_object.



3
4
5
# File 'lib/table_creator/result_group.rb', line 3

def group_object
  @group_object
end

#levelObject

Returns the value of attribute level.



4
5
6
# File 'lib/table_creator/result_group.rb', line 4

def level
  @level
end

#rowsObject

Returns the value of attribute rows.



3
4
5
# File 'lib/table_creator/result_group.rb', line 3

def rows
  @rows
end

#sumObject

Returns the value of attribute sum.



5
6
7
# File 'lib/table_creator/result_group.rb', line 5

def sum
  @sum
end

#total_levelsObject

Returns the value of attribute total_levels.



4
5
6
# File 'lib/table_creator/result_group.rb', line 4

def total_levels
  @total_levels
end

Instance Method Details

#aggregate(fields) ⇒ Object

fields is a hash where:

key is the field/method
value is the aggregate type (currently only :sum)

Raises:



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/table_creator/result_group.rb', line 87

def aggregate(fields)
  invalid_aggregates = fields.values.uniq - [:sum]
  raise Error.new("Aggregation #{invalid_aggregates.to_sentence} not implemented") if invalid_aggregates.present?

  @sum ||= {}

  fields.each do |field, aggregation|
    @sum[field] = nil

    if @rows.first.is_a?(ResultGroup)
      @sum[field] = @rows.map do |row|
        # agregate each lower levels
        row.aggregate(fields)

        case aggregation
        when :sum
          row.sum[field]
        end
      end.compact.sum
    else
      @sum[field] = @rows.map do |row|
        case aggregation
        when :sum
          row.send(field) || 0 # encase result is nil
        end
      end.compact.sum
    end
  end
end

#countObject



26
27
28
# File 'lib/table_creator/result_group.rb', line 26

def count
  @rows.map { |r| r.is_a?(self.class) ? r.count : 1 }.sum
end

#group(groups, order = nil) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/table_creator/result_group.rb', line 31

def group(groups, order = nil)
  groups = [groups] unless groups.is_a?(Array)

  return if groups.empty?
  new_rows = []

  first_group = groups.shift
  new_group = if @rows.first.is_a? ResultGroup
    @rows.map{|rg| [rg.group_object, rg.group_children(first_group, order)]}
  else
    if first_group == true
      {'All' => @rows}
    elsif first_group.is_a?(Hash)
      @rows.group_by{|r|
        r.send(first_group.keys.first).send(first_group.values.first)
      }
    else
      @rows.group_by{|r| r.send(first_group)}
    end
  end

  new_group = case order.try(:to_sym)
  when :group_name
    Hash[new_group.sort { |a,b| a.first.to_s <=> b.first.to_s }]
  when :row_count
    Hash[new_group.sort { |a,b| b.last.size <=> a.last.size }]
  else
    new_group
  end

  @total_levels += 1
  new_group.each do |group, rows|
    r = ResultGroup.new(group, rows, @total_levels)
    r.level = @level + 1
    new_rows << r
  end

  @rows = new_rows
  group(groups, order) unless groups.empty?
  @rows
end

#group_children(group, order = nil) ⇒ Object



74
75
76
77
78
79
80
81
# File 'lib/table_creator/result_group.rb', line 74

def group_children(group, order = nil)
  if @rows.first.is_a?(ResultGroup)
    @rows.each{|r| r.group_children(group, order)}
    @rows
  else
    self.group(group, order)
  end
end

#to_data_rows(&block) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/table_creator/result_group.rb', line 118

def to_data_rows(&block)
  rows = []
  @rows.each do |row|
    if row.is_a? ResultGroup

      sub_rows = row.to_data_rows(&block)
      format = block.call(row, nil)

      if format.is_a? Hash
        group_data = format[:group]
        group_summary_data = format[:summary]
      elsif format.is_a? Array
        group_data = nil
        group_summary_data = format
      end

      if group_data.is_a? Array
        rows << {:class => "d#{@total_levels-row.level} l#{@total_levels} group", :data => group_data}
      elsif group_data.is_a? Hash
        rows << group_data.merge(:class => "h#{row.level}")
      end

      if group_summary_data.is_a? Array
        rows << {:class => "d#{@total_levels-row.level} l#{@total_levels} summary", :data => group_summary_data}
      elsif group_summary_data.is_a? Hash
        rows << group_summary_data.merge(:class => "h#{row.level}")
      end

      rows += sub_rows
    else
      format = block.call(nil, row)

      if format.is_a? Hash
        if format[:data].first.is_a? Array
          format[:data].each do |row|
            rows << row
          end
        else
          rows << format[:data]
        end
        
      elsif format.is_a? Array
        rows << format
      end
    end
  end
  rows
end