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 Method Summary collapse

Constructor Details

#initialize(dispatcher, type_parser, all_callables) ⇒ 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.



257
258
259
260
261
# File 'lib/puppet/functions.rb', line 257

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

Instance Method Details

#arg_count(min_occurs, max_occurs) ⇒ Object

Specifies the min and max occurance of arguments (of the specified types) if something other than the exact count from the number of specified types). The max value may be specified as :default if an infinite number of arguments are supported. When max is > than the number of specified types, the last specified type repeats.



339
340
341
342
343
344
345
346
347
348
349
350
351
# File 'lib/puppet/functions.rb', line 339

def arg_count(min_occurs, max_occurs)
  @min = min_occurs
  @max = max_occurs
  unless min_occurs.is_a?(Integer) && min_occurs >= 0
    raise ArgumentError, "min arg_count of function parameter must be an Integer >=0, got #{min_occurs.class} '#{min_occurs}'"
  end
  unless max_occurs == :default || (max_occurs.is_a?(Integer) && max_occurs >= 0)
    raise ArgumentError, "max arg_count of function parameter must be an Integer >= 0, or :default, got #{max_occurs.class} '#{max_occurs}'"
  end
  unless max_occurs == :default || (max_occurs.is_a?(Integer) && max_occurs >= min_occurs)
    raise ArgumentError, "max arg_count must be :default (infinite) or >= min arg_count, got min: '#{min_occurs}, max: '#{max_occurs}'"
  end
end

#last_captures_restObject

Specifies that the last argument captures the rest.



356
357
358
# File 'lib/puppet/functions.rb', line 356

def last_captures_rest
  @last_captures = true
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).



326
327
328
329
330
# File 'lib/puppet/functions.rb', line 326

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

#param(type, name) ⇒ Void

Defines a positional parameter with type and name

Parameters:

  • type (String)

    The type specification for the parameter.

  • name (String)

    The name of the parameter. This is primarily used for error message output and does not have to match the name of the parameter on the implementation method.

Returns:

  • (Void)


272
273
274
275
276
277
278
279
280
281
# File 'lib/puppet/functions.rb', line 272

def param(type, name)
  if type.is_a?(String)
    @types << type
    @names << name
    # mark what should be picked for this position when dispatching
    @weaving << @names.size()-1
  else
    raise ArgumentError, "Type signature argument must be a String reference to a Puppet Data Type. Got #{type.class}"
  end
end

#required_block_param(*type_and_name) ⇒ Object

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



288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
# File 'lib/puppet/functions.rb', line 288

def required_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