Class: Alf::Engine::Coerce

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

Overview

Coerce input tuples according to a Heading. Attributes that do not belong to the heading are simply ignored but are kept in the output.

Example:

operand = [
  {:name => "Jones", :price => "10.0"}, 
  {:name => "Smith", :price => "-12.0"}
]

Coerce.new(operand, Heading[:price => Float]).to_a
# => [
#      {:name => "Jones", :price => 10.0}, 
#      {:name => "Smith", :price => -12.0}
#    ]

Constant Summary

Constants included from Cog

Alf::Engine::Cog::EMPTY_CHILDREN, Alf::Engine::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, coercions, expr = nil, compiler = nil) ⇒ Coerce

Creates an Coerce instance


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

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

Instance Attribute Details

#coercionsHeading (readonly)

Returns Heading for coercions

Returns:

  • (Heading)

    Heading for coercions


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

def coercions
  @coercions
end

#operandEnumerable (readonly)

Returns The operand

Returns:

  • (Enumerable)

    The operand


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

def operand
  @operand
end

Instance Method Details

#_eachObject


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

def _each
  @operand.each do |tuple|
    yield tuple.merge(Hash[@coercions.map{|k,d|
      [k, Support.coerce(tuple[k], d)]
    }])
  end
end

#argumentsObject


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

def arguments
  [ coercions ]
end