Class: Puppet::Functions::InternalDispatchBuilder Private

Inherits:
DispatcherBuilder show all
Defined in:
lib/puppet/functions.rb

Overview

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

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.

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.

Examples:

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

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

Instance Attribute Summary

Attributes inherited from DispatcherBuilder

#loader

Instance Method Summary collapse

Methods inherited from DispatcherBuilder

#block_param, #initialize, #optional_block_param, #optional_param, #param, #repeated_param, #required_repeated_param, #return_type

Constructor Details

This class inherits a constructor from Puppet::Functions::DispatcherBuilder

Instance Method Details

#injected_param(type, name, injection_name = '') ⇒ 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.

TODO: is param name really needed? Perhaps for error messages? (it is unused now)



730
731
732
733
734
# File 'lib/puppet/functions.rb', line 730

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

#injected_producer_param(type, name, injection_name = '') ⇒ 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.

TODO: is param name really needed? Perhaps for error messages? (it is unused now)



739
740
741
742
743
# File 'lib/puppet/functions.rb', line 739

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

#scope_paramObject

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.



722
723
724
725
726
# File 'lib/puppet/functions.rb', line 722

def scope_param()
  @injections << [:scope, 'scope', '', :dispatcher_internal]
  # mark what should be picked for this position when dispatching
  @weaving << [@injections.size()-1]
end