Module: PageTemplate::NamespaceItem

Included in:
Namespace, Parser, Template
Defined in:
lib/PageTemplate/parser.rb

Overview

A Namespace object consists of three things:

parent: A parent object to get a value from if the namespace does not 'know' a value.

object: An object is a hash or list that contains the values that this namespace will refer to. It may also be an object, in which case, its methods are treated as a hash, with respond_to? and send()

Cache: A cache ensures that a method on an object will only be called once.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#objectObject

Returns the value of attribute object


23
24
25
# File 'lib/PageTemplate/parser.rb', line 23

def object
  @object
end

#parentObject

Returns the value of attribute parent


23
24
25
# File 'lib/PageTemplate/parser.rb', line 23

def parent
  @parent
end

Instance Method Details

#clearObject Also known as: clear_cache

Clears the cache


26
27
28
# File 'lib/PageTemplate/parser.rb', line 26

def clear
  @values = Hash.new
end

#delete(key) ⇒ Object

Removes an entry from the Namespace


131
132
133
# File 'lib/PageTemplate/parser.rb', line 131

def delete(key)
  @values.delete(key)
end

#get(val, clean_rescue = true) ⇒ Object Also known as: []

Returns the first value found for key in the nested namespaces. Returns nil if no value is found.

Values are checked for in this order through each level of namespace:

  • level.key

  • level.key()

  • level

If a value is not found in any of the nested namespaces, get() searches for the key in the global namespace.

If val is a dot-separated list of words, then 'key' is the first part of it. The remainder of the words are sent to key (and further results) to premit accessing attributes of objects.


54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/PageTemplate/parser.rb', line 54

def get(val,clean_rescue=true)
  args = parser.args
  @values ||= {}
  @object ||= nil

  clean_rescue = !args['raise_on_error'] if clean_rescue

  val.gsub!(/[#{Regexp.escape(parser.method_separators)}]/,'.')

  key, rest = val.split(/\./, 2)

  value = case
  when @values.has_key?(key)
    @values[key]
  when @values.has_key?(key.to_sym)
    @values[key.to_sym]
  when !@object
    if @parent
      @parent.get(val)
    else
      nil
    end
  when @object.respond_to?(:has_key?)
    if @object.has_key?(key)
      @values[key] = @object[key]
    else
      return @parent.get(val) if @parent
      nil
    end
  when @object.respond_to?(sym = key.to_sym)
    @values[key] = @object.send(sym)
  when key == '__ITEM__'
    @object
  when @parent
    return @parent.get(val)
  else
    nil
  end

  if rest
    names = [key]
    rest.split(/\./).each do |i|
      names << i
      name = names.join('.')
      begin
        value = if @values.has_key?(name)
          @values[name]
        else
          @values[name] = value = case
          when @values.has_key?(name)
            @values[name]
          when value.respond_to?(:has_key?) # Hash
            value[i]
          when value.respond_to?(:[]) && i =~ /^\d+$/ # Array
            value[i.to_i]
          when value.respond_to?(i) # Just a method
            value.send(i)
          else
            nil
          end
        end
      rescue NoMethodError => er
        return nil
      end
    end
  end
  value
rescue Exception => e
  if clean_rescue
    "[ Error: #{e.message} ]"
  else
    raise e
  end
end

#parserObject

parser: most namespace objects won't be a parser, but pass this query up to the parser object.


137
138
139
140
141
142
143
# File 'lib/PageTemplate/parser.rb', line 137

def parser
  if @parent
    @parent.parser
  else
    Parser.recent_parser
  end
end

#set(key, value) ⇒ Object Also known as: []=

Saves a variable key as the string value in the global namespace.


33
34
35
36
# File 'lib/PageTemplate/parser.rb', line 33

def set(key, value)
  @values ||= {}
  @values[key] = value
end

#true?(flag) ⇒ Boolean

A convenience method to test whether a variable has a true value. Returns nil if flag is not found in the namespace, or flag has a nil value attached to it.

Returns:

  • (Boolean)

148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/PageTemplate/parser.rb', line 148

def true?(flag)
  args = parser.args
  val = get(flag,false)
  case
  when ! val
    false
  when args['empty_is_true']
    true
  when val.respond_to?(:empty?)
    ! val.empty?
  else
    true
  end
rescue Exception => er
  false
end