Class: Puppet::Parser::AST::HashOrArrayAccess

Inherits:
Leaf show all
Defined in:
lib/vendor/puppet/parser/ast/leaf.rb

Constant Summary

Constants inherited from Puppet::Parser::AST

AST

Constants included from Util::Docs

Util::Docs::HEADER_LEVELS

Instance Attribute Summary collapse

Attributes inherited from Leaf

#type, #value

Attributes inherited from Puppet::Parser::AST

#file, #line, #parent, #scope

Attributes included from Util::Docs

#doc, #nodoc

Instance Method Summary collapse

Methods inherited from Leaf

#match

Methods inherited from Puppet::Parser::AST

associates_doc, #evaluate_match, #initialize, #inspect, #parsefail, #parsewrap, #safeevaluate, settor?, #use_docs

Methods included from Util::Docs

#desc, #dochook, #doctable, #markdown_definitionlist, #markdown_header, #nodoc?, #pad, scrub

Methods included from Util::MethodHelper

#requiredopts, #set_options, #symbolize_options

Methods included from Util::Errors

#adderrorcontext, #devfail, #error_context, #exceptwrap, #fail

Constructor Details

This class inherits a constructor from Puppet::Parser::AST

Instance Attribute Details

#keyObject

Returns the value of attribute key.



140
141
142
# File 'lib/vendor/puppet/parser/ast/leaf.rb', line 140

def key
  @key
end

#variableObject

Returns the value of attribute variable.



140
141
142
# File 'lib/vendor/puppet/parser/ast/leaf.rb', line 140

def variable
  @variable
end

Instance Method Details

#array_index_or_key(object, key) ⇒ Object



151
152
153
154
155
156
# File 'lib/vendor/puppet/parser/ast/leaf.rb', line 151

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) ⇒ Object

Assign value to this hashkey or array index



168
169
170
171
172
173
174
175
176
177
178
# File 'lib/vendor/puppet/parser/ast/leaf.rb', line 168

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

Raises:



158
159
160
161
162
163
164
165
# File 'lib/vendor/puppet/parser/ast/leaf.rb', line 158

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) ⇒ Object



142
143
144
145
# File 'lib/vendor/puppet/parser/ast/leaf.rb', line 142

def evaluate_container(scope)
  container = variable.respond_to?(:evaluate) ? variable.safeevaluate(scope) : variable
  (container.is_a?(Hash) or container.is_a?(Array)) ? container : scope.lookupvar(container, :file => file, :line => line)
end

#evaluate_key(scope) ⇒ Object



147
148
149
# File 'lib/vendor/puppet/parser/ast/leaf.rb', line 147

def evaluate_key(scope)
  key.respond_to?(:evaluate) ? key.safeevaluate(scope) : key
end

#to_sObject



180
181
182
# File 'lib/vendor/puppet/parser/ast/leaf.rb', line 180

def to_s
  "\$#{variable.to_s}[#{key.to_s}]"
end