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
397 def initialize(dispatcher, all_callables, loader)
398   @all_callables = all_callables
399   @dispatcher = dispatcher
400   @loader = loader
401 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
473 def block_param(*type_and_name)
474   case type_and_name.size
475   when 0
476     type = @all_callables
477     name = :block
478   when 1
479     type = @all_callables
480     name = type_and_name[0]
481   when 2
482     type, name = type_and_name
483     type = Puppet::Pops::Types::TypeParser.singleton.parse(type, loader) unless type.is_a?(Puppet::Pops::Types::PAnyType)
484   else
485     raise ArgumentError, _("block_param accepts max 2 arguments (type, name), got %{size}.") % { size: type_and_name.size }
486   end
487 
488   unless Puppet::Pops::Types::TypeCalculator.is_kind_of_callable?(type, false)
489     raise ArgumentError, _("Expected PCallableType or PVariantType thereof, got %{type_class}") % { type_class: type.class }
490   end
491 
492   unless name.is_a?(Symbol)
493     raise ArgumentError, _("Expected block_param name to be a Symbol, got %{name_class}") % { name_class: name.class }
494   end
495 
496   if @block_type.nil?
497     @block_type = type
498     @block_name = name
499   else
500     raise ArgumentError, _('Attempt to redefine block')
501   end
502 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
510 def optional_block_param(*type_and_name)
511   # same as required, only wrap the result in an optional type
512   required_block_param(*type_and_name)
513   @block_type = Puppet::Pops::Types::TypeFactory.optional(@block_type)
514 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
430 def optional_param(type, name)
431   internal_param(type, name)
432   @max += 1
433 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
412 def param(type, name)
413   internal_param(type, name)
414   raise ArgumentError, _('A required parameter cannot be added after an optional parameter') if @min != @max
415   @min += 1
416   @max += 1
417 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
445 def repeated_param(type, name)
446   internal_param(type, name, true)
447   @max = :default
448 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
461 def required_repeated_param(type, name)
462   internal_param(type, name, true)
463   raise ArgumentError, _('A required repeated parameter cannot be added after an optional parameter') if @min != @max
464   @min += 1
465   @max = :default
466 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
520 def return_type(type)
521   unless type.is_a?(String) || type.is_a?(Puppet::Pops::Types::PAnyType)
522     raise ArgumentError, _("Argument to 'return_type' must be a String reference to a Puppet Data Type. Got %{type_class}") % { type_class: type.class }
523   end
524   @return_type = type
525 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
579 def create_callable(types, block_type, return_type, from, to)
580   mapped_types = types.map do |t|
581     t.is_a?(Puppet::Pops::Types::PAnyType) ? t : internal_type_parse(t, loader)
582   end
583   param_types = Puppet::Pops::Types::PTupleType.new(mapped_types, from > 0 && from == to ? nil : Puppet::Pops::Types::PIntegerType.new(from, to))
584   return_type = internal_type_parse(return_type, loader) unless return_type.nil? || return_type.is_a?(Puppet::Pops::Types::PAnyType)
585   Puppet::Pops::Types::PCallableType.new(param_types, block_type, return_type)
586 end
dispatch(meth_name, argument_mismatch_handler, &block) click to toggle source

@api private

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

@api private

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