Class: LtdTemplate::Code::Variable

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

Instance Method Summary collapse

Methods inherited from LtdTemplate::Code

#inspect, #rubyversed

Constructor Details

#initialize(template, name) ⇒ Variable

Returns a new instance of Variable.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/ltdtemplate/code/variable.rb', line 11

def initialize (template, name)
	super template
	if name.size > 1 && (name[0] == '@' || name[0] == '^')
 # @var is in the root namespace
 # ^var is in the parent namespace
 @modifier = name[0]
 @name = name[1..-1]
 @name = @name.to_i if @name =~ /^(?:0|[1-9]\d*)$/
	else
 # Standard bottom-to-top namespace search variable
 @modifier = nil
 @name = name
	end
end

Instance Method Details

#evaluate(opts = {}) ⇒ Object

Evaluate



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/ltdtemplate/code/variable.rb', line 29

def evaluate (opts = {})
	case opts[:method]
	when '=', '?='
 if opts[:method] != '?=' || self.namespace[@name].nil?
		params = opts[:parameters]
		params = params[0] if params.is_a? LtdTemplate::Univalue
		self.namespace[@name] = params
 end
 nil
	else
 self.namespace[@name] = "" if
   opts[:method] == 'methods' && self.namespace[@name].nil?
 rubyversed(self.namespace[@name]).evaluate opts
	end
end

#namespaceObject

Return the namespace in which this variable currently resides (or would reside, if it doesn’t currently exist).



49
50
51
52
53
54
55
56
# File 'lib/ltdtemplate/code/variable.rb', line 49

def namespace
	case @modifier
	when '@' then base = @template.namespace.root
	when '^' then base = @template.namespace.parent || @template.namespace
	else base = @template.namespace
	end
	base.find_item(@name) || base
end