Class: Puppet::Parser::AST::Function

Inherits:
Branch show all
Defined in:
lib/vendor/puppet/parser/ast/function.rb

Overview

An AST object to call a function.

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 Branch

#children, #pin

Attributes inherited from Puppet::Parser::AST

#file, #line, #parent, #scope

Attributes included from Util::Docs

#doc, #nodoc

Instance Method Summary collapse

Methods inherited from Branch

#each

Methods inherited from Puppet::Parser::AST

associates_doc, #evaluate_match, #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

#initialize(hash) ⇒ Function

Returns a new instance of Function.



36
37
38
39
40
41
42
43
# File 'lib/vendor/puppet/parser/ast/function.rb', line 36

def initialize(hash)
  @ftype = hash[:ftype] || :rvalue
  hash.delete(:ftype) if hash.include? :ftype

  super(hash)

  # Lastly, check the parity
end

Instance Attribute Details

#argumentsObject

Returns the value of attribute arguments.



9
10
11
# File 'lib/vendor/puppet/parser/ast/function.rb', line 9

def arguments
  @arguments
end

#nameObject

Returns the value of attribute name.



9
10
11
# File 'lib/vendor/puppet/parser/ast/function.rb', line 9

def name
  @name
end

Instance Method Details

#evaluate(scope) ⇒ Object

Raises:



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/vendor/puppet/parser/ast/function.rb', line 13

def evaluate(scope)
  # Make sure it's a defined function
  raise Puppet::ParseError, "Unknown function #{@name}" unless Puppet::Parser::Functions.function(@name)

  # Now check that it's been used correctly
  case @ftype
  when :rvalue
    raise Puppet::ParseError, "Function '#{@name}' does not return a value" unless Puppet::Parser::Functions.rvalue?(@name)
  when :statement
    if Puppet::Parser::Functions.rvalue?(@name)
      raise Puppet::ParseError,
        "Function '#{@name}' must be the value of a statement"
    end
  else
    raise Puppet::DevError, "Invalid function type #{@ftype.inspect}"
  end

  # We don't need to evaluate the name, because it's plaintext
  args = @arguments.safeevaluate(scope).map { |x| x == :undef ? '' : x }

  scope.send("function_#{@name}", args)
end

#to_sObject



45
46
47
48
# File 'lib/vendor/puppet/parser/ast/function.rb', line 45

def to_s
  args = arguments.is_a?(ASTArray) ? arguments.to_s.gsub(/\[(.*)\]/,'\1') : arguments
  "#{name}(#{args})"
end