Class: LtdTemplate::Code
- Inherits:
-
Object
- Object
- LtdTemplate::Code
- Defined in:
- lib/ltdtemplate/code.rb
Overview
LtdTemplate::Code - Base class for LtdTemplate code/value objects
Direct Known Subclasses
Call, Code_Block, Parameters, Subscript, Variable, Value::Array, Value::Boolean, Value::Code_Block, Value::Nil, Value::Number, Value::String
Defined Under Namespace
Classes: Call, Code_Block, Parameters, Subscript, Variable
Instance Attribute Summary collapse
-
#tpl_methods ⇒ Array<LtdTemplate::Value::Code_Block>
readonly
The code blocks bound to non-array values.
Class Method Summary collapse
-
.instance(*args) ⇒ Object
Return a new factory object instance (or a singleton in some subclasses, e.g. nil).
Instance Method Summary collapse
-
#do_method(opts, class_name = nil) ⇒ Object
Try to execute code-block methods bound to the object or object class.
-
#do_set(opts) ⇒ LtdTemplate::Value::Nil
Implement “=” (assignment).
-
#get_item(key) ⇒ LtdTemplate::Value::Code_Block
Return a non-array value’s method code block (if set).
-
#has_item?(key) ⇒ Boolean
Does a non-array value have a particular template method?.
-
#initialize(template) ⇒ Code
constructor
Initialize the object with a link to the associated template.
-
#is_set? ⇒ true
Is this value set? Always true except for unset variables.
-
#set_item(key, value) ⇒ Object
Set a non-array value’s method code block.
-
#set_value(value) ⇒ LtdTemplate::Code
No-op setting a value.
Constructor Details
#initialize(template) ⇒ Code
Initialize the object with a link to the associated template.
25 26 27 28 |
# File 'lib/ltdtemplate/code.rb', line 25 def initialize (template) @template = template @tpl_methods = {} end |
Instance Attribute Details
#tpl_methods ⇒ Array<LtdTemplate::Value::Code_Block> (readonly)
The code blocks bound to non-array values.
14 15 16 |
# File 'lib/ltdtemplate/code.rb', line 14 def tpl_methods @tpl_methods end |
Class Method Details
.instance(*args) ⇒ Object
Return a new factory object instance (or a singleton in some subclasses, e.g. nil).
20 |
# File 'lib/ltdtemplate/code.rb', line 20 def self.instance (*args); self.new(*args); end |
Instance Method Details
#do_method(opts, class_name = nil) ⇒ Object
Try to execute code-block methods bound to the object or object class. Returns the return value from the code block or t-nil.
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/ltdtemplate/code.rb', line 80 def do_method (opts, class_name = nil) method = nil if name = opts[:method] if @tpl_methods.has_key? name method = @tpl_methods[name] elsif class_name class_var = @template.factory :variable, class_name method = class_var.target.tpl_methods[name] if class_var.is_set? end end if method opts[:target] = self method.get_value opts else @template.factory :nil end end |
#do_set(opts) ⇒ LtdTemplate::Value::Nil
Implement “=” (assignment). Note that set_value is a no-op except for variables and array subscripts.
71 72 73 74 75 76 |
# File 'lib/ltdtemplate/code.rb', line 71 def do_set (opts) if params = opts[:parameters] set_value(params.scalar? ? params.positional[0] : params) end @template.factory :nil end |
#get_item(key) ⇒ LtdTemplate::Value::Code_Block
Return a non-array value’s method code block (if set).
40 41 42 43 |
# File 'lib/ltdtemplate/code.rb', line 40 def get_item (key) (@tpl_methods.has_key? key) ? @tpl_methods[key] : @template.factory(:nil) end |
#has_item?(key) ⇒ Boolean
Does a non-array value have a particular template method?
34 |
# File 'lib/ltdtemplate/code.rb', line 34 def has_item? (key); @tpl_methods.has_key? key; end |
#is_set? ⇒ true
Is this value set? Always true except for unset variables.
62 |
# File 'lib/ltdtemplate/code.rb', line 62 def is_set?; true; end |
#set_item(key, value) ⇒ Object
Set a non-array value’s method code block.
50 |
# File 'lib/ltdtemplate/code.rb', line 50 def set_item (key, value); @tpl_methods[key] = value; end |
#set_value(value) ⇒ LtdTemplate::Code
No-op setting a value. (Typically only variables can change their primary values.)
57 |
# File 'lib/ltdtemplate/code.rb', line 57 def set_value (value); self; end |