Class: Axiom::Evaluator::Context

Inherits:
BasicObject
Extended by:
Aliasable
Defined in:
lib/axiom/support/evaluator.rb

Overview

Provide a context to evaluate a Relation operation block

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Aliasable

inheritable_alias

Constructor Details

#initialize(header) ⇒ undefined

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initialize a Context



33
34
35
36
37
38
# File 'lib/axiom/support/evaluator.rb', line 33

def initialize(header)
  @header    = header
  @functions = ::Hash.new
  @yield     = yield self
  @functions.freeze
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Attribute (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Lookup the attribute in the header using the attribute name

Examples:

attribute = context.id


117
118
119
120
121
# File 'lib/axiom/support/evaluator.rb', line 117

def method_missing(name, *args)
  super unless respond_to?(name)
  ::Kernel.raise ::ArgumentError, "wrong number of arguments (#{args.length} for 0)" unless args.empty?
  self[name]
end

Instance Attribute Details

#functionsHash (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The functions to evaluate



17
18
19
# File 'lib/axiom/support/evaluator.rb', line 17

def functions
  @functions
end

#yieldObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return the block results



24
25
26
# File 'lib/axiom/support/evaluator.rb', line 24

def yield
  @yield
end

Instance Method Details

#[](name) ⇒ Attribute

Lookup the attribute in the header

Examples:

attribute = context[name]


79
80
81
# File 'lib/axiom/support/evaluator.rb', line 79

def [](name)
  @header[name]
end

#add(attribute, object = Undefined) { ... } ⇒ self

Add a function to be evaluated by the summarization operation

Examples:

of a function

context.add(:total, context[:unit_price] * context[:quantity])

of a block

context.add(:total) { |tuple| tuple[:unit_price] * tuple[:quantity] } }

Yields:

  • [] optional block to execute in the summarization operation



59
60
61
62
63
64
65
66
67
# File 'lib/axiom/support/evaluator.rb', line 59

def add(attribute, object = Undefined, &block)
  object = block if object.equal?(Undefined)
  type   = Attribute.infer_type(object)
  klass  = Attribute.descendants.detect do |descendant|
    descendant.type >= type
  end
  functions[klass.coerce(attribute)] = object
  self
end

#respond_to?(name) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Test if the method is supported on this object



90
91
92
# File 'lib/axiom/support/evaluator.rb', line 90

def respond_to?(name, *)
  @header.any? { |attribute| attribute.name.equal?(name) }
end

#send(*args, &block) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Forward a message to the object



101
102
103
# File 'lib/axiom/support/evaluator.rb', line 101

def send(*args, &block)
  __send__(*args, &block)
end