class Puppet::Functions::DispatcherBuilder

Public api methods of the DispatcherBuilder are available within dispatch() blocks declared in a Puppet::Function.create_function() call.

@api public

Attributes

loader[R]

Public Class Methods

new(dispatcher, all_callables, loader) click to toggle source

@api private

    # File lib/puppet/functions.rb
398 def initialize(dispatcher, all_callables, loader)
399   @all_callables = all_callables
400   @dispatcher = dispatcher
401   @loader = loader
402 end

Public Instance Methods

block_param(*type_and_name) click to toggle source

Defines one required block parameter that may appear last. If type and name is missing the default type is “Callable”, and the name is “block”. If only one parameter is given, then that is the name and the type is “Callable”.

@api public

    # File lib/puppet/functions.rb
474 def block_param(*type_and_name)
475   case type_and_name.size
476   when 0
477     type = @all_callables
478     name = :block
479   when 1
480     type = @all_callables
481     name = type_and_name[0]
482   when 2
483     type, name = type_and_name
484     type = Puppet::Pops::Types::TypeParser.singleton.parse(type, loader) unless type.is_a?(Puppet::Pops::Types::PAnyType)
485   else
486     raise ArgumentError, _("block_param accepts max 2 arguments (type, name), got %{size}.") % { size: type_and_name.size }
487   end
488 
489   unless Puppet::Pops::Types::TypeCalculator.is_kind_of_callable?(type, false)
490     raise ArgumentError, _("Expected PCallableType or PVariantType thereof, got %{type_class}") % { type_class: type.class }
491   end
492 
493   unless name.is_a?(Symbol)
494     raise ArgumentError, _("Expected block_param name to be a Symbol, got %{name_class}") % { name_class: name.class }
495   end
496 
497   if @block_type.nil?
498     @block_type = type
499     @block_name = name
500   else
501     raise ArgumentError, _('Attempt to redefine block')
502   end
503 end
Also aliased as: required_block_param
optional_block_param(*type_and_name) click to toggle source

Defines one optional block parameter that may appear last. If type or name is missing the defaults are “any callable”, and the name is “block”. The implementor of the dispatch target must use block = nil when it is optional (or an error is raised when the call is made).

@api public

    # File lib/puppet/functions.rb
511 def optional_block_param(*type_and_name)
512   # same as required, only wrap the result in an optional type
513   required_block_param(*type_and_name)
514   @block_type = Puppet::Pops::Types::TypeFactory.optional(@block_type)
515 end
optional_param(type, name) click to toggle source

Defines an optional positional parameter with type and name. May not be followed by a required parameter.

@param type [String] The type specification for the parameter. @param name [Symbol] The name of the parameter. This is primarily used

for error message output and does not have to match an implementation
method parameter.

@return [Void]

@api public

    # File lib/puppet/functions.rb
431 def optional_param(type, name)
432   internal_param(type, name)
433   @max += 1
434 end
optional_repeated_param(type, name)
Alias for: repeated_param
param(type, name) click to toggle source

Defines a required positional parameter with type and name.

@param type [String] The type specification for the parameter. @param name [Symbol] The name of the parameter. This is primarily used

for error message output and does not have to match an implementation
method parameter.

@return [Void]

@api public

    # File lib/puppet/functions.rb
413 def param(type, name)
414   internal_param(type, name)
415   raise ArgumentError, _('A required parameter cannot be added after an optional parameter') if @min != @max
416   @min += 1
417   @max += 1
418 end
Also aliased as: required_param
repeated_param(type, name) click to toggle source

Defines a repeated positional parameter with type and name that may occur 0 to “infinite” number of times. It may only appear last or just before a block parameter.

@param type [String] The type specification for the parameter. @param name [Symbol] The name of the parameter. This is primarily used

for error message output and does not have to match an implementation
method parameter.

@return [Void]

@api public

    # File lib/puppet/functions.rb
446 def repeated_param(type, name)
447   internal_param(type, name, true)
448   @max = :default
449 end
Also aliased as: optional_repeated_param
required_block_param(*type_and_name)
Alias for: block_param
required_param(type, name)
Alias for: param
required_repeated_param(type, name) click to toggle source

Defines a repeated positional parameter with type and name that may occur 1 to “infinite” number of times. It may only appear last or just before a block parameter.

