Class: LtdTemplate::Code::Parameters

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from LtdTemplate::Code

#inspect, #rubyversed

Constructor Details

#initialize(template, positional = [], named = nil) ⇒ Parameters

Create a parameter list builder with code to generate positional values and possibly code to generate named values.



18
19
20
21
22
23
# File 'lib/ltdtemplate/code/parameters.rb', line 18

def initialize (template, positional = [], named = nil)
	super template

	# Save the code blocks for positional and named parameters.
	@positional, @named = positional, named
end

Instance Attribute Details

#namedObject (readonly)

Returns the value of attribute named.



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

def named
  @named
end

#positionalObject (readonly)

Returns the value of attribute positional.



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

def positional
  @positional
end

Instance Method Details

#evaluate(opts = {}) ⇒ Sarah

Evaluate the code provided for the positional and named parameters and return a corresponding Sarah.

Returns:

  • (Sarah)


29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/ltdtemplate/code/parameters.rb', line 29

def evaluate (opts = {})
	params = @template.factory :array

	# Process the positional parameters (pos1, ..., posN)
	@positional.each do |code|
 value = rubyversed(code).evaluate
 if value.is_a? LtdTemplate::Value::Array_Splat
		# Merge parameters from array/ or array%
		# RESOURCE array_growth: Increases in array sizes
		@template.use :array_growth, value.positional.size
		params.concat value.positional
		if value.named
  @template.use :array_growth, value.named.size / 2
  params.set_pairs *value.named
		end
 else params.push value
 end
	end

	# Process the named parameters (.. key1, val1, ..., keyN, valN)
	if @named
 @named.each_slice(2) do |k_code, v_code|
		params[rubyversed(k_code).evaluate] =
rubyversed(v_code).evaluate if v_code
 end
	end

	# Is this a candidate for scalar assignment?
	params.extend LtdTemplate::Univalue if
	  !@named && params.size(:seq) == 1

	params
end