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
609 def initialize(loader, name)
610   @loader = Puppet::Pops::Loader::PredefinedLoader.new(loader, :"local_function_#{name}", loader.environment)
611   @local_types = []
612   # get the shared parser used by puppet's compiler
613   @parser = Puppet::Pops::Parser::EvaluatingParser.singleton()
614 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
624 def type(assignment_string)
625   # Get location to use in case of error - this produces ruby filename and where call to 'type' occurred
626   # but strips off the rest of the internal "where" as it is not meaningful to user.
627   #
628   rb_location = caller(1, 1).first
629   begin
630     result = parser.parse_string("type #{assignment_string}", nil)
631   rescue StandardError => e
632     rb_location = rb_location.gsub(/:in.*$/, '')
633     # Create a meaningful location for parse errors - show both what went wrong with the parsing
634     # and in which ruby file it was found.
635     raise ArgumentError, _("Parsing of 'type \"%{assignment_string}\"' failed with message: <%{message}>.\n" +
636       "Called from <%{ruby_file_location}>") % {
637         assignment_string: assignment_string,
638         message: e.message,
639         ruby_file_location: rb_location
640     }
641   end
642   unless result.body.kind_of?(Puppet::Pops::Model::TypeAlias)
643     rb_location = rb_location.gsub(/:in.*$/, '')
644     raise ArgumentError, _("Expected a type alias assignment on the form 'AliasType = T', got '%{assignment_string}'.\n"+
645     "Called from <%{ruby_file_location}>") % {
646       assignment_string: assignment_string,
647       ruby_file_location: rb_location
648     }
649   end
650   @local_types << result.body
651 end