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

Inherits:
Puppet::Pops::Adaptable::Adapter show all
Defined in:
lib/puppet/pops/adapters.rb

Overview

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

API:

  • public

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

#loaderLoader::Loader

Returns the loader.

Returns:

  • the loader

API:

  • public



122
123
124
# File 'lib/puppet/pops/adapters.rb', line 122

def loader
  @loader
end

Class Method Details

.adapt_by_source(scope, instance) ⇒ Object

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:

API:

  • public



156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/puppet/pops/adapters.rb', line 156

def self.adapt_by_source(scope, instance)
  source_pos = Utils.find_adapter(instance, SourcePosAdapter)
  unless source_pos.nil?
    mod = find_module_for_file(scope.environment, source_pos.locator.file)
    unless mod.nil?
      adapter = LoaderAdapter.adapt(source_pos.adapted)
      adapter.loader = scope.compiler.loaders.private_loader_for_module(mod.name)
      return adapter
    end
  end
  nil
end

.loader_for_model_object(model, scope) ⇒ Loader?

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

Parameters:

  • The model object

  • The scope to use

Returns:

  • the found loader or nil if it could not be found

API:

  • public



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/puppet/pops/adapters.rb', line 130

def self.loader_for_model_object(model, scope)
  # find the loader that loaded the code, or use the private_environment_loader (sees env + all modules)
  adapter = Utils.find_adapter(model, self)
  return adapter.loader unless adapter.nil?

  if scope.nil?
    loaders = Puppet.lookup(:loaders) { nil }
    loaders.nil? ? nil : loaders.private_environment_loader
  else
    # Use source location to determine calling module, or use the private_environment_loader (sees env + all modules)
    # This is necessary since not all .pp files are loaded by a Loader (see PUP-1833)
    adapter = adapt_by_source(scope, model)
    adapter.nil? ? scope.compiler.loaders.private_environment_loader : adapter.loader
  end
end