class MetricFu::Grouping

Public Class Methods

new(table, opts) click to toggle source
# 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

Public Instance Methods

[](key) click to toggle source
# 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
each() { |value, rows| ... } click to toggle source
# File lib/base/grouping.rb, line 35
def each
  @arr.each do |value, rows|
    yield value, rows
  end
end