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 141
def array_index_or_key(object, key)
  if object.is_a?(Array)
    raise Puppet::ParserError, "#{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 158
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) click to toggle source
# File lib/puppet/parser/ast/leaf.rb, line 148
def evaluate(scope)
  object = evaluate_container(scope)
  accesskey = evaluate_key(scope)

  raise Puppet::ParseError, "#{variable} is not an hash or array when accessing it with #{accesskey}" unless object.is_a?(Hash) or object.is_a?(Array)

  object[array_index_or_key(object, accesskey)] || :undef
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
  (container.is_a?(Hash) or container.is_a?(Array)) ? container : scope[container, {:file => file, :line => line}]
end
evaluate_key(scope) click to toggle source
# File lib/puppet/parser/ast/leaf.rb, line 137
def evaluate_key(scope)
  key.respond_to?(:evaluate) ? key.safeevaluate(scope) : key
end
to_s() click to toggle source
# File lib/puppet/parser/ast/leaf.rb, line 170
def to_s
  "\$#{variable.to_s}[#{key.to_s}]"
end