Class: LtdTemplate::Value::Namespace

Inherits:
Sarah
  • Object
show all
Includes:
LtdTemplate::Value, XKeys::Hash
Defined in:
lib/ltdtemplate/value/namespace.rb

Instance Attribute Summary collapse

Attributes included from LtdTemplate::Value

#runtime_methods

Instance Method Summary collapse

Methods included from LtdTemplate::Value

#do_methods, #do_run_method, included, #inspect, #rubyversed, #tpl_boolean

Constructor Details

#initialize(template, tpl_method, parameters, parent = nil) ⇒ Namespace

Returns a new instance of Namespace.



19
20
21
22
23
24
25
26
# File 'lib/ltdtemplate/value/namespace.rb', line 19

def initialize (template, tpl_method, parameters, parent = nil)
	super template
	@tpl_method, @parameters = tpl_method, parameters
	@root = parent ? parent.root : self
	@parent = parent
	@target = nil
	clear()
end

Instance Attribute Details

#parametersObject (readonly)

Returns the value of attribute parameters.



16
17
18
# File 'lib/ltdtemplate/value/namespace.rb', line 16

def parameters
  @parameters
end

#parentObject (readonly)

Returns the value of attribute parent.



16
17
18
# File 'lib/ltdtemplate/value/namespace.rb', line 16

def parent
  @parent
end

#rootObject (readonly)

Returns the value of attribute root.



16
17
18
# File 'lib/ltdtemplate/value/namespace.rb', line 16

def root
  @root
end

#targetObject

Returns the value of attribute target.



17
18
19
# File 'lib/ltdtemplate/value/namespace.rb', line 17

def target
  @target
end

#templateObject (readonly)

Returns the value of attribute template.



16
17
18
# File 'lib/ltdtemplate/value/namespace.rb', line 16

def template
  @template
end

#tpl_methodObject (readonly)

Returns the value of attribute tpl_method.



16
17
18
# File 'lib/ltdtemplate/value/namespace.rb', line 16

def tpl_method
  @tpl_method
end

Instance Method Details

#clearObject

Clear values except for permanent namespace attributes.



29
30
31
32
33
34
35
36
# File 'lib/ltdtemplate/value/namespace.rb', line 29

def clear
	super
	self['_'] = @parameters
	self['@'] = @root
	self['^'] = @parent if @parent
	self['$'] = self
	self
end

#do_add_names(opts) ⇒ Object

Add new namespace names with nil or specific values. $.var(name1, …, nameN .. key1, val1, …, keyN, valN)



79
80
81
82
83
84
85
# File 'lib/ltdtemplate/value/namespace.rb', line 79

def do_add_names (opts)
	if params = opts[:parameters]
 params.each(:seq) { |idx, val| self[val] = nil }
 params.each(:nsq) { |key, val| self[key] = val }
	end
	nil
end

#do_if(opts) ⇒ Object

Implement conditionals $.if(test1, result1, …, testN, resultN, else_value)



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/ltdtemplate/value/namespace.rb', line 89

def do_if (opts)
	if params = opts[:parameters]
 params.values(:seq).each_slice(2) do |pair|
		e1 = rubyversed(pair[0]).evaluate :method => 'call'

		#
		# Return the "else" value, e1, in the absence of
		# a condition/result value pair.
		#
		return e1 if pair.size == 1

		# Return the e2 result if e1 evaluates to true
		if rubyversed(e1).tpl_boolean
  return rubyversed(pair[1]).evaluate :method => 'call'
		end
 end
	end
	nil
end

#do_loop(opts) ⇒ Object

Implement loops $.loop(pre_test, body) $.loop(pre_test, body, post_test)



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/ltdtemplate/value/namespace.rb', line 112

def do_loop (opts)
	results = @template.factory :array
	if (params = opts[:parameters]) && params.size(:seq) > 1
 while rubyversed(params[0]).evaluate(:method => 'call').
   in_rubyverse(@template).tpl_boolean
		# RESOURCE iterations: Total loop iterations
		@template.use :iterations
		results.push rubyversed(params[1]).evaluate :method => 'call'
		break if params.size(:seq) > 2 && !rubyversed(params[2]).
evaluate(:method => 'call').in_rubyverse(@template).
tpl_boolean
 end
	end
	results
end

#do_use(opts) ⇒ Object

Load external resources



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/ltdtemplate/value/namespace.rb', line 129

def do_use (opts)
	tpl = @template
	if (loader = tpl.options[:loader]) && (params = opts[:parameters]) &&
	  params.size(:seq) > 0
 name = params[0]
 if !tpl.used[name]
		# RESOURCE use: Total $.use invocations
		tpl.use :use
		tpl.used[name] = true
		result = loader.call(tpl, name)
		tpl.parse_template(tpl.get_tokens result).evaluate if
result.kind_of? String
 end
	end
	nil
end

#evaluate(opts = {}) ⇒ Object

Evaluate supported methods on namespaces.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/ltdtemplate/value/namespace.rb', line 39

def evaluate (opts = {})
	case opts[:method]
	when nil, 'call' then self
	when 'array', '*' # anonymous array
 opts[:parameters] ? opts[:parameters] : @template.factory(:array)
	when 'class' then 'Namespace'
	when 'false' then false
	when 'if' then do_if opts
	when 'loop' then do_loop opts
	when 'method' then @tpl_method
	when 'nil' then nil
	when 'target' then @target
	when 'true' then true
	when 'type' then 'namespace'
	when 'use' then do_use opts
	when 'var' then do_add_names opts
	else super opts
	end
end

#find_item(name) ⇒ Object

Search for the specified item in the current namespace or above.



60
61
62
63
64
65
66
67
# File 'lib/ltdtemplate/value/namespace.rb', line 60

def find_item (name)
	namespace = self
	while namespace
 break if namespace.has_key? name
 namespace = namespace.parent
	end
	namespace
end

#tpl_textObject

Namespaces do not generate template output.



70
# File 'lib/ltdtemplate/value/namespace.rb', line 70

def tpl_text; ''; end

#xkeys_new(*args) ⇒ Object

Auto-vivicate arrays in namespaces.



73
# File 'lib/ltdtemplate/value/namespace.rb', line 73

def xkeys_new (*args); @template.factory :array; end