Class: LtdTemplate::Code::Variable

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

Instance Attribute Summary

Attributes inherited from LtdTemplate::Code

#tpl_methods

Instance Method Summary collapse

Methods inherited from LtdTemplate::Code

#do_method, #do_set, instance

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
25
# File 'lib/ltdtemplate/code/variable.rb', line 11

def initialize (template, name)
	super template
	case name[0]
	when '@', '^'
 # @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

#get_item(key) ⇒ Object



49
# File 'lib/ltdtemplate/code/variable.rb', line 49

def get_item (key); target.get_item key; end

#get_value(opts = {}) ⇒ Object



66
67
68
69
70
71
72
73
74
75
# File 'lib/ltdtemplate/code/variable.rb', line 66

def get_value (opts = {})
	case opts[:method]
	when '=' then do_set opts	# see LtdTemplate::Code
	when '?='
 if is_set? then @template.factory :nil
 else do_set opts
 end
	else target.get_value opts
	end
end

#has_item?(key) ⇒ Boolean

Implement the subscripting interface.

Returns:

  • (Boolean)


48
# File 'lib/ltdtemplate/code/variable.rb', line 48

def has_item? (key); target.has_item? key; end

#is_set?Boolean

Is this variable set? Among other possible uses, this is needed for determining when to auto-vivicate array subscripts.

Returns:

  • (Boolean)


64
# File 'lib/ltdtemplate/code/variable.rb', line 64

def is_set?; namespace.has_item? @name; end

#namespaceObject

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



31
32
33
34
35
36
37
38
# File 'lib/ltdtemplate/code/variable.rb', line 31

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

#set_item(key, value) ⇒ Object



50
# File 'lib/ltdtemplate/code/variable.rb', line 50

def set_item (key, value); target.set_item key, value; end

#set_value(value) ⇒ Object

Try to set the value.



55
56
57
# File 'lib/ltdtemplate/code/variable.rb', line 55

def set_value (value)
	namespace.set_item(@name, value)
end

#targetObject

Return the namespace item for this variable.



43
# File 'lib/ltdtemplate/code/variable.rb', line 43

def target; namespace.get_item(@name); end