@note WARNING: This style of creating functions is not public. It is a system
under development that will be used for creating "system" functions.
This is a private, internal, system for creating functions. It supports everything that the public function definition system supports as well as a few extra features.
Injection Support
The Function API supports injection of data and services. It is possible to make injection that takes effect when the function is loaded (for services and runtime configuration that does not change depending on how/from where in what context the function is called. It is also possible to inject and weave argument values into a call.
Injection of attributes
Injection of attributes is performed by one of the methods `::attr_injected`, and `::attr_injected_producer`. The injected attributes are available via accessor method calls.
@example using injected attributes
Puppet::Functions.create_function('test') do attr_injected String, :larger, 'message_larger' attr_injected String, :smaller, 'message_smaller' def test(a, b) a > b ? larger() : smaller() end end
@api private
Allows the implementation of a function to call other functions by name and pass the caller scope. The callable functions are those visible to the same loader that loaded this function (the calling function).
@param scope [Puppet::Parser::Scope] The caller scope @param function_name [String] The name of the function @param *args [Object] splat of arguments @return [Object] The result returned by the called function
@api public
# File lib/puppet/functions.rb, line 546 def call_function_with_scope(scope, function_name, *args) internal_call_function(scope, function_name, args) end
Defines class level injected attribute with reader method
@api private
# File lib/puppet/functions.rb, line 511 def self.attr_injected(type, attribute_name, injection_name = nil) define_method(attribute_name) do ivar = :"@#{attribute_name.to_s}" unless instance_variable_defined?(ivar) injector = Puppet.lookup(:injector) instance_variable_set(ivar, injector.lookup(closure_scope, type, injection_name)) end instance_variable_get(ivar) end end
Defines class level injected producer attribute with reader method
@api private
# File lib/puppet/functions.rb, line 525 def self.attr_injected_producer(type, attribute_name, injection_name = nil) define_method(attribute_name) do ivar = :"@#{attribute_name.to_s}" unless instance_variable_defined?(ivar) injector = Puppet.lookup(:injector) instance_variable_set(ivar, injector.lookup_producer(closure_scope, type, injection_name)) end instance_variable_get(ivar) end end
@api private
# File lib/puppet/functions.rb, line 502 def self.builder @type_parser ||= Puppet::Pops::Types::TypeParser.new @all_callables ||= Puppet::Pops::Types::TypeFactory.all_callables InternalDispatchBuilder.new(dispatcher, @type_parser, @all_callables) end