Class: Grouping

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

Instance Method Summary collapse

Constructor Details

#initialize(table, opts) ⇒ Grouping

Returns a new instance of Grouping.



370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
# File 'lib/base/metric_analyzer.rb', line 370

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



389
390
391
392
393
394
# File 'lib/base/metric_analyzer.rb', line 389

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

#eachObject



396
397
398
399
400
# File 'lib/base/metric_analyzer.rb', line 396

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