Class: Alf::Engine::Defaults

Inherits:
Object
  • Object
show all
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

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, 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

#defaultsTupleComputation (readonly)


25
26
27
# File 'lib/alf/engine/defaults.rb', line 25

def defaults
  @defaults
end

#operandEnumerable (readonly)


22
23
24
# File 'lib/alf/engine/defaults.rb', line 22

def operand
  @operand
end

Instance Method Details

#_eachObject


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

#argumentsObject


43
44
45
# File 'lib/alf/engine/defaults.rb', line 43

def arguments
  [ defaults ]
end