class Puppet::Pops::Functions::Dispatcher
Evaluate the dispatches defined as {Puppet::Pops::Functions::Dispatch} instances to call the appropriate method on the {Puppet::Pops::Functions::Function} instance.
@api private
Attributes
Public Class Methods
@api private
# File lib/puppet/pops/functions/dispatcher.rb 11 def initialize() 12 @dispatchers = [ ] 13 end
Public Instance Methods
Adds a dispatch directly to the set of dispatchers. @api private
# File lib/puppet/pops/functions/dispatcher.rb 54 def add(a_dispatch) 55 @dispatchers << a_dispatch 56 end
Dispatches the call to the first found signature (entry with matching type).
@param instance [Puppet::Functions::Function] - the function to call @param calling_scope [T.B.D::Scope] - the scope of the caller @param args [Array<Object>] - the given arguments in the form of an Array @return [Object] - what the called function produced
@api private
# File lib/puppet/pops/functions/dispatcher.rb 35 def dispatch(instance, calling_scope, args, &block) 36 37 dispatcher = find_matching_dispatcher(args, &block) 38 unless dispatcher 39 args_type = Puppet::Pops::Types::TypeCalculator.singleton.infer_set(block_given? ? args + [block] : args) 40 raise ArgumentError, Puppet::Pops::Types::TypeMismatchDescriber.describe_signatures(instance.class.name, signatures, args_type) 41 end 42 if dispatcher.argument_mismatch_handler? 43 msg = dispatcher.invoke(instance, calling_scope, args) 44 raise ArgumentError, "'#{instance.class.name}' #{msg}" 45 end 46 47 catch(:next) do 48 dispatcher.invoke(instance, calling_scope, args, &block) 49 end 50 end
Answers if dispatching has been defined @return [Boolean] true if dispatching has been defined
@api private
# File lib/puppet/pops/functions/dispatcher.rb 19 def empty? 20 @dispatchers.empty? 21 end
# File lib/puppet/pops/functions/dispatcher.rb 23 def find_matching_dispatcher(args, &block) 24 @dispatchers.find { |d| d.type.callable_with?(args, block) } 25 end
@api private
# File lib/puppet/pops/functions/dispatcher.rb 73 def signatures 74 @dispatchers.reject { |dispatcher| dispatcher.argument_mismatch_handler? } 75 end
Produces a CallableType for a single signature, and a Variant otherwise
@api private
# File lib/puppet/pops/functions/dispatcher.rb 61 def to_type() 62 # make a copy to make sure it can be contained by someone else (even if it is not contained here, it 63 # should be treated as immutable). 64 # 65 callables = dispatchers.map { | dispatch | dispatch.type } 66 67 # multiple signatures, produce a Variant type of Callable1-n (must copy them) 68 # single signature, produce single Callable 69 callables.size > 1 ? Puppet::Pops::Types::TypeFactory.variant(*callables) : callables.pop 70 end