Class: Puppet::Pops::Functions::Function
- Defined in:
- lib/puppet/pops/functions/function.rb
Overview
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
Instance Attribute Summary collapse
-
#loader ⇒ Object
readonly
The loader that loaded this function.
Class Method Summary collapse
-
.dispatcher ⇒ Object
private
The dispatcher for the function.
-
.signatures ⇒ Object
private
Produces information about parameters in a way that is compatible with Closure.
Instance Method Summary collapse
-
#call(scope, *args, &block) ⇒ Object
Invokes the function via the dispatching logic that performs type check and weaving.
-
#call_function(function_name, *args, &block) ⇒ Object
Allows the implementation of a function to call other functions by name.
-
#closure_scope ⇒ Object
The scope where the function was defined.
-
#initialize(closure_scope, loader) ⇒ Function
constructor
A new instance of Function.
Constructor Details
#initialize(closure_scope, loader) ⇒ Function
Returns a new instance of Function.
21 22 23 24 |
# File 'lib/puppet/pops/functions/function.rb', line 21 def initialize(closure_scope, loader) @closure_scope = closure_scope @loader = loader end |
Instance Attribute Details
#loader ⇒ Object (readonly)
The loader that loaded this function. Should be used if function wants to load other things.
19 20 21 |
# File 'lib/puppet/pops/functions/function.rb', line 19 def loader @loader end |
Class Method Details
.dispatcher ⇒ Object
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
83 84 85 |
# File 'lib/puppet/pops/functions/function.rb', line 83 def self.dispatcher @dispatcher ||= Puppet::Pops::Functions::Dispatcher.new end |
.signatures ⇒ Object
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
90 91 92 |
# File 'lib/puppet/pops/functions/function.rb', line 90 def self.signatures @dispatcher.signatures end |
Instance Method Details
#call(scope, *args, &block) ⇒ 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`).
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/puppet/pops/functions/function.rb', line 42 def call(scope, *args, &block) result = catch(:return) do return self.class.dispatcher.dispatch(self, scope, args, &block) end result.value rescue Puppet::Pops::Evaluator::Next => jumper begin throw :next, jumper.value rescue Puppet::Parser::Scope::UNCAUGHT_THROW_EXCEPTION raise Puppet::ParseError.new("next() from context where this is illegal", jumper.file, jumper.line) end rescue Puppet::Pops::Evaluator::Return => jumper begin throw :return, jumper rescue Puppet::Parser::Scope::UNCAUGHT_THROW_EXCEPTION raise Puppet::ParseError.new("return() from context where this is illegal", jumper.file, jumper.line) end end |
#call_function(function_name, *args, &block) ⇒ 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). The referenced function is called with the calling functions closure scope as the caller’s scope.
70 71 72 |
# File 'lib/puppet/pops/functions/function.rb', line 70 def call_function(function_name, *args, &block) internal_call_function(closure_scope, function_name, args, &block) end |
#closure_scope ⇒ Object
The scope where the function was defined
75 76 77 78 |
# File 'lib/puppet/pops/functions/function.rb', line 75 def closure_scope # If closure scope is explicitly set to not nil, if there is a global scope, otherwise an empty hash @closure_scope || Puppet.lookup(:global_scope) { {} } end |