Method: Puppet::Functions::Function.local_types

Defined in:
lib/puppet/functions.rb

.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



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

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