Class: Puppet::Pops::Types::ClassLoader Private

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet/pops/types/class_loader.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

The ClassLoader provides a Class instance given a class name or a meta-type. If the class is not already loaded, it is loaded using the Puppet Autoloader. This means it can load a class from a gem, or from puppet modules.

Class Method Summary collapse

Class Method Details

.provide(name) ⇒ Class?

Returns a Class given a fully qualified class name. Lookup of class is never relative to the calling namespace.

Raises:

  • ArgumentError If the given argument has the wrong type


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/puppet/pops/types/class_loader.rb', line 19

def self.provide(name)
  case name
  when String
    provide_from_string(name)

  when Array
    provide_from_name_path(name.join('::'), name)

  when PAnyType, PTypeType
    provide_from_type(name)

  else
    raise ArgumentError, "Cannot provide a class from a '#{name.class.name}'"
  end
end

.provide_from_name_path(name, name_path) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/puppet/pops/types/class_loader.rb', line 82

def self.provide_from_name_path(name, name_path)
  # If class is already loaded, try this first
  result = find_class(name_path)

  unless result.is_a?(Module)
    # Attempt to load it using the auto loader
    loaded_path = nil
    if paths_for_name(name_path).find {|path| loaded_path = path; @autoloader.load(path, Puppet.lookup(:current_environment)) }
      result = find_class(name_path)
      unless result.is_a?(Module)
        raise RuntimeError, "Loading of #{name} using relative path: '#{loaded_path}' did not create expected class"
      end
    end
  end
  return nil unless result.is_a?(Module)
  result
end