Class: Alf::Engine::Defaults
- Inherits:
-
Object
- Object
- Alf::Engine::Defaults
- Includes:
- Cog
- Defined in:
- lib/alf/engine/defaults.rb
Overview
Replace missing attributes and nil by default, computed values.
Example:
rel = [
{:id => 1, :name => "Jones"},
{:id => 2, :name => nil}
]
Defaults.new(rel, TupleComputation[:name => "Smith"]).to_a
# => [
# {:id => 1, :name => "Jones"},
# {:id => 2, :name => "Smith"}
# ]
Constant Summary
Constants included from Cog
Cog::EMPTY_CHILDREN, Cog::EMPTY_OPTIONS
Instance Attribute Summary collapse
-
#defaults ⇒ TupleComputation
readonly
The default values.
-
#operand ⇒ Enumerable
readonly
The operand.
Attributes included from Compiler::Cog
Instance Method Summary collapse
- #_each ⇒ Object
- #arguments ⇒ Object
-
#initialize(operand, defaults, expr = nil, compiler = nil) ⇒ Defaults
constructor
Creates a Defaults 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, defaults, expr = nil, compiler = nil) ⇒ Defaults
Creates a Defaults instance
28 29 30 31 32 |
# File 'lib/alf/engine/defaults.rb', line 28 def initialize(operand, defaults, expr = nil, compiler = nil) super(expr, compiler) @operand = operand @defaults = defaults end |
Instance Attribute Details
#defaults ⇒ TupleComputation (readonly)
Returns The default values.
25 26 27 |
# File 'lib/alf/engine/defaults.rb', line 25 def defaults @defaults end |
#operand ⇒ Enumerable (readonly)
Returns The operand.
22 23 24 |
# File 'lib/alf/engine/defaults.rb', line 22 def operand @operand end |
Instance Method Details
#_each ⇒ Object
35 36 37 38 39 40 41 |
# File 'lib/alf/engine/defaults.rb', line 35 def _each scope = tuple_scope operand.each do |tuple| defs = @defaults.evaluate(scope.__set_tuple(tuple)) yield tuple.merge(defs){|k,v1,v2| (v1.nil? ? v2 : v1)} end end |
#arguments ⇒ Object
43 44 45 |
# File 'lib/alf/engine/defaults.rb', line 43 def arguments [ defaults ] end |