Class: Puppet::Pops::Loader::Loader

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

Direct Known Subclasses

BaseLoader, StaticLoader

Defined Under Namespace

Classes: NamedEntry

Constant Summary collapse

LOADABLE_KINDS =

Describes the kinds of things that loaders can load

[:func_4x, :func_4xpp, :func_3x, :datatype, :type_pp, :resource_type_pp, :plan, :task].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(loader_name, environment) ⇒ Loader

Returns a new instance of Loader.

Parameters:


35
36
37
38
# File 'lib/puppet/pops/loader/loader.rb', line 35

def initialize(loader_name, environment)
  @loader_name = loader_name.freeze
  @environment = environment
end

Instance Attribute Details

#environmentObject (readonly)


29
30
31
# File 'lib/puppet/pops/loader/loader.rb', line 29

def environment
  @environment
end

#loader_nameObject (readonly)


29
30
31
# File 'lib/puppet/pops/loader/loader.rb', line 29

def loader_name
  @loader_name
end

Instance Method Details

#[](typed_name) ⇒ 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.

Produces the value associated with the given name if defined **in this loader**, or nil if not defined. This lookup does not trigger any loading, or search of the given name. An implementor of this method may not search or look up in any other loader, and it may not define the name.

Parameters:

  • typed_name (TypedName)
    • the type, name combination to lookup


114
115
116
117
118
119
120
121
# File 'lib/puppet/pops/loader/loader.rb', line 114

def [](typed_name)
  found = get_entry(typed_name)
  if found
    found.value
  else
    nil
  end
end

#discover(type, error_collector = nil, name_authority = Pcore::RUNTIME_NAME_AUTHORITY) {|typed_name| ... } ⇒ Array<TypedName>

Search all places where this loader would find values of a given type and return a list the found values for which the given block returns true. All found entries will be returned if no block is given.

Errors that occur function discovery will either be logged as warnings or collected by the optional `error_collector` array. When provided, it will receive DataTypes::Error instances describing each error in detail and no warnings will be logged.

Parameters:

  • type (Symbol)

    the type of values to search for

  • error_collector (Array<Puppet::DataTypes::Error>) (defaults to: nil)

    an optional array that will receive errors during load

  • name_authority (String) (defaults to: Pcore::RUNTIME_NAME_AUTHORITY)

    the name authority, defaults to the pcore runtime

Yields:

  • (typed_name)

    optional block to filter the results

Yield Parameters:

  • typed_name (TypedName)

    the typed name of a found entry

Yield Returns:

  • (Boolean)

    `true` to keep the entry, `false` to discard it.

Returns:

  • (Array<TypedName>)

    the list of names of discovered values


55
56
57
# File 'lib/puppet/pops/loader/loader.rb', line 55

def discover(type, error_collector = nil, name_authority = Pcore::RUNTIME_NAME_AUTHORITY, &block)
  return EMPTY_ARRAY
end

#find(typed_name) ⇒ NamedEntry?

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.

Searches for the given name in this loader's context (parents should already have searched their context(s) without producing a result when this method is called). An implementation of find typically caches the result.

Parameters:

  • typed_name (TypedName)

    the type, name combination to lookup

Returns:

  • (NamedEntry, nil)

    the entry for the loaded entry, or nil if not found

Raises:

  • (NotImplementedError)

132
133
134
# File 'lib/puppet/pops/loader/loader.rb', line 132

def find(typed_name)
  raise NotImplementedError, "Class #{self.class.name} must implement method #find"
end

#get_entry(typed_name) ⇒ NamedEntry?

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.

Produces a NamedEntry if a value is bound to the given name, or nil if nothing is bound.

Parameters:

  • typed_name (TypedName)

    the type, name combination to lookup

Returns:

  • (NamedEntry, nil)

    the value bound in an entry

Raises:

  • (NotImplementedError)

176
177
178
# File 'lib/puppet/pops/loader/loader.rb', line 176

def get_entry(typed_name)
  raise NotImplementedError.new
end

#inspectObject

Loaders may contain references to the environment they load items within. Consequently, calling Kernel#inspect may return strings that are large enough to cause OutOfMemoryErrors on some platforms.

