Class: Puppet::Functions::Function

Inherits:
Pops::Functions::Function show all
Defined in:
lib/puppet/functions.rb

Overview

Function

This class is the base class for all Puppet 4x Function API functions. A specialized class is created for each puppet function.

Direct Known Subclasses

InternalFunction, PuppetFunction

Instance Attribute Summary

Attributes inherited from Pops::Functions::Function

#loader

Class Method Summary collapse

Methods inherited from Pops::Functions::Function

#call, #call_function, #closure_scope, dispatcher, #initialize, signatures

Constructor Details

This class inherits a constructor from Puppet::Pops::Functions::Function

Class Method Details

.argument_mismatch(meth_name, &block) ⇒ Void

Like ‘dispatch` but used for a specific type of argument mismatch. Will not be include in the list of valid parameter overloads for the function.

Parameters:

  • meth_name (Symbol)

    The name of the implementation method to call when the signature defined in the block matches the arguments to a call to the function.

Returns:

  • (Void)


335
336
337
338
339
# File 'lib/puppet/functions.rb', line 335

def self.argument_mismatch(meth_name, &block)
  builder().instance_eval do
    dispatch(meth_name, true, &block)
  end
end

.builderObject

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.



308
309
310
# File 'lib/puppet/functions.rb', line 308

def self.builder
  DispatcherBuilder.new(dispatcher, Puppet::Pops::Types::PCallableType::DEFAULT, loader)
end

.dispatch(meth_name, &block) ⇒ Void

Dispatch any calls that match the signature to the provided method name.

Parameters:

  • meth_name (Symbol)

    The name of the implementation method to call when the signature defined in the block matches the arguments to a call to the function.

Returns:

  • (Void)


320
321
322
323
324
# File 'lib/puppet/functions.rb', line 320

def self.dispatch(meth_name, &block)
  builder().instance_eval do
    dispatch(meth_name, false, &block)
  end
end

.local_types(&block) ⇒ Object

Allows types local to the function to be defined to ease the use of complex types in a 4.x function. Within the given block, calls to ‘type` can be made with a string ’AliasType = ExistingType` can be made to define aliases. The defined aliases are available for further aliases, and in all dispatchers.

Since:

  • 4.5.0



349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
# File 'lib/puppet/functions.rb', line 349

def self.local_types(&block)
  if loader.nil?
    raise ArgumentError, _("No loader present. Call create_loaded_function(:myname, loader,...), instead of 'create_function' if running tests")
  end

  aliases = LocalTypeAliasesBuilder.new(loader, name)
  aliases.instance_eval(&block)
  # Add the loaded types to the builder
  aliases.local_types.each do |type_alias_expr|
    # Bind the type alias to the local_loader using the alias
    t = Puppet::Pops::Loader::TypeDefinitionInstantiator.create_from_model(type_alias_expr, aliases.loader)

    # Also define a method for convenient access to the defined type alias.
    # Since initial capital letter in Ruby means a Constant these names use a prefix of
    # `type`. As an example, the type 'MyType' is accessed by calling `type_MyType`.
    define_method("type_#{t.name}") { t }
  end
  # Store the loader in the class
  @loader = aliases.loader
end

.new(closure_scope, given_loader) ⇒ Object

Creates a new function instance in the given closure scope (visibility to variables), and a loader (visibility to other definitions). The created function will either use the ‘given_loader` or (if it has local type aliases) a loader that was constructed from the loader used when loading the function’s class.

TODO: It would be of value to get rid of the second parameter here, but that would break API.



377
378
379
# File 'lib/puppet/functions.rb', line 377

def self.new(closure_scope, given_loader)
  super(closure_scope, @loader || given_loader)
end