class Puppet::Functions::LocalTypeAliasesBuilder

The LocalTypeAliasBuilder is used by the 'local_types' method to collect the individual type aliases given by the function's author.

Attributes

loader[R]
local_types[R]
parser[R]

Public Class Methods

new(loader, name) click to toggle source
    # File lib/puppet/functions.rb
608 def initialize(loader, name)
609   @loader = Puppet::Pops::Loader::PredefinedLoader.new(loader, :"local_function_#{name}", loader.environment)
610   @local_types = []
611   # get the shared parser used by puppet's compiler
612   @parser = Puppet::Pops::Parser::EvaluatingParser.singleton()
613 end

Public Instance Methods

type(assignment_string) click to toggle source

Defines a local type alias, the given string should be a Puppet Language type alias expression in string form without the leading 'type' keyword. Calls to local_type must be made before the first parameter definition or an error will be raised.

@param assignment_string [String] a string on the form 'AliasType = ExistingType' @api public

    # File lib/puppet/functions.rb
623 def type(assignment_string)
624   # Get location to use in case of error - this produces ruby filename and where call to 'type' occurred
625   # but strips off the rest of the internal "where" as it is not meaningful to user.
626   #
627   rb_location = caller(1, 1).first
628   begin
629     result = parser.parse_string("type #{assignment_string}", nil)
630   rescue StandardError => e
631     rb_location = rb_location.gsub(/:in.*$/, '')
632     # Create a meaningful location for parse errors - show both what went wrong with the parsing
633     # and in which ruby file it was found.
634     raise ArgumentError, _("Parsing of 'type \"%{assignment_string}\"' failed with message: <%{message}>.\n" +
635       "Called from <%{ruby_file_location}>") % {
636         assignment_string: assignment_string,
637         message: e.message,
638         ruby_file_location: rb_location
639     }
640   end
641   unless result.body.kind_of?(Puppet::Pops::Model::TypeAlias)
642     rb_location = rb_location.gsub(/:in.*$/, '')
643     raise ArgumentError, _("Expected a type alias assignment on the form 'AliasType = T', got '%{assignment_string}'.\n"+
644     "Called from <%{ruby_file_location}>") % {
645       assignment_string: assignment_string,
646       ruby_file_location: rb_location
647     }
648   end
649   @local_types << result.body
650 end