Module: Puppet::Util::InstanceLoader

Includes:
Puppet::Util
Included in:
Indirector::Terminus, Reports, Reference
Defined in:
lib/puppet/util/instance_loader.rb

Overview

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

Constant Summary

Constants included from Puppet::Util

AbsolutePathPosix, AbsolutePathWindows, DEFAULT_POSIX_MODE, DEFAULT_WINDOWS_MODE, RFC_3986_URI_REGEX

Constants included from SymbolicFileMode

SymbolicFileMode::SetGIDBit, SymbolicFileMode::SetUIDBit, SymbolicFileMode::StickyBit, SymbolicFileMode::SymbolicMode, SymbolicFileMode::SymbolicSpecialToBit

Constants included from POSIX

POSIX::LOCALE_ENV_VARS, POSIX::USER_ENV_VARS

Instance Method Summary collapse

Methods included from Puppet::Util

absolute_path?, benchmark, chuser, clear_environment, default_env, deterministic_rand, deterministic_rand_int, exit_on_fail, get_env, get_environment, logmethods, merge_environment, path_to_uri, pretty_backtrace, replace_file, safe_posix_fork, set_env, symbolizehash, thinmark, uri_encode, uri_query_encode, uri_to_path, which, withenv, withumask

Methods included from SymbolicFileMode

#normalize_symbolic_mode, #symbolic_mode_to_int, #valid_symbolic_mode?

Methods included from POSIX

#get_posix_field, #gid, groups_of, #idfield, #methodbyid, #methodbyname, #search_posix_field, #uid

Instance Method Details

#instance_hash(type) ⇒ Object

Return the instance hash for our type.


36
37
38
# File 'lib/puppet/util/instance_loader.rb', line 36

def instance_hash(type)
  @instances[type.intern]
end

#instance_load(type, path) ⇒ Object

Define a new type of autoloading.


15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/puppet/util/instance_loader.rb', line 15

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

#instance_loader(type) ⇒ Object

Return the Autoload object for a given type.


41
42
43
# File 'lib/puppet/util/instance_loader.rb', line 41

def instance_loader(type)
  @autoloaders[type.intern]
end

#instance_loading?(type) ⇒ Boolean

Are we instance-loading this type?


10
11
12
# File 'lib/puppet/util/instance_loader.rb', line 10

def instance_loading?(type)
  defined?(@autoloaders) and @autoloaders.include?(type.intern)
end

#loaded_instance(type, name) ⇒ Object

Retrieve an already-loaded instance, or attempt to load our instance.


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/puppet/util/instance_loader.rb', line 46

def loaded_instance(type, name)
  name = name.intern
  instances = instance_hash(type)
  return nil unless instances
  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

#loaded_instances(type) ⇒ Object

Return a list of the names of all instances


31
32
33
# File 'lib/puppet/util/instance_loader.rb', line 31

def loaded_instances(type)
  @instances[type].keys
end