We do not call alias_method here as that would copy the content of to_s at this point to inspect (ie children would print out `loader_name` rather than their version of to_s if they chose to implement it).


200
201
202
# File 'lib/puppet/pops/loader/loader.rb', line 200

def inspect
  self.to_s
end

#load(type, name) ⇒ Object?

Produces the value associated with the given name if already loaded, or available for loading by this loader, one of its parents, or other loaders visible to this loader. This is the method an external party should use to “get” the named element.

An implementor of this method should first check if the given name is already loaded by self, or a parent loader, and if so return that result. If not, it should call `find` to perform the loading.

Parameters:

  • type (:Symbol)

    the type to load

  • name (String, Symbol)

    the name of the entity to load

Returns:

  • (Object, nil)

    the value or nil if not found


72
73
74
75
76
77
78
79
# File 'lib/puppet/pops/loader/loader.rb', line 72

def load(type, name)
  synchronize do
    result = load_typed(TypedName.new(type, name.to_s))
    if result
      result.value
    end
  end
end

#load_typed(typed_name) ⇒ NamedEntry?

Loads the given typed name, and returns a NamedEntry if found, else returns nil. This the same a `load`, but returns a NamedEntry with origin/value information.

Parameters:

  • typed_name (TypedName)
    • the type, name combination to lookup

Returns:

  • (NamedEntry, nil)

    the entry containing the loaded value, or nil if not found

Raises:

  • (NotImplementedError)

89
90
91
# File 'lib/puppet/pops/loader/loader.rb', line 89

def load_typed(typed_name)
  raise NotImplementedError, "Class #{self.class.name} must implement method #load_typed"
end

#loadablesObject

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.

A loader is by default a loader for all kinds of loadables. An implementation may override if it cannot load all kinds.


184
185
186
# File 'lib/puppet/pops/loader/loader.rb', line 184

def loadables
  LOADABLE_KINDS
end

#loaded_entry(typed_name, check_dependencies = false) ⇒ NamedEntry?

Returns an already loaded entry if one exists, or nil. This does not trigger loading of the given type/name.

Parameters:

  • typed_name (TypedName)
    • the type, name combination to lookup

  • check_dependencies (Boolean) (defaults to: false)
    • if dependencies should be checked in addition to here and parent

Returns:

  • (NamedEntry, nil)

    the entry containing the loaded value, or nil if not found

Raises:

  • (NotImplementedError)

101
102
103
# File 'lib/puppet/pops/loader/loader.rb', line 101

def loaded_entry(typed_name, check_dependencies = false)
  raise NotImplementedError, "Class #{self.class.name} must implement method #loaded_entry"
end

#parentObject

Returns the parent of the loader, or nil, if this is the top most loader. This implementation returns nil.


137
138
139
# File 'lib/puppet/pops/loader/loader.rb', line 137

def parent
  nil
end

#private_loaderObject

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.

Produces the private loader for loaders that have a one (the visibility given to loaded entities). For loaders that does not provide a private loader, self is returned.


145
146
147
# File 'lib/puppet/pops/loader/loader.rb', line 145

def private_loader
  self
end

#set_entry(type, name, value, origin = nil) ⇒ NamedEntry?

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.

Binds a value to a name. The name should not start with '::', but may contain multiple segments.

Parameters:

  • type (:Symbol)

    the type of the entity being set

  • name (String, Symbol)

    the name of the entity being set

  • origin (URI, #uri, String) (defaults to: nil)

    the origin of the set entity, a URI, or provider of URI, or URI in string form

Returns:

Raises:

  • (NotImplementedError)

165
166
167
# File 'lib/puppet/pops/loader/loader.rb', line 165

def set_entry(type, name, value, origin = nil)
  raise NotImplementedError.new
end

#synchronize(&block) ⇒ Object

Lock around a block This exists so some subclasses that are set up statically and don't actually load can override it


152
153
154
# File 'lib/puppet/pops/loader/loader.rb', line 152

def synchronize(&block)
  @environment.lock.synchronize(&block)
end

#to_sObject

A loader may want to implement its own version with more detailed information.


189
190
191
# File 'lib/puppet/pops/loader/loader.rb', line 189

def to_s
  loader_name
end