Class: Mullet::DefaultScope
- Inherits:
-
Object
- Object
- Mullet::DefaultScope
- Includes:
- Scope
- Defined in:
- lib/mullet/default_scope.rb
Overview
Default scope implementation which resolves variable names to values by reading from a data object. Given a variable name key, the following mechanisms are tried in this order:
* If the variable name is `.`, then return the object.
* If the object has a method named _key_ taking no parameters, then use
the value returned from calling the method.
* If the object has an instance variable named @_key_, then use the
variable value.
* If the object is a `Hash`, then use _key_ as the key to retrieve the
value from the hash.
If the value is a Proc, then use the value returned from calling it.
Constant Summary
Constants included from Scope
Instance Method Summary collapse
- #fetch_impl(name) ⇒ Object
-
#get_variable_value(name) ⇒ Object
Resolves variable name to value.
-
#initialize(data) ⇒ DefaultScope
constructor
A new instance of DefaultScope.
Constructor Details
#initialize(data) ⇒ DefaultScope
Returns a new instance of DefaultScope.
21 22 23 |
# File 'lib/mullet/default_scope.rb', line 21 def initialize(data) @data = data end |
Instance Method Details
#fetch_impl(name) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/mullet/default_scope.rb', line 25 def fetch_impl(name) if name == :'.' return @data end # Does the variable name match a method name in the object? if @data.respond_to?(name) method = @data.method(name) if method.arity == 0 return method.call() end end # Does the variable name match an instance variable name in the object? variable = :"@#{name}" if @data.instance_variable_defined?(variable) return @data.instance_variable_get(variable) end # Is the variable name a key in a Hash? if @data.respond_to?(:fetch) # If the key was not found, then try to find it as a String. return @data.fetch(name) {|k| @data.fetch(k.to_s(), NOT_FOUND) } end return NOT_FOUND end |
#get_variable_value(name) ⇒ Object
Resolves variable name to value.
58 59 60 61 62 63 64 |
# File 'lib/mullet/default_scope.rb', line 58 def get_variable_value(name) value = fetch_impl(name) if value.is_a?(Proc) value = value.call() end return value end |