Class: LtdTemplate::Code::Subscript

Inherits:
LtdTemplate::Code show all
Defined in:
lib/ltdtemplate/code/subscript.rb

Instance Method Summary collapse

Methods inherited from LtdTemplate::Code

#inspect, #rubyversed

Constructor Details

#initialize(template, base, subscripts) ⇒ Subscript

Returns a new instance of Subscript.



13
14
15
16
# File 'lib/ltdtemplate/code/subscript.rb', line 13

def initialize (template, base, subscripts)
	super template
	@base, @subscripts = base, subscripts
end

Instance Method Details

#do_set(opts) ⇒ Object

Implement = and ?=



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/ltdtemplate/code/subscript.rb', line 69

def do_set (opts)
	subscripts = evaluate_subscripts true
	if subscripts.empty?
 # Treat expression[] as expression
 rubyversed(@base).evaluate opts
	elsif opts[:method] != '?=' ||
	  rubyversed(@base).evaluate.in_rubyverse(@template)[*subscripts,
	  {}].nil?
 # Assign if unconditional or unset
 params = opts[:parameters]
 params = params[0] if params.is_a? LtdTemplate::Univalue
 rubyversed(@base).evaluate.in_rubyverse(@template)[*subscripts,
   {}] = params
	end
	nil
end

#evaluate(opts = {}) ⇒ Object

Evaluate the target’s value.



21
22
23
24
25
26
# File 'lib/ltdtemplate/code/subscript.rb', line 21

def evaluate (opts = {})
	case opts[:method]
	when '=', '?=' then do_set opts # Support array assignment
	else rubyversed(target(true)).evaluate opts
	end
end

#evaluate_subscripts(meter = false) ⇒ Object

Return subscripts calculated from the supplied code blocks.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/ltdtemplate/code/subscript.rb', line 31

def evaluate_subscripts (meter = false)
	subscripts = []
	@subscripts.each do |code|
 subscript = rubyversed(code).evaluate
 case subscript
 when LtdTemplate::Value::Array_Splat
		if meter && (size = subscript.positional.size) > 1
  # RESOURCE subscripts: Total number of subscripts
  # RESOURCE subscript_depth: Deepest subscript depth
  @template.use :subscripts, size - 1
  @template.using :subscript_depth, size
		end
		subscripts.concat subscript.positional
 when Numeric, String then subscripts << subscript
 end
	end

	if meter
 @template.use :subscripts, @subscripts.size
 @template.using :subscript_depth, subscripts.size
	end

	subscripts
end

#target(meter = false) ⇒ Object

Return the target value, variable[sub1, …, subN]



59
60
61
62
63
64
# File 'lib/ltdtemplate/code/subscript.rb', line 59

def target (meter = false)
	subscripts = evaluate_subscripts meter
	if subscripts.empty? then rubyversed(@base).evaluate
	else rubyversed(@base).evaluate.in_rubyverse(@template)[*subscripts, {}]
	end
end