@note WARNING: This style of creating functions is not public. It is a system
under development that will be used for creating "system" functions.
Injection and Weaving of parameters
It is possible to inject and weave parameters into a call. These extra parameters are not part of the parameters passed from the Puppet logic, and they can not be overridden by parameters given as arguments in the call. They are invisible to the Puppet Language.
@example using injected parameters
Puppet::Functions.create_function('test') do dispatch :test do param 'Scalar', 'a' param 'Scalar', 'b' injected_param 'String', 'larger', 'message_larger' injected_param 'String', 'smaller', 'message_smaller' end def test(a, b, larger, smaller) a > b ? larger : smaller end end
The function in the example above is called like this:
test(10, 20)
Using injected value as default
Default value assignment is handled by using the regular Ruby mechanism (a value is assigned to the variable). The dispatch simply indicates that the value is optional. If the default value should be injected, it can be handled different ways depending on what is desired:
by calling the accessor method for an injected Function class attribute. This is suitable if the value is constant across all instantiations of the function, and across all calls.
by injecting a parameter into the call to the left of the parameter, and then assigning that as the default value.
One of the above forms, but using an injected producer instead of a directly injected value.
@example method with injected default values
Puppet::Functions.create_function('test') do dispatch :test do injected_param String, 'b_default', 'b_default_value_key' param 'Scalar', 'a' param 'Scalar', 'b' end def test(b_default, a, b = b_default) # ... end end
@api private
TODO: is param name really needed? Perhaps for error messages? (it is unused now)
@api private
# File lib/puppet/functions.rb, line 615 def injected_param(type, name, injection_name = '') @injections << [type, name, injection_name] # mark what should be picked for this position when dispatching @weaving << [@injections.size() -1] end
TODO: is param name really needed? Perhaps for error messages? (it is unused now)
@api private
# File lib/puppet/functions.rb, line 624 def injected_producer_param(type, name, injection_name = '') @injections << [type, name, injection_name, :producer] # mark what should be picked for this position when dispatching @weaving << [@injections.size()-1] end
# File lib/puppet/functions.rb, line 607 def scope_param() @injections << [:scope, 'scope', '', :dispatcher_internal] # mark what should be picked for this position when dispatching @weaving << [@injections.size()-1] end