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
- #mutation_deprecation ⇒ 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 175 176 |
# 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 mutation_deprecation() # 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 |
#mutation_deprecation ⇒ Object
182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/puppet/parser/ast/leaf.rb', line 182 def mutation_deprecation deprecation_location_text = if file && line " at #{file}:#{line}" elsif file " in file #{file}" elsif line " at #{line}" end Puppet.warning(["The use of mutating operations on Array/Hash is deprecated#{deprecation_location_text}.", " See http://links.puppetlabs.com/puppet-mutation-deprecation"].join('')) end |
#to_s ⇒ Object
178 179 180 |
# File 'lib/puppet/parser/ast/leaf.rb', line 178 def to_s "\$#{variable.to_s}[#{key.to_s}]" end |