Class: Puppet::Functions::DispatcherBuilder

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

Overview

Public api methods of the DispatcherBuilder are available within dispatch() blocks declared in a Puppet::Function.create_function() call.

Direct Known Subclasses

InternalDispatchBuilder

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dispatcher, all_callables, loader) ⇒ DispatcherBuilder

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 DispatcherBuilder.



392
393
394
395
396
# File 'lib/puppet/functions.rb', line 392

def initialize(dispatcher, all_callables, loader)
  @all_callables = all_callables
  @dispatcher = dispatcher
  @loader = loader
end

Instance Attribute Details

#loaderObject (readonly)



389
390
391
# File 'lib/puppet/functions.rb', line 389

def loader
  @loader
end

Instance Method Details

#block_param(*type_and_name) ⇒ Object Also known as: required_block_param

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”.



468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
# File 'lib/puppet/functions.rb', line 468

def block_param(*type_and_name)
  case type_and_name.size
  when 0
    type = @all_callables
    name = :block
  when 1
    type = @all_callables
    name = type_and_name[0]
  when 2
    type_string, name = type_and_name
    type = Puppet::Pops::Types::TypeParser.singleton.parse(type_string, loader)
  else
    raise ArgumentError, _("block_param accepts max 2 arguments (type, name), got %{size}.") % { size: 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}") % { type_class: type.class }
  end

  unless name.is_a?(Symbol)
    raise ArgumentError, _("Expected block_param name to be a Symbol, got %{name_class}") % { name_class: name.class }
  end

  if @block_type.nil?
    @block_type = type
    @block_name = name
  else
    raise ArgumentError, _('Attempt to redefine block')
  end
end

#optional_block_param(*type_and_name) ⇒ Object

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).



505
506
507
508
509
# File 'lib/puppet/functions.rb', line 505

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) ⇒ Void

Defines an optional positional parameter with type and name. May not be followed by a required parameter.

Parameters:

  • type (String)

    The type specification for the parameter.

  • 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.

Returns:

  • (Void)


425
426
427
428
# File 'lib/puppet/functions.rb', line 425

def optional_param(type, name)
  internal_param(type, name)
  @max += 1
end

#param(type, name) ⇒ Void Also known as: required_param

Defines a required positional parameter with type and name.

Parameters:

  • type (String)

    The type specification for the parameter.

  • 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.

Returns:

  • (Void)

Raises:

  • (ArgumentError)


407
408
409
410
411
412
# File 'lib/puppet/functions.rb', line 407

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

#repeated_param(type, name) ⇒ Void Also known as: optional_repeated_param

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.

Parameters:

  • type (String)

    The type specification for the parameter.

  • 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.

Returns:

  • (Void)


440
441
442
443
# File 'lib/puppet/functions.rb', line 440

def repeated_param(type, name)
  internal_param(type, name, true)
  @max = :default
end

#required_repeated_param(type, name) ⇒ Void

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.

Parameters:

  • type (String)

    The type specification for the parameter.

  • 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.

Returns:

  • (Void)

Raises:

  • (ArgumentError)


456
457
458
459
460
461
# File 'lib/puppet/functions.rb', line 456

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

#return_type(type) ⇒ Object

Defines the return type. Defaults to ‘Any’

Parameters:

  • type (String)

    a reference to a Puppet Data Type

Raises:

  • (ArgumentError)


515
516
517
518
# File 'lib/puppet/functions.rb', line 515

def return_type(type)
  raise ArgumentError, _("Argument to 'return_type' must be a String reference to a Puppet Data Type. Got %{type_class}") % { type_class: type.class } unless type.is_a?(String)
  @return_type = type
end