Class: Puppet::Functions::DispatcherBuilder
- 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
Instance Method Summary collapse
-
#block_param(*type_and_name) ⇒ Object
(also: #required_block_param)
Defines one required block parameter that may appear last.
-
#initialize(dispatcher, type_parser, all_callables) ⇒ DispatcherBuilder
constructor
private
A new instance of DispatcherBuilder.
-
#optional_block_param(*type_and_name) ⇒ Object
Defines one optional block parameter that may appear last.
-
#optional_param(type, name) ⇒ Void
Defines an optional positional parameter with type and name.
-
#param(type, name) ⇒ Void
(also: #required_param)
Defines a required positional parameter with type and name.
-
#repeated_param(type, name) ⇒ Void
(also: #optional_repeated_param)
Defines a repeated positional parameter with type and name that may occur 0 to “infinite” number of times.
-
#required_repeated_param(type, name) ⇒ Void
Defines a repeated positional parameter with type and name that may occur 1 to “infinite” number of times.
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.
273 274 275 276 277 |
# File 'lib/puppet/functions.rb', line 273 def initialize(dispatcher, type_parser, all_callables) @type_parser = type_parser @all_callables = all_callables @dispatcher = dispatcher 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”.
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 |
# File 'lib/puppet/functions.rb', line 349 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 = @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?(Symbol) raise ArgumentError, "Expected block_param name to be a Symbol, got #{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).
386 387 388 389 390 |
# File 'lib/puppet/functions.rb', line 386 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.
306 307 308 309 |
# File 'lib/puppet/functions.rb', line 306 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.
288 289 290 291 292 293 |
# File 'lib/puppet/functions.rb', line 288 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.
321 322 323 324 |
# File 'lib/puppet/functions.rb', line 321 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.
337 338 339 340 341 342 |
# File 'lib/puppet/functions.rb', line 337 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 |