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

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

Overview

An AST object to call a function.

Instance Attribute Summary collapse

Attributes inherited from Branch

#children, #pin

Instance Method Summary collapse

Methods inherited from Branch

#each

Constructor Details

#initialize(hash) ⇒ Function

Returns a new instance of Function.



45
46
47
48
49
50
51
52
# File 'lib/puppet/parser/ast/function.rb', line 45

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



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

def arguments
  @arguments
end

#nameObject



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

def name
  @name
end

#pblockObject



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

def pblock
  @pblock
end

Instance Method Details

#evaluate(scope) ⇒ Object

Raises:



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/puppet/parser/ast/function.rb', line 11

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
    # It is harmless to produce an ignored rvalue, the alternative is to mark functions
    # as appropriate for both rvalue and statements
    # Keeping the old behavior when a pblock is not present. This since it is not known
    # if the lambda contains a statement or not (at least not without a costly search).
    # The purpose of the check is to protect a user for producing a meaningless rvalue where the
    # operation has no side effects.
    #
    if !pblock && 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 }

  # append a puppet lambda (unevaluated) if it is defined
  args << pblock if pblock

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

#to_sObject



54
55
56
57
# File 'lib/puppet/parser/ast/function.rb', line 54

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