Class: BinData::LazyEvalEnv
- Inherits:
-
Object
- Object
- BinData::LazyEvalEnv
- Defined in:
- lib/bindata/lazy.rb
Overview
The enviroment in which a lazily evaluated lamba is called. These lambdas are those that are passed to data objects as parameters. Each lambda has access to the following:
- parent
-
the environment of the parent data object
- params
-
any extra parameters that have been passed to the data object. The value of a parameter is either a lambda, a symbol or a literal value (such as a Fixnum).
Unknown methods are resolved in the context of the parent environment, first as keys in the extra parameters, and secondly as methods in the parent data object. This makes the lambda easier to read as we just write field
instead of obj.field
.
Constant Summary collapse
- @@empty_hash =
An empty hash shared by all instances
Hash.new.freeze
Instance Attribute Summary collapse
-
#data_object ⇒ Object
writeonly
Sets the attribute data_object.
-
#params ⇒ Object
Returns the value of attribute params.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
Instance Method Summary collapse
-
#add_variable(sym, value) ⇒ Object
Add a variable with a pre-assigned value to this environment.
-
#initialize(parent = nil) ⇒ LazyEvalEnv
constructor
Creates a new environment.
-
#lazy_eval(obj, overrides = nil) ⇒ Object
Evaluates
obj
in the context of this environment. - #method_missing(symbol, *args) ⇒ Object
-
#offset_of(sym) ⇒ Object
TODO: offset_of needs to be better thought out.
-
#parent_data_object ⇒ Object
Returns the data_object for the parent environment.
Constructor Details
#initialize(parent = nil) ⇒ LazyEvalEnv
Creates a new environment. parent
is the environment of the parent data object.
21 22 23 24 25 26 |
# File 'lib/bindata/lazy.rb', line 21 def initialize(parent = nil) @parent = parent @variables = @@empty_hash @overrides = @@empty_hash @params = @@empty_hash end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(symbol, *args) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/bindata/lazy.rb', line 75 def method_missing(symbol, *args) if @overrides.include?(symbol) @overrides[symbol] elsif @variables.include?(symbol) @variables[symbol] elsif @parent obj = symbol if @parent.params and @parent.params.has_key?(symbol) obj = @parent.params[symbol] elsif @parent.data_object and @parent.data_object.respond_to?(symbol) obj = @parent.data_object.__send__(symbol, *args) end @parent.lazy_eval(obj) else super end end |
Instance Attribute Details
#data_object=(value) ⇒ Object
Sets the attribute data_object
28 29 30 |
# File 'lib/bindata/lazy.rb', line 28 def data_object=(value) @data_object = value end |
#params ⇒ Object
Returns the value of attribute params.
27 28 29 |
# File 'lib/bindata/lazy.rb', line 27 def params @params end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
27 28 29 |
# File 'lib/bindata/lazy.rb', line 27 def parent @parent end |
Instance Method Details
#add_variable(sym, value) ⇒ Object
Add a variable with a pre-assigned value to this environment. sym
will be accessible as a variable for any lambda evaluated with #lazy_eval.
41 42 43 44 45 46 |
# File 'lib/bindata/lazy.rb', line 41 def add_variable(sym, value) if @variables.equal?(@@empty_hash) @variables = {} end @variables[sym.to_sym] = value end |
#lazy_eval(obj, overrides = nil) ⇒ Object
Evaluates obj
in the context of this environment. Evaluation recurses until it yields a value that is not a symbol or lambda. overrides
is an optional params
like hash
63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/bindata/lazy.rb', line 63 def lazy_eval(obj, overrides = nil) @overrides = overrides if overrides if obj.is_a? Symbol # treat :foo as lambda { foo } obj = __send__(obj) elsif obj.respond_to? :arity obj = instance_eval(&obj) end @overrides = @@empty_hash obj end |
#offset_of(sym) ⇒ Object
TODO: offset_of needs to be better thought out
49 50 51 52 53 |
# File 'lib/bindata/lazy.rb', line 49 def offset_of(sym) @parent.data_object.offset_of(sym) rescue nil end |
#parent_data_object ⇒ Object
Returns the data_object for the parent environment.
56 57 58 |
# File 'lib/bindata/lazy.rb', line 56 def parent_data_object @parent.nil? ? nil : @parent.data_object end |