Class: LtdTemplate::Value::Namespace

Inherits:
Array
  • Object
show all
Defined in:
lib/ltdtemplate/value/namespace.rb

Instance Attribute Summary collapse

Attributes inherited from Array

#sarah

Attributes inherited from Code

#tpl_methods

Instance Method Summary collapse

Methods inherited from Array

#do_join, #do_pop, #do_push, #do_shift, #do_unshift, #get_item, #has_item?, #named, #positional, #scalar?, #set_from_array, #set_from_hash, #set_item, #set_value, #to_boolean, #to_native, #to_text

Methods inherited from Code

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

Constructor Details

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

Returns a new instance of Namespace.



14
15
16
17
18
19
20
21
# File 'lib/ltdtemplate/value/namespace.rb', line 14

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

Instance Attribute Details

#methodObject (readonly)

Returns the value of attribute method.



11
12
13
# File 'lib/ltdtemplate/value/namespace.rb', line 11

def method
  @method
end

#parametersObject (readonly)

Returns the value of attribute parameters.



11
12
13
# File 'lib/ltdtemplate/value/namespace.rb', line 11

def parameters
  @parameters
end

#parentObject (readonly)

Returns the value of attribute parent.



11
12
13
# File 'lib/ltdtemplate/value/namespace.rb', line 11

def parent
  @parent
end

#rootObject (readonly)

Returns the value of attribute root.



11
12
13
# File 'lib/ltdtemplate/value/namespace.rb', line 11

def root
  @root
end

#targetObject

Returns the value of attribute target.



12
13
14
# File 'lib/ltdtemplate/value/namespace.rb', line 12

def target
  @target
end

Instance Method Details

#clearObject

Clear values except for permanent namespace attributes.



26
27
28
29
30
31
32
33
# File 'lib/ltdtemplate/value/namespace.rb', line 26

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

#do_add_names(opts) ⇒ Object

Add new namespace names with nil or specific values



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/ltdtemplate/value/namespace.rb', line 74

def do_add_names (opts)
  params = opts[:parameters]
  if params.positional.size
 tnil = @template.factory :nil
 params.positional.each { |item| set_item(item.to_native, tnil) }
  end
  if params.named.size
 params.named.each { |item, val| set_item(item, val) }
  end
  @template.factory :nil
end

#do_if(opts) ⇒ Object

Implement conditionals



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

def do_if (opts)
  if params = opts[:parameters]
 params.positional.each_slice(2) do |e1, e2|
    e1 = e1.get_value :method => 'call'

    #
    # Return the "else" value, e1, in the absence of
    # a condition/result value pair.
    #
    return e1 unless e2

    # Return the e2 result if e1 evaluates to true
    return e2.get_value(:method => 'call') if e1.to_boolean
 end
  end
  @template.factory :nil
end

#do_loop(opts) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/ltdtemplate/value/namespace.rb', line 105

def do_loop (opts)
  results = @template.factory(:array)
  if params = opts[:parameters] and params.positional.size > 1
 params = params.positional
 while params[0].get_value(:method => 'call').to_boolean
    @template.use :iterations
    results.sarah.push params[1].get_value(:method => 'call')
    break if params[2] and
!params[2].get_value(:method => 'call').to_boolean
 end
  end
  results
end

#do_use(opts) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/ltdtemplate/value/namespace.rb', line 119

def do_use (opts)
  tpl = @template
  if loader = tpl.options[:loader] and
    params = opts[:parameters] and params.positional.size > 0
 name = params.positional[0].get_value.to_text
 if !tpl.used[name]
    tpl.use :use
    tpl.used[name] = true
    result = loader.call(tpl, name)
    tpl.parse_template(tpl.get_tokens result).get_value if
result.kind_of? String
 end
  end
  tpl.factory :nil
end

#find_item(name) ⇒ Object

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



38
39
40
41
42
43
44
45
# File 'lib/ltdtemplate/value/namespace.rb', line 38

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

#get_value(opts = {}) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/ltdtemplate/value/namespace.rb', line 54

def get_value (opts = {})
  case opts[:method]
  when 'array', '*' # anonymous array
 opts[:parameters] ? opts[:parameters] :
   @template.factory(:parameters)
  when 'false' then @template.factory :boolean, false
  when 'if' then do_if opts
  when 'loop' then do_loop opts
  when 'method' then method_string
  when 'nil' then @template.factory :nil
  when 'target' then @target || @template.factory(:nil)
  when 'true' then @template.factory :boolean, true
  when 'use' then do_use opts
  when 'var' then do_add_names opts
  else super
  end
end

#method_stringObject

Template string-value for method



50
51
52
# File 'lib/ltdtemplate/value/namespace.rb', line 50

def method_string
  @method_string ||= @template.factory :string, @method
end