class Puppet::Parser::AST::HashOrArrayAccess

Attributes

key[RW]
variable[RW]

Public Instance Methods

array_index_or_key(object, key) click to toggle source
# 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) click to toggle source

Assign value to this hashkey or array index

# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/puppet/parser/ast/leaf.rb, line 143
def evaluate_key(scope)
  key.respond_to?(:evaluate) ? key.safeevaluate(scope) : key
end
mutation_deprecation() click to toggle source
# 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() click to toggle source
# File lib/puppet/parser/ast/leaf.rb, line 178
def to_s
  "\$#{variable.to_s}[#{key.to_s}]"
end