class Evaluator
Represents a lazy evaluator that resolves and caches environment values on demand.
Definitions
def initialize(environment)
Initialize the evaluator with an environment.
Signature
-
parameter
environmentEnvironment The environment whose values will be evaluated.
Implementation
def initialize(environment)
@environment = environment
@cache = {}
end
def initialize_dup(other)
Copy the environment reference and cache when duplicating.
Signature
-
parameter
otherEvaluator The evaluator being duplicated.
Implementation
def initialize_dup(other)
@environment = other.instance_variable_get(:@environment)
@cache = other.instance_variable_get(:@cache).dup
super
end
def respond_to?(name, include_private = false)
Check whether the evaluator responds to a given method name.
Signature
-
parameter
nameSymbol The method name to check.
-
parameter
include_privateBoolean Whether to include private methods.
-
returns
Boolean trueif the environment contains the key or the evaluator responds viasuper.
Implementation
def respond_to?(name, include_private = false)
@environment.include?(name) || super
end
def method_missing(name)
Evaluate and cache the value for the named key.
Signature
-
parameter
nameSymbol The key to resolve from the environment.
-
returns
Object The resolved and cached value.
Implementation
def method_missing(name)
@cache[name] ||= object_value(@environment[name])
end
def object_value(value)
Compute the literal object value for a given key:
Implementation
def object_value(value)
case value
when Array
value.collect{|item| object_value(item)}.flatten
when Symbol
object_value(@environment[value])
when Proc
object_value(instance_exec(&value))
when Default
object_value(value.value)
when Replace
object_value(value.value)
else
value
end
end