class Proforma::Compiling::Aggregation

This class is a group of aggregators that knows how to process records.

Attributes

aggregators[R]
counters[R]
evaluator[R]

Public Class Methods

new(aggregators, evaluator) click to toggle source
# File lib/proforma/compiling/aggregation.rb, line 16
def initialize(aggregators, evaluator)
  raise ArgumentError, 'evaluator is required' unless evaluator

  @aggregators  = Array(aggregators)
  @counters     = {}
  @evaluator    = evaluator

  freeze
end

Public Instance Methods

add(records) click to toggle source
# File lib/proforma/compiling/aggregation.rb, line 26
def add(records)
  records.each do |record|
    aggregators.each do |aggregator|
      property  = aggregator.property
      value     = property.to_s.empty? ? nil : evaluator.value(record, property)
      name      = aggregator.name

      entry(name).add(value)
    end
  end

  self
end
to_h() click to toggle source
# File lib/proforma/compiling/aggregation.rb, line 40
def to_h
  aggregators.map { |aggregator| execute(aggregator) }.to_h
end

Private Instance Methods

entry(name) click to toggle source
# File lib/proforma/compiling/aggregation.rb, line 48
def entry(name)
  counters[name.to_s] ||= Counter.new
end
execute(aggregator) click to toggle source
# File lib/proforma/compiling/aggregation.rb, line 52
def execute(aggregator)
  name      = aggregator.name
  function  = aggregator.function

  raise ArgumentError, "bad func: #{function}" unless entry(name).respond_to?(function)

  value = entry(name).send(function)

  [name, value.to_s('f')]
end