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

#closure_scope, #loader

Class Method Summary collapse

Methods inherited from Pops::Functions::Function

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

Constructor Details

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

Class Method Details

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



274
275
276
# File 'lib/puppet/functions.rb', line 274

def self.builder
  DispatcherBuilder.new(dispatcher, Puppet::Pops::Types::TypeParser.singleton, 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)


286
287
288
289
290
# File 'lib/puppet/functions.rb', line 286

def self.dispatch(meth_name, &block)
  builder().instance_eval do
    dispatch(meth_name, &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



300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
# File 'lib/puppet/functions.rb', line 300

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.



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

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