Sha256: ba86960251fee506be831eecc38c84ca58b2e95ec8c45cdab98584441fe54643

Contents?: true

Size: 862 Bytes

Versions: 1

Compression:

Stored size: 862 Bytes

Contents

%w(table).each do |path|
  MetricFu.data_structures_require { path }
end
module MetricFu
  class Grouping

    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]] ||= MetricFu::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

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

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

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
metric_fu-2.1.3.5 lib/data_structures/grouping.rb