Class: Puppet::Functions::LocalTypeAliasesBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet/functions.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(loader, name) ⇒ LocalTypeAliasesBuilder

Returns a new instance of LocalTypeAliasesBuilder


608
609
610
611
612
613
# File 'lib/puppet/functions.rb', line 608

def initialize(loader, name)
  @loader = Puppet::Pops::Loader::PredefinedLoader.new(loader, :"local_function_#{name}")
  @local_types = []
  # get the shared parser used by puppet's compiler
  @parser = Puppet::Pops::Parser::EvaluatingParser.singleton()
end

Instance Attribute Details

#loaderObject (readonly)


606
607
608
# File 'lib/puppet/functions.rb', line 606

def loader
  @loader
end

#local_typesObject (readonly)


606
607
608
# File 'lib/puppet/functions.rb', line 606

def local_types
  @local_types
end

#parserObject (readonly)


606
607
608
# File 'lib/puppet/functions.rb', line 606

def parser
  @parser
end

Instance Method Details

#type(assignment_string) ⇒ Object

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.


623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
# File 'lib/puppet/functions.rb', line 623

def type(assignment_string)
  # Get location to use in case of error - this produces ruby filename and where call to 'type' occurred
  # but strips off the rest of the internal "where" as it is not meaningful to user.
  #
  rb_location = caller(1, 1).first
  begin
    result = parser.parse_string("type #{assignment_string}", nil)
  rescue StandardError => e
    rb_location = rb_location.gsub(/:in.*$/, '')
    # Create a meaningful location for parse errors - show both what went wrong with the parsing
    # and in which ruby file it was found.
    raise ArgumentError, _("Parsing of 'type \"%{assignment_string}\"' failed with message: <%{message}>.\n" +
      "Called from <%{ruby_file_location}>") % {
        assignment_string: assignment_string,
        message: e.message,
        ruby_file_location: rb_location
    }
  end
  unless result.body.kind_of?(Puppet::Pops::Model::TypeAlias)
    rb_location = rb_location.gsub(/:in.*$/, '')
    raise ArgumentError, _("Expected a type alias assignment on the form 'AliasType = T', got '%{assignment_string}'.\n"+
    "Called from <%{ruby_file_location}>") % {
      assignment_string: assignment_string,
      ruby_file_location: rb_location
    }
  end
  @local_types << result.body
end