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

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, line 358
def block_param(*type_and_name)
  case type_and_name.size
  when 0
    # the type must be an independent instance since it will be contained in another type
    type = @all_callables.copy
    name = 'block'
  when 1
    # the type must be an independent instance since it will be contained in another type
    type = @all_callables.copy
    name = type_and_name[0]
  when 2
    type_string, name = type_and_name
    type = @type_parser.parse(type_string)
  else
    raise ArgumentError, "block_param accepts max 2 arguments (type, name), got #{type_and_name.size}."
  end

  unless Puppet::Pops::Types::TypeCalculator.is_kind_of_callable?(type, false)
    raise ArgumentError, "Expected PCallableType or PVariantType thereof, got #{type.class}"
  end

  unless name.is_a?(String) || name.is_a?(Symbol)
    raise ArgumentError, "Expected block_param name to be a String or Symbol, got #{name.class}"
  end

  if @block_type.nil?
    @block_type = type
    @block_name = name
  else
    raise ArgumentError, 'Attempt to redefine block'
  end
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, line 397
def optional_block_param(*type_and_name)
  # same as required, only wrap the result in an optional type
  required_block_param(*type_and_name)
  @block_type = Puppet::Pops::Types::TypeFactory.optional(@block_type)
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, line 315
def optional_param(type, name)
  internal_param(type, name)
  @max += 1
end
optional_repeated_param(type, name) click to toggle source
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, line 297
def param(type, name)
  internal_param(type, name)
  raise ArgumentError, 'A required parameter cannot be added after an optional parameter' if @min != @max
  @min += 1
  @max += 1
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, line 330
def repeated_param(type, name)
  internal_param(type, name, true)
  @max = :default
end
Also aliased as: optional_repeated_param
required_block_param(*type_and_name) click to toggle source
Alias for: block_param
required_param(type, name) click to toggle source
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, line 346
def required_repeated_param(type, name)
  internal_param(type, name, true)
  raise ArgumentError, 'A required repeated parameter cannot be added after an optional parameter' if @min != @max
  @min += 1
  @max = :default
end

Public Class Methods

new(dispatcher, type_parser, all_callables) click to toggle source

@api private

# File lib/puppet/functions.rb, line 282
def initialize(dispatcher, type_parser, all_callables)
  @type_parser = type_parser
  @all_callables = all_callables
  @dispatcher = dispatcher
end