class Puppet::Pops::Functions::Dispatch

Defines a connection between a implementation method and the signature that the method will handle.

This interface should not be used directly. Instead dispatches should be constructed using the DSL defined in {Puppet::Functions}.

@api private

Attributes

block_name[R]

@api public

injections[R]
param_names[R]

TODO: refactor to parameter_names since that makes it API

type[R]

@api public

weaving[R]

Describes how arguments are woven if there are injections, a regular argument is a given arg index, an array an injection description.

Public Class Methods

new(type, method_name, param_names, last_captures = false, block_name = nil, injections = EMPTY_ARRAY, weaving = EMPTY_ARRAY, argument_mismatch_handler = false) click to toggle source

@param type [Puppet::Pops::Types::PArrayType, Puppet::Pops::Types::PTupleType] - type describing signature @param method_name [String] the name of the method that will be called when type matches given arguments @param param_names [Array<String>] names matching the number of parameters specified by type (or empty array) @param block_name [String,nil] name of block parameter, no nil @param injections [Array<Array>] injection data for weaved parameters @param weaving [Array<Integer,Array>] weaving knits @param last_captures [Boolean] true if last parameter is captures rest @param argument_mismatch_handler [Boolean] true if this is a dispatch for an argument mismatch @api private

   # File lib/puppet/pops/functions/dispatch.rb
34 def initialize(type, method_name, param_names, last_captures = false, block_name = nil, injections = EMPTY_ARRAY, weaving = EMPTY_ARRAY, argument_mismatch_handler = false)
35   @type = type
36   @method_name = method_name
37   @param_names = param_names
38   @last_captures = last_captures
39   @block_name = block_name
40   @injections = injections
41   @weaving = weaving
42   @argument_mismatch_handler = argument_mismatch_handler
43 end

Public Instance Methods

argument_mismatch_handler?() click to toggle source
   # File lib/puppet/pops/functions/dispatch.rb
55 def argument_mismatch_handler?
56   @argument_mismatch_handler
57 end
invoke(instance, calling_scope, args, &block) click to toggle source

@api private

   # File lib/puppet/pops/functions/dispatch.rb
60 def invoke(instance, calling_scope, args, &block)
61   result = instance.send(@method_name, *weave(calling_scope, args), &block)
62   return_type = @type.return_type
63   Types::TypeAsserter.assert_instance_of(nil, return_type, result) { "value returned from function '#{@method_name}'" } unless return_type.nil?
64   result
65 end
last_captures_rest?() click to toggle source

@api private

   # File lib/puppet/pops/functions/dispatch.rb
51 def last_captures_rest?
52   @last_captures
53 end
parameter_names() click to toggle source

@api private

   # File lib/puppet/pops/functions/dispatch.rb
46 def parameter_names
47   @param_names
48 end
weave(scope, args) click to toggle source

@api private

    # File lib/puppet/pops/functions/dispatch.rb
 68 def weave(scope, args)
 69   # no need to weave if there are no injections
 70   if @injections.empty?
 71     args
 72   else
 73     new_args = []
 74     @weaving.each do |knit|
 75       if knit.is_a?(Array)
 76         injection_name = @injections[knit[0]]
 77         new_args <<
 78           case injection_name
 79           when :scope
 80             scope
 81           when :pal_script_compiler
 82             Puppet.lookup(:pal_script_compiler)
 83           when :cache
 84             Puppet::Pops::Adapters::ObjectIdCacheAdapter.adapt(scope.compiler)
 85           when :pal_catalog_compiler
 86             Puppet.lookup(:pal_catalog_compiler)
 87           when :pal_compiler
 88             Puppet.lookup(:pal_compiler)
 89           else
 90             raise ArgumentError, _("Unknown injection %{injection_name}") % { injection_name: injection_name }
 91           end
 92       else
 93         # Careful so no new nil arguments are added since they would override default
 94         # parameter values in the received
 95         if knit < 0
 96           idx = -knit - 1
 97           new_args += args[idx..-1] if idx < args.size
 98         else
 99           new_args << args[knit] if knit < args.size
100         end
101       end
102     end
103     new_args
104   end
105 end