Class: MetricFu::Grouping

Inherits:
Object
  • Object
show all
Defined in:
lib/base/grouping.rb

Instance Method Summary collapse

Constructor Details

#initialize(table, opts) ⇒ Grouping

Returns a new instance of Grouping.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/base/grouping.rb', line 9

def initialize(table, opts)
  column_name = opts.fetch(:by)
  order = opts.fetch(:order) { nil }
  hash = {}
  if column_name.to_sym == :metric # special optimized case
    hash = table.group_by_metric
  else
    table.each do |row|
      hash[row[column_name]] ||= Table.new(:column_names => row.attributes)
      hash[row[column_name]] << row
    end
  end
  if order
    @arr = hash.sort_by &order
  else
    @arr = hash.to_a
  end
end

Instance Method Details

#[](key) ⇒ Object



28
29
30
31
32
33
# File 'lib/base/grouping.rb', line 28

def [](key)
  @arr.each do |group_key, table|
    return table if group_key == key
  end
  return nil
end

#eachObject



35
36
37
38
39
# File 'lib/base/grouping.rb', line 35

def each
  @arr.each do |value, rows|
    yield value, rows
  end
end