Class: TExp::EvalEnvironment
Overview
Internal class that provides an evaluation environment for temporal expressions. The temporal expression DSL methods are not generally exposed outside of the TExp module, meaning that any coded temporal expression must be prefixed with the TExp module name. This is cumbersome for a DSL.
One solution is to just include the TExp module wherever you wish to use temporal expressions. Including at the top level would make them available everywhere, but would also pollute the top level namespace with all of TExp’s methods.
An alternate solution is to use a block that is instance_eval’ed in the TExp namespace. Within in the block you can reference the TExp DSL methods freely, but they do not pollute any namespaces outside the block.
Unfortunately, the instance_eval technique will cut off access to methods defined in the calling namespace (since we are using TExp’s environment instead). The EvalEnvironment handles this by directing any unknown methods to the calling environment, making the instance_eval technique much more friendly.
See the texp
method to see how this class is used.
Instance Method Summary collapse
-
#initialize(containing_env) ⇒ EvalEnvironment
constructor
Create a TExp environment for evaluating temporal expressions.
-
#method_missing(sym, *args, &block) ⇒ Object
The methods identified by
sym
is not found in the TExp namespace.
Methods included from DSL
#day, #dow, #evaluate_expression_in_environment, #every, #month, #normalize_units, #on, #week, #year
Constructor Details
#initialize(containing_env) ⇒ EvalEnvironment
Create a TExp environment for evaluating temporal expressions. Use the block binding to find the object where the block is embeded.
314 315 316 |
# File 'lib/texp/dsl.rb', line 314 def initialize(containing_env) @container = eval "self", containing_env end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(sym, *args, &block) ⇒ Object
The methods identified by sym
is not found in the TExp namespace. Try calling it in the containing namespace.
320 321 322 |
# File 'lib/texp/dsl.rb', line 320 def method_missing(sym, *args, &block) @container.send(sym, *args, &block) end |