@param type [String] The type specification for the parameter. @param name [Symbol] The name of the parameter. This is primarily used

for error message output and does not have to match an implementation
method parameter.

@return [Void]

@api public

    # File lib/puppet/functions.rb
462 def required_repeated_param(type, name)
463   internal_param(type, name, true)
464   raise ArgumentError, _('A required repeated parameter cannot be added after an optional parameter') if @min != @max
465   @min += 1
466   @max = :default
467 end
return_type(type) click to toggle source

Defines the return type. Defaults to 'Any' @param [String] type a reference to a Puppet Data Type

@api public

    # File lib/puppet/functions.rb
521 def return_type(type)
522   unless type.is_a?(String) || type.is_a?(Puppet::Pops::Types::PAnyType)
523     raise ArgumentError, _("Argument to 'return_type' must be a String reference to a Puppet Data Type. Got %{type_class}") % { type_class: type.class }
524   end
525   @return_type = type
526 end

Private Instance Methods

create_callable(types, block_type, return_type, from, to) click to toggle source

Handles creation of a callable type from strings specifications of puppet types and allows the min/max occurs of the given types to be given as one or two integer values at the end. The given block_type should be Optional, Callable, or nil.

@api private

    # File lib/puppet/functions.rb
580 def create_callable(types, block_type, return_type, from, to)
581   mapped_types = types.map do |t|
582     t.is_a?(Puppet::Pops::Types::PAnyType) ? t : internal_type_parse(t, loader)
583   end
584   param_types = Puppet::Pops::Types::PTupleType.new(mapped_types, from > 0 && from == to ? nil : Puppet::Pops::Types::PIntegerType.new(from, to))
585   return_type = internal_type_parse(return_type, loader) unless return_type.nil? || return_type.is_a?(Puppet::Pops::Types::PAnyType)
586   Puppet::Pops::Types::PCallableType.new(param_types, block_type, return_type)
587 end
dispatch(meth_name, argument_mismatch_handler, &block) click to toggle source

@api private

    # File lib/puppet/functions.rb
554 def dispatch(meth_name, argument_mismatch_handler, &block)
555   # an array of either an index into names/types, or an array with
556   # injection information [type, name, injection_name] used when the call
557   # is being made to weave injections into the given arguments.
558   #
559   @types = []
560   @names = []
561   @weaving = []
562   @injections = []
563   @min = 0
564   @max = 0
565   @block_type = nil
566   @block_name = nil
567   @return_type = nil
568   @argument_mismatch_hander = argument_mismatch_handler
569   self.instance_eval(&block)
570   callable_t = create_callable(@types, @block_type, @return_type, @min, @max)
571   @dispatcher.add(Puppet::Pops::Functions::Dispatch.new(callable_t, meth_name, @names, @max == :default, @block_name, @injections, @weaving, @argument_mismatch_hander))
572 end
internal_param(type, name, repeat = false) click to toggle source

@api private

    # File lib/puppet/functions.rb
531 def internal_param(type, name, repeat = false)
532   raise ArgumentError, _('Parameters cannot be added after a block parameter') unless @block_type.nil?
533   raise ArgumentError, _('Parameters cannot be added after a repeated parameter') if @max == :default
534 
535   if name.is_a?(String)
536     raise ArgumentError, _("Parameter name argument must be a Symbol. Got %{name_class}") % { name_class: name.class }
537   end
538 
539   if type.is_a?(String) || type.is_a?(Puppet::Pops::Types::PAnyType)
540     @types << type
541     @names << name
542     # mark what should be picked for this position when dispatching
543     if repeat
544       @weaving << -@names.size()
545     else
546       @weaving << @names.size()-1
547     end
548   else
549     raise ArgumentError, _("Parameter 'type' must be a String reference to a Puppet Data Type. Got %{type_class}") % { type_class: type.class }
550   end
551 end
internal_type_parse(type_string, loader) click to toggle source
    # File lib/puppet/functions.rb
589 def internal_type_parse(type_string, loader)
590   begin
591     Puppet::Pops::Types::TypeParser.singleton.parse(type_string, loader)
592   rescue StandardError => e
593     raise ArgumentError, _("Parsing of type string '\"%{type_string}\"' failed with message: <%{message}>.\n") % {
594         type_string: type_string,
595         message: e.message
596     }
597   end
598 end