Class: Alf::Engine::Aggregate

Inherits:
Object
  • Object
show all
Includes:
Cog
Defined in:
lib/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}
#    ]

Constant Summary

Constants included from Cog

Cog::EMPTY_CHILDREN, Cog::EMPTY_OPTIONS

Instance Attribute Summary collapse

Attributes included from Compiler::Cog

#compiler, #expr

Instance Method Summary collapse

Methods included from Cog

#children, #each, #options, #to_s

Methods included from Compiler::Cog

#cog_orders, #orderedby?, #relation_type, #to_ascii_tree, #to_cog, #to_relation

Constructor Details

#initialize(operand, summarization, expr = nil, compiler = nil) ⇒ Aggregate

Creates an Aggregate instance


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

def initialize(operand, summarization, expr = nil, compiler = nil)
  super(expr, compiler)
  @operand = operand
  @summarization = summarization
end

Instance Attribute Details

#operandEnumerable (readonly)


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

def operand
  @operand
end

#summarizationSummarization (readonly)


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

def summarization
  @summarization
end

Instance Method Details

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

Yields:


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

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

#argumentsObject


45
46
47
# File 'lib/alf/engine/aggregate.rb', line 45

def arguments
  [ summarization ]
end