Class: Proforma::Compiling::Aggregation

Inherits:
Object
  • Object
show all
Defined in:
lib/proforma/compiling/aggregation.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(aggregators, evaluator) ⇒ Aggregation

Returns a new instance of Aggregation.

Raises:

  • (ArgumentError)


16
17
18
19
20
21
22
23
24
# 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

Instance Attribute Details

#aggregatorsObject (readonly)

Returns the value of attribute aggregators.



14
15
16
# File 'lib/proforma/compiling/aggregation.rb', line 14

def aggregators
  @aggregators
end

#evaluatorObject (readonly)

Returns the value of attribute evaluator.



14
15
16
# File 'lib/proforma/compiling/aggregation.rb', line 14

def evaluator
  @evaluator
end

Instance Method Details

#add(records) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
# 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_hObject



40
41
42
# File 'lib/proforma/compiling/aggregation.rb', line 40

def to_h
  aggregators.map { |aggregator| execute(aggregator) }.to_h
end