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
@api public
TODO: refactor to parameter_names
since that makes it API
@api public
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
@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 33 def initialize(type, method_name, param_names, last_captures = false, block_name = nil, injections = EMPTY_ARRAY, weaving = EMPTY_ARRAY, argument_mismatch_handler = false) 34 @type = type 35 @method_name = method_name 36 @param_names = param_names 37 @last_captures = last_captures 38 @block_name = block_name 39 @injections = injections 40 @weaving = weaving 41 @argument_mismatch_handler = argument_mismatch_handler 42 end
Public Instance Methods
# File lib/puppet/pops/functions/dispatch.rb 54 def argument_mismatch_handler? 55 @argument_mismatch_handler 56 end
@api private
# File lib/puppet/pops/functions/dispatch.rb 59 def invoke(instance, calling_scope, args, &block) 60 result = instance.send(@method_name, *weave(calling_scope, args), &block) 61 return_type = @type.return_type 62 Types::TypeAsserter.assert_instance_of(nil, return_type, result) { "value returned from function '#{@method_name}'" } unless return_type.nil? 63 result 64 end
@api private
# File lib/puppet/pops/functions/dispatch.rb 50 def last_captures_rest? 51 @last_captures 52 end
@api private
# File lib/puppet/pops/functions/dispatch.rb 45 def parameter_names 46 @param_names 47 end
@api private
# File lib/puppet/pops/functions/dispatch.rb 67 def weave(scope, args) 68 # no need to weave if there are no injections 69 if @injections.empty? 70 args 71 else 72 new_args = [] 73 @weaving.each do |knit| 74 if knit.is_a?(Array) 75 injection_name = @injections[knit[0]] 76 new_args << 77 case injection_name 78 when :scope 79 scope 80 when :pal_script_compiler 81 Puppet.lookup(:pal_script_compiler) 82 when :cache 83 Puppet::Pops::Adapters::ObjectIdCacheAdapter.adapt(scope.compiler) 84 when :pal_catalog_compiler 85 Puppet.lookup(:pal_catalog_compiler) 86 when :pal_compiler 87 Puppet.lookup(:pal_compiler) 88 else 89 raise ArgumentError, _("Unknown injection %{injection_name}") % { injection_name: injection_name } 90 end 91 else 92 # Careful so no new nil arguments are added since they would override default 93 # parameter values in the received 94 if knit < 0 95 idx = -knit - 1 96 new_args += args[idx..-1] if idx < args.size 97 else 98 new_args << args[knit] if knit < args.size 99 end 100 end 101 end 102 new_args 103 end 104 end