Class: Puppet::Pops::Functions::Dispatcher Private

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet/pops/functions/dispatcher.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Evaluate the dispatches defined as Dispatch instances to call the appropriate method on the Function instance.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDispatcher

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Dispatcher.



10
11
12
# File 'lib/puppet/pops/functions/dispatcher.rb', line 10

def initialize()
  @dispatchers = [ ]
end

Instance Attribute Details

#dispatchersObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



7
8
9
# File 'lib/puppet/pops/functions/dispatcher.rb', line 7

def dispatchers
  @dispatchers
end

Instance Method Details

#add_dispatch(type, method_name, param_names, block_name, injections, weaving, last_captures) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Adds a regular dispatch for one method name

Parameters:



48
49
50
# File 'lib/puppet/pops/functions/dispatcher.rb', line 48

def add_dispatch(type, method_name, param_names, block_name, injections, weaving, last_captures)
  @dispatchers << Puppet::Pops::Functions::Dispatch.new(type, method_name, param_names, block_name, injections, weaving, last_captures)
end

#dispatch(instance, calling_scope, args) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Dispatches the call to the first found signature (entry with matching type).

Parameters:

  • instance (Puppet::Functions::Function)
    • the function to call

  • calling_scope (T.B.D::Scope)
    • the scope of the caller

  • args (Array<Object>)
    • the given arguments in the form of an Array

Returns:

  • (Object)
    • what the called function produced



30
31
32
33
34
35
36
37
38
39
# File 'lib/puppet/pops/functions/dispatcher.rb', line 30

def dispatch(instance, calling_scope, args)
  tc = Puppet::Pops::Types::TypeCalculator
  actual = tc.infer_set(args)
  found = @dispatchers.find { |d| tc.callable?(d.type, actual) }
  if found
    found.invoke(instance, calling_scope, args)
  else
    raise ArgumentError, "function '#{instance.class.name}' called with mis-matched arguments\n#{Puppet::Pops::Evaluator::CallableMismatchDescriber.diff_string(instance.class.name, actual, @dispatchers)}"
  end
end

#empty?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Answers if dispatching has been defined

Returns:

  • (Boolean)

    true if dispatching has been defined



18
19
20
# File 'lib/puppet/pops/functions/dispatcher.rb', line 18

def empty?
  @dispatchers.empty?
end

#signaturesObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



67
68
69
# File 'lib/puppet/pops/functions/dispatcher.rb', line 67

def signatures
  @dispatchers
end

#to_typeObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Produces a CallableType for a single signature, and a Variant otherwise



55
56
57
58
59
60
61
62
63
64
# File 'lib/puppet/pops/functions/dispatcher.rb', line 55

def to_type()
  # make a copy to make sure it can be contained by someone else (even if it is not contained here, it
  # should be treated as immutable).
  #
  callables = dispatchers.map { | dispatch | dispatch.type.copy }

  # multiple signatures, produce a Variant type of Callable1-n (must copy them)
  # single signature, produce single Callable
  callables.size > 1 ?  Puppet::Pops::Types::TypeFactory.variant(*callables) : callables.pop
end