Class: Alf::Engine::Coerce

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Cog

#each, #to_dot, #to_relation

Constructor Details

#initialize(operand, coercions) ⇒ Coerce

Creates an Coerce instance



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

def initialize(operand, coercions)
  @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/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/alf/engine/coerce.rb', line 24

def operand
  @operand
end

Instance Method Details

#_eachObject



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

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