Class: Alf::Engine::Coerce
- Inherits:
-
Object
- Object
- Alf::Engine::Coerce
- 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
-
#coercions ⇒ Heading
readonly
Heading for coercions.
-
#operand ⇒ Enumerable
readonly
The operand.
Attributes included from Compiler::Cog
Instance Method Summary collapse
- #_each ⇒ Object
- #arguments ⇒ Object
-
#initialize(operand, coercions, expr = nil, compiler = nil) ⇒ Coerce
constructor
Creates an Coerce instance.
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
#coercions ⇒ Heading (readonly)
Returns Heading for coercions.
27 28 29 |
# File 'lib/alf/engine/coerce.rb', line 27 def coercions @coercions end |
#operand ⇒ Enumerable (readonly)
Returns The operand.
24 25 26 |
# File 'lib/alf/engine/coerce.rb', line 24 def operand @operand end |
Instance Method Details
#_each ⇒ Object
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 |
#arguments ⇒ Object
45 46 47 |
# File 'lib/alf/engine/coerce.rb', line 45 def arguments [ coercions ] end |