Class: LtdTemplate::Code::Parameters

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

Instance Attribute Summary collapse

Attributes inherited from LtdTemplate::Code

#tpl_methods

Instance Method Summary collapse

Methods inherited from LtdTemplate::Code

#do_method, #do_set, #get_item, #has_item?, instance, #is_set?, #set_item, #set_value

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.



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

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.



11
12
13
# File 'lib/ltdtemplate/code/parameters.rb', line 11

def named
  @named
end

#positionalObject (readonly)

Returns the value of attribute positional.



11
12
13
# File 'lib/ltdtemplate/code/parameters.rb', line 11

def positional
  @positional
end

Instance Method Details

#get_value(opts = {}) ⇒ Object

Evaluate the code provided for the positional and named parameters and return a corresponding array t-value.



28
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
62
63
64
65
66
# File 'lib/ltdtemplate/code/parameters.rb', line 28

def get_value (opts = {})
  named = {}

  # Process the positional parameters (pos1, ..., posN)
  positional = @positional.map do |code|
 val = code.get_value
 if val.is_a? LtdTemplate::Code::Parameters
    if val.named.is_a? Hash
  # Named parameters from array/
  val.named.each { |key, val| named[key] = val }
    elsif val.named.is_a? Array
  # Named parameters from array%
  val.named.each_slice(2) do |key, val|
      named[key.get_value.to_native] = val if val
  end
    end
    val.positional # Positional parameters from array/
 else val
 end
  end.flatten

  # Process the named parameters (.. key1, val1, ..., keyN, valN)
  if @named
 if @named.is_a? Hash then named.merge! @named
 else
    @named.each_slice(2) do |key, val|
  named[key.get_value.to_native] = val.get_value if val
    end
 end
 scalar = false
  else
 scalar = positional.size == 1 and !named.empty?
  end

  array = @template.factory(:array).set_value(positional, named, scalar)

  # Parameters may get called if chained, e.g. array/.type
  opts[:method] ? array.get_value(opts) : array
end