Class: Puppet::Pops::Functions::Function

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet/pops/functions/function.rb

Overview

Note:

WARNING: This new function API is still under development and may change at any time

A function in the puppet evaluator.

Functions are normally defined by another system, which produces subclasses of this class as well as constructing delegations to call the appropriate methods.

This class should rarely be used directly. Instead functions should be constructed using Functions.create_function.

Direct Known Subclasses

Functions::Function

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(closure_scope, loader) ⇒ Function

Returns a new instance of Function.



22
23
24
25
# File 'lib/puppet/pops/functions/function.rb', line 22

def initialize(closure_scope, loader)
  @closure_scope = closure_scope
  @loader = loader
end

Instance Attribute Details

#closure_scopeObject (readonly)

The scope where the function was defined



15
16
17
# File 'lib/puppet/pops/functions/function.rb', line 15

def closure_scope
  @closure_scope
end

#loaderObject (readonly)

The loader that loaded this function. Should be used if function wants to load other things.



20
21
22
# File 'lib/puppet/pops/functions/function.rb', line 20

def loader
  @loader
end

Class Method Details

.dispatcherObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The dispatcher for the function



67
68
69
# File 'lib/puppet/pops/functions/function.rb', line 67

def self.dispatcher
  @dispatcher ||= Puppet::Pops::Functions::Dispatcher.new
end

.signaturesObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Produces information about parameters in a way that is compatible with Closure



74
75
76
# File 'lib/puppet/pops/functions/function.rb', line 74

def self.signatures
  @dispatcher.signatures
end

Instance Method Details

#call(scope, *args) ⇒ Object

Invokes the function via the dispatching logic that performs type check and weaving. A specialized function may override this method to do its own dispatching and checking of the raw arguments. A specialized implementation can rearrange arguments, add or remove arguments and then delegate to the dispatching logic by calling:

System functions that must have access to the calling scope can use this technique. Functions in general should not need the calling scope. (The closure scope; what is visible where the function is defined) is available via the method ‘closure_scope`).

Examples:

Delegating to the dispatcher

def call(scope, *args)
  manipulated_args = args + ['easter_egg']
  self.class.dispatcher.dispatch(self, scope, manipulated_args)
end


43
44
45
# File 'lib/puppet/pops/functions/function.rb', line 43

def call(scope, *args)
  self.class.dispatcher.dispatch(self, scope, args)
end

#call_function(function_name, *args) ⇒ Object

Allows the implementation of a function to call other functions by name. The callable functions are those visible to the same loader that loaded this function (the calling function).

Raises:

  • (ArgumentError)


51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/puppet/pops/functions/function.rb', line 51

def call_function(function_name, *args)
  if the_loader = loader
    func = the_loader.load(:function, function_name)
    if func
      return func.call(closure_scope, *args)
    end
  end
  # Raise a generic error to allow upper layers to fill in the details about where in a puppet manifest this
  # error originates. (Such information is not available here).
  #
  raise ArgumentError, "Function #{self.class.name}(): cannot call function '#{function_name}' - not found"
end