Class: Puppet::Pops::Adapters::LoaderAdapter Private

Inherits:
Puppet::Pops::Adaptable::Adapter show all
Defined in:
lib/puppet/pops/adapters.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.

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 Also:

  • Utils#find_adapter

Defined Under Namespace

Classes: PathsAndNameCacheAdapter

Constant Summary

Constants inherited from Puppet::Pops::Adaptable::Adapter

Puppet::Pops::Adaptable::Adapter::DOUBLE_COLON, Puppet::Pops::Adaptable::Adapter::USCORE

Instance Attribute Summary collapse

Class Method Summary collapse

Methods inherited from Puppet::Pops::Adaptable::Adapter

adapt, adapt_new, associate_adapter, clear, create_adapter, get, instance_var_name, self_attr_name

Instance Attribute Details

#loader_nameObject

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.



117
118
119
# File 'lib/puppet/pops/adapters.rb', line 117

def loader_name
  @loader_name
end

Class Method Details

.find_file(instance) ⇒ 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.



187
188
189
190
# File 'lib/puppet/pops/adapters.rb', line 187

def self.find_file(instance)
  source_pos = Utils.find_closest_positioned(instance)
  source_pos.nil? ? nil : source_pos.locator.file
end

.find_module_for_dir(environment, paths, dir) ⇒ 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.



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/puppet/pops/adapters.rb', line 168

def self.find_module_for_dir(environment, paths, dir)
  return nil if dir.nil?
  file_path = Pathname.new(dir)
  paths.each do |path|
    begin
      relative_path = file_path.relative_path_from(path).to_s.split(File::SEPARATOR)
    rescue ArgumentError
      # file_path was not relative to the module_path. That's OK.
      next
    end
    if relative_path.length > 1
      mod = environment.module(relative_path[0])
      return mod unless mod.nil?
    end
  end
  nil
end

.loader_for_model_object(model, scope, file = nil) ⇒ Loader?

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.

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

Parameters:

Returns:

  • (Loader, nil)

    the found loader or ‘nil` if it could not be found



126
127
128
129
130
131
132
133
134
135
# File 'lib/puppet/pops/adapters.rb', line 126

def self.loader_for_model_object(model, scope, file = nil)
  if scope.nil?
    loaders = Puppet.lookup(:loaders) { nil }
    loaders.nil? ? nil : loaders.private_environment_loader
  else
    loaders = scope.compiler.loaders
    loader_name = loader_name_by_source(scope.environment, model, file)
    loader_name.nil? ? loaders.private_environment_loader : loaders[loader_name]
  end
end

.loader_name_by_source(environment, instance, file) ⇒ 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.

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.

Parameters:

  • scope
  • instance


152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/puppet/pops/adapters.rb', line 152

def self.loader_name_by_source(environment, instance, file)
  file = find_file(instance) if file.nil?
  return nil if file.nil?
  pn_adapter = PathsAndNameCacheAdapter.adapt(environment) do |a|
    a.paths ||= environment.modulepath.map { |p| Pathname.new(p) }
    a.cache ||= {}
  end
  dir = File.dirname(file)
  pn_adapter.cache.fetch(dir) do |key|
    mod = find_module_for_dir(environment, pn_adapter.paths, dir)
    loader_name = mod.nil? ? nil : "#{mod.name} private"
    pn_adapter.cache[key] = loader_name
  end
end