Class: LtdTemplate::Code::Subscript
- Inherits:
-
LtdTemplate::Code
- Object
- LtdTemplate::Code
- LtdTemplate::Code::Subscript
- Defined in:
- lib/ltdtemplate/code/subscript.rb
Instance Attribute Summary
Attributes inherited from LtdTemplate::Code
Instance Method Summary collapse
- #get_item(key) ⇒ Object
- #get_value(opts = {}) ⇒ Object
-
#has_item?(key) ⇒ Boolean
Implement the subscript interface for the target.
-
#initialize(template, base, subscripts) ⇒ Subscript
constructor
A new instance of Subscript.
-
#native_subs(usage = false) ⇒ Object
Return native subscripts calculated from the supplied code blocks.
- #set_item(key, value) ⇒ Object
-
#set_value(value) ⇒ Object
Set the target’s value.
-
#target(usage = false) ⇒ Object
Return the target value, variable[sub1, …, subN].
Methods inherited from LtdTemplate::Code
#do_method, #do_set, instance, #is_set?
Constructor Details
#initialize(template, base, subscripts) ⇒ Subscript
Returns a new instance of Subscript.
12 13 14 15 |
# File 'lib/ltdtemplate/code/subscript.rb', line 12 def initialize (template, base, subscripts) super template @base, @subscripts = base, subscripts end |
Instance Method Details
#get_item(key) ⇒ Object
44 |
# File 'lib/ltdtemplate/code/subscript.rb', line 44 def get_item (key); target.get_item key; end |
#get_value(opts = {}) ⇒ Object
74 75 76 77 78 79 |
# File 'lib/ltdtemplate/code/subscript.rb', line 74 def get_value (opts = {}) case opts[:method] when '=' then do_set opts # see LtdTemplate::Code else target.get_value opts end end |
#has_item?(key) ⇒ Boolean
Implement the subscript interface for the target.
43 |
# File 'lib/ltdtemplate/code/subscript.rb', line 43 def has_item? (key); target.has_item? key; end |
#native_subs(usage = false) ⇒ Object
Return native subscripts calculated from the supplied code blocks.
20 21 22 23 24 25 26 27 28 29 |
# File 'lib/ltdtemplate/code/subscript.rb', line 20 def native_subs (usage = false) nsubs = @subscripts ? @subscripts.map { |sub| sub.get_value.to_native }.flatten : [] num_subs = nsubs.size if usage and num_subs > 0 @template.using :subscript_depth, num_subs @template.use :subscripts, num_subs end nsubs end |
#set_item(key, value) ⇒ Object
45 |
# File 'lib/ltdtemplate/code/subscript.rb', line 45 def set_item (key, value); target(true).set_item key, value; end |
#set_value(value) ⇒ Object
Set the target’s value.
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/ltdtemplate/code/subscript.rb', line 50 def set_value (value) subs = native_subs true if subs.size == 0 # If there are no subscripts, just use the base. @base.set_value value else # # Traverse all but the last subscript, trying to autovivicate # new arrays as we go. This will silently fail if there is an # existing non-array value somewhere. # current = @base current.set_value @template.factory :array unless current.is_set? subs[0..-2].each do |sub| if !current.has_item? sub current.set_item sub, @template.factory(:array) end current = current.get_item sub end current.set_item subs[-1], value end self end |
#target(usage = false) ⇒ Object
Return the target value, variable[sub1, …, subN]
34 35 36 37 38 |
# File 'lib/ltdtemplate/code/subscript.rb', line 34 def target (usage = false) current = @base.get_value native_subs(usage).each { |subs| current = current.get_item subs } current end |