Class: Alf::Engine::Aggregate

Inherits:
Object
  • Object
show all
Includes:
Cog
Defined in:
lib/alf-engine/alf/engine/aggregate.rb

Overview

Aggregates the whole operand according to a Summarization. The result contains only one tuple.

Example:

res = [
  {:name => "Jones", :price => 12.0, :id => 1},
  {:name => "Smith", :price => 10.0, :id => 2}
]
agg = Summarization[:size  => "count", 
                    :total => "sum{ price }"]
Aggregate.new(res, agg).to_a
# => [
#      {:size => 2, :total => 22.0}
#    ]

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Cog

#each, #to_dot, #to_relation

Constructor Details

#initialize(operand, summarization) ⇒ Aggregate

Creates an Aggregate instance



30
31
32
33
# File 'lib/alf-engine/alf/engine/aggregate.rb', line 30

def initialize(operand, summarization)
  @operand = operand
  @summarization = summarization
end

Instance Attribute Details

#operandEnumerable (readonly)

Returns The operand.

Returns:

  • (Enumerable)

    The operand



24
25
26
# File 'lib/alf-engine/alf/engine/aggregate.rb', line 24

def operand
  @operand
end

#summarizationSummarization (readonly)

Returns The summarization to use.

Returns:

  • (Summarization)

    The summarization to use



27
28
29
# File 'lib/alf-engine/alf/engine/aggregate.rb', line 27

def summarization
  @summarization
end

Instance Method Details

#_each {|@summarization.finalize(agg)| ... } ⇒ Object

Yields:



36
37
38
39
40
41
42
# File 'lib/alf-engine/alf/engine/aggregate.rb', line 36

def _each
  scope = tuple_scope
  agg = operand.inject(@summarization.least) do |memo,tuple|
    @summarization.happens(memo, scope.__set_tuple(tuple))
  end
  yield @summarization.finalize(agg)
end