Class: Predicator::Context
- Inherits:
-
Object
- Object
- Predicator::Context
show all
- Defined in:
- lib/predicator/context.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(attrs = {}) ⇒ Context
7
8
9
10
|
# File 'lib/predicator/context.rb', line 7
def initialize attrs={}
@bindings = {}
attrs.each{ |key, val| bind key, val }
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
39
40
41
42
43
44
45
46
|
# File 'lib/predicator/context.rb', line 39
def method_missing method, *args, &block
found_binding = bindings.fetch method.to_s, nil
if found_binding.nil?
super
else
found_binding
end
end
|
Instance Attribute Details
#bindings ⇒ Object
Returns the value of attribute bindings.
5
6
7
|
# File 'lib/predicator/context.rb', line 5
def bindings
@bindings
end
|
Instance Method Details
#bind(name, obj) ⇒ Object
Also known as:
[]=
12
13
14
15
16
17
|
# File 'lib/predicator/context.rb', line 12
def bind name, obj
if obj.kind_of? Hash
obj = OpenStruct.new obj
end
bindings[name.to_s] = obj
end
|
#eval(string) ⇒ Object
48
49
50
51
52
53
54
|
# File 'lib/predicator/context.rb', line 48
def eval string
string.gsub(/{{([^}]+)}}/) do |match|
name, attribute = $1.strip.split "."
variable = Predicator::Variable.new name, attribute
value_for variable
end
end
|
#node_for(input) ⇒ Object
20
21
22
23
24
|
# File 'lib/predicator/context.rb', line 20
def node_for input
value = value_for input
node_class = Predicator::Nodes::BaseNode.class_for value
node_class.new value
end
|
#respond_to?(method) ⇒ Boolean
35
36
37
|
# File 'lib/predicator/context.rb', line 35
def respond_to? method
bindings.key?(method.to_s) || super
end
|
#to_hash ⇒ Object
56
57
58
59
60
61
62
|
# File 'lib/predicator/context.rb', line 56
def to_hash
hsh = {}
bindings.each do |key, value|
hsh[key] = hash_value_for value
end
hsh
end
|
#value_for(input) ⇒ Object
Also known as:
[]
26
27
28
29
30
31
32
|
# File 'lib/predicator/context.rb', line 26
def value_for input
if input.kind_of? Predicator::Variable
input.value_in self
else
input
end
end
|