Class: Puppet::Parser::AST::HashOrArrayAccess
- Defined in:
- lib/puppet/parser/ast/leaf.rb
Instance Attribute Summary collapse
Attributes inherited from Leaf
Instance Method Summary collapse
- #array_index_or_key(object, key) ⇒ Object
-
#assign(scope, value) ⇒ Object
Assign value to this hashkey or array index.
- #evaluate(scope) ⇒ Object
- #evaluate_container(scope) ⇒ Object
- #evaluate_key(scope) ⇒ Object
- #to_s ⇒ Object
Methods inherited from Leaf
Instance Attribute Details
#variable ⇒ Object
130 131 132 |
# File 'lib/puppet/parser/ast/leaf.rb', line 130 def variable @variable end |
Instance Method Details
#array_index_or_key(object, key) ⇒ Object
147 148 149 150 151 152 |
# File 'lib/puppet/parser/ast/leaf.rb', line 147 def array_index_or_key(object, key) if object.is_a?(Array) raise Puppet::ParseError, "#{key} is not an integer, but is used as an index of an array" unless key = Puppet::Parser::Scope.number?(key) end key end |
#assign(scope, value) ⇒ Object
Assign value to this hashkey or array index
164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/puppet/parser/ast/leaf.rb', line 164 def assign(scope, value) object = evaluate_container(scope) accesskey = evaluate_key(scope) if object.is_a?(Hash) and object.include?(accesskey) raise Puppet::ParseError, "Assigning to the hash '#{variable}' with an existing key '#{accesskey}' is forbidden" end # assign to hash or array object[array_index_or_key(object, accesskey)] = value end |
#evaluate(scope) ⇒ Object
154 155 156 157 158 159 160 161 |
# File 'lib/puppet/parser/ast/leaf.rb', line 154 def evaluate(scope) object = evaluate_container(scope) accesskey = evaluate_key(scope) raise Puppet::ParseError, "#{variable} is not a hash or array when accessing it with #{accesskey}" unless object.is_a?(Hash) or object.is_a?(Array) result = object[array_index_or_key(object, accesskey)] result.nil? ? :undef : result end |
#evaluate_container(scope) ⇒ Object
132 133 134 135 136 137 138 139 140 141 |
# File 'lib/puppet/parser/ast/leaf.rb', line 132 def evaluate_container(scope) container = variable.respond_to?(:evaluate) ? variable.safeevaluate(scope) : variable if container.is_a?(Hash) || container.is_a?(Array) container elsif container.is_a?(::String) scope[container, {:file => file, :line => line}] else raise Puppet::ParseError, "#{variable} is #{container.inspect}, not a hash or array" end end |
#evaluate_key(scope) ⇒ Object
143 144 145 |
# File 'lib/puppet/parser/ast/leaf.rb', line 143 def evaluate_key(scope) key.respond_to?(:evaluate) ? key.safeevaluate(scope) : key end |
#to_s ⇒ Object
176 177 178 |
# File 'lib/puppet/parser/ast/leaf.rb', line 176 def to_s "\$#{variable.to_s}[#{key.to_s}]" end |