Sha256: 0970dd830cb83dcf147503e49cecb19481c400d5d9031b2fdf738a0713151722

Contents?: true

Size: 1.6 KB

Versions: 94

Compression:

Stored size: 1.6 KB

Contents

require 'puppet/util/autoload'
require 'puppet/util'

# A module that can easily autoload things for us.  Uses an instance
# of Puppet::Util::Autoload
module Puppet::Util::InstanceLoader
  include Puppet::Util

  # Are we instance-loading this type?
  def instance_loading?(type)
    defined?(@autoloaders) and @autoloaders.include?(type.intern)
  end

  # Define a new type of autoloading.
  def instance_load(type, path)
    @autoloaders ||= {}
    @instances ||= {}
    type = type.intern
    @instances[type] = {}
    @autoloaders[type] = Puppet::Util::Autoload.new(self, path)

    # Now define our new simple methods
    unless respond_to?(type)
      meta_def(type) do |name|
        loaded_instance(type, name)
      end
    end
  end

  # Return a list of the names of all instances
  def loaded_instances(type)
    @instances[type].keys
  end

  # Return the instance hash for our type.
  def instance_hash(type)
    @instances[type.intern]
  end

  # Return the Autoload object for a given type.
  def instance_loader(type)
    @autoloaders[type.intern]
  end

  # Retrieve an already-loaded instance, or attempt to load our instance.
  def loaded_instance(type, name)
    name = name.intern
    return nil unless instances = instance_hash(type)
    unless instances.include? name
      if instance_loader(type).load(name, Puppet.lookup(:current_environment))
        unless instances.include? name
          Puppet.warning(_("Loaded %{type} file for %{name} but %{type} was not defined") % { type: type, name: name })
          return nil
        end
      else
        return nil
      end
    end
    instances[name]
  end
end

Version data entries

94 entries across 94 versions & 2 rubygems

Version Path
puppet-6.4.5 lib/puppet/util/instance_loader.rb
puppet-6.4.5-x86-mingw32 lib/puppet/util/instance_loader.rb
puppet-6.4.5-x64-mingw32 lib/puppet/util/instance_loader.rb
puppet-6.4.5-universal-darwin lib/puppet/util/instance_loader.rb
puppet-6.4.4 lib/puppet/util/instance_loader.rb
puppet-6.4.4-x86-mingw32 lib/puppet/util/instance_loader.rb
puppet-6.4.4-x64-mingw32 lib/puppet/util/instance_loader.rb
puppet-6.4.4-universal-darwin lib/puppet/util/instance_loader.rb
puppet-6.4.3 lib/puppet/util/instance_loader.rb
puppet-6.4.3-x86-mingw32 lib/puppet/util/instance_loader.rb
puppet-6.4.3-x64-mingw32 lib/puppet/util/instance_loader.rb
puppet-6.4.3-universal-darwin lib/puppet/util/instance_loader.rb
puppet-6.0.10 lib/puppet/util/instance_loader.rb
puppet-6.0.10-x86-mingw32 lib/puppet/util/instance_loader.rb
puppet-6.0.10-x64-mingw32 lib/puppet/util/instance_loader.rb
puppet-6.0.10-universal-darwin lib/puppet/util/instance_loader.rb
puppet-6.6.0 lib/puppet/util/instance_loader.rb
puppet-6.6.0-x86-mingw32 lib/puppet/util/instance_loader.rb
puppet-6.6.0-x64-mingw32 lib/puppet/util/instance_loader.rb
puppet-6.6.0-universal-darwin lib/puppet/util/instance_loader.rb