class Puppet::Pops::Adapters::LoaderAdapter

A LoaderAdapter adapts an object with a {Loader}. This is used to make further loading from the perspective of the adapted object take place in the perspective of this Loader.

It is typically enough to adapt the root of a model as a search is made towards the root of the model until a loader is found, but there is no harm in duplicating this information provided a contained object is adapted with the correct loader.

@see Utils#find_adapter @api private

Attributes

loader_name[RW]

Public Class Methods

find_module_for_dir(environment, paths, dir) click to toggle source

@api private

    # File lib/puppet/pops/adapters.rb
138 def self.find_module_for_dir(environment, paths, dir)
139   return nil if dir.nil?
140   file_path = Pathname.new(dir)
141   paths.each do |path|
142     begin
143       relative_path = file_path.relative_path_from(path).to_s.split(File::SEPARATOR)
144     rescue ArgumentError
145       # file_path was not relative to the module_path. That's OK.
146       next
147     end
148     if relative_path.length > 1
149       mod = environment.module(relative_path[0])
150       return mod unless mod.nil?
151     end
152   end
153   nil
154 end
loader_for_model_object(model, file = nil, default_loader = nil) click to toggle source

Finds the loader to use when loading originates from the source position of the given argument.

@param instance [Model::PopsObject] The model object @param file [String] the file from where the model was parsed @param default_loader [Loader] the loader to return if no loader is found for the model @return [Loader] the found loader or default_loader if it could not be found

   # File lib/puppet/pops/adapters.rb
87 def self.loader_for_model_object(model, file = nil, default_loader = nil)
88   loaders = Puppet.lookup(:loaders) { nil }
89   if loaders.nil?
90     default_loader || Loaders.static_loader
91   else
92     loader_name = loader_name_by_source(loaders.environment, model, file)
93     if loader_name.nil?
94       default_loader || loaders[Loader::ENVIRONMENT_PRIVATE]
95     else
96       loaders[loader_name]
97     end
98   end
99 end
loader_name_by_source(environment, instance, file) click to toggle source

Attempts to find the module that `instance` originates from by looking at it's {SourcePosAdapter} and compare the `locator.file` found there with the module paths given in the environment found in the given `scope`. If the file is found to be relative to a path, then the first segment of the relative path is interpreted as the name of a module. The object that the {SourcePosAdapter} is adapted to will then be adapted to the private loader for that module and that adapter is returned.

The method returns `nil` when no module could be found.

@param environment [Puppet::Node::Environment] the current environment @param instance [Model::PopsObject] the AST for the code @param file [String] the path to the file for the code or `nil` @return [String] the name of the loader associated with the source @api private

    # File lib/puppet/pops/adapters.rb
125 def self.loader_name_by_source(environment, instance, file)
126   file = instance.file if file.nil?
127   return nil if file.nil? || EMPTY_STRING == file
128   pn_adapter = PathsAndNameCacheAdapter.adapt(environment)
129   dir = File.dirname(file)
130   pn_adapter.cache.fetch(dir) do |key|
131     mod = find_module_for_dir(environment, pn_adapter.paths, dir)
132     loader_name = mod.nil? ? nil : "#{mod.name} private"
133     pn_adapter.cache[key] = loader_name
134   end
135 end