class Puppet::Functions::Function
¶ ↑
This class is the base class for all Puppet
4x Function
API functions. A specialized class is created for each puppet function.
@api public
Public Class Methods
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.
@param 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.
@return [Void]
@api public
# File lib/puppet/functions.rb 334 def self.argument_mismatch(meth_name, &block) 335 builder().instance_eval do 336 dispatch(meth_name, true, &block) 337 end 338 end
@api private
# File lib/puppet/functions.rb 307 def self.builder 308 DispatcherBuilder.new(dispatcher, Puppet::Pops::Types::PCallableType::DEFAULT, loader) 309 end
Dispatch any calls that match the signature to the provided method name.
@param 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.
@return [Void]
@api public
# File lib/puppet/functions.rb 319 def self.dispatch(meth_name, &block) 320 builder().instance_eval do 321 dispatch(meth_name, false, &block) 322 end 323 end
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 @api public
# File lib/puppet/functions.rb 348 def self.local_types(&block) 349 if loader.nil? 350 raise ArgumentError, _("No loader present. Call create_loaded_function(:myname, loader,...), instead of 'create_function' if running tests") 351 end 352 aliases = LocalTypeAliasesBuilder.new(loader, name) 353 aliases.instance_eval(&block) 354 # Add the loaded types to the builder 355 aliases.local_types.each do |type_alias_expr| 356 # Bind the type alias to the local_loader using the alias 357 t = Puppet::Pops::Loader::TypeDefinitionInstantiator.create_from_model(type_alias_expr, aliases.loader) 358 359 # Also define a method for convenient access to the defined type alias. 360 # Since initial capital letter in Ruby means a Constant these names use a prefix of 361 # `type`. As an example, the type 'MyType' is accessed by calling `type_MyType`. 362 define_method("type_#{t.name}") { t } 363 end 364 # Store the loader in the class 365 @loader = aliases.loader 366 end
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.
Puppet::Pops::Functions::Function::new
# File lib/puppet/functions.rb 375 def self.new(closure_scope, given_loader) 376 super(closure_scope, @loader || given_loader) 377 end