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

Constants included from POSIX

POSIX::LOCALE_ENV_VARS, POSIX::USER_ENV_VARS

Constants included from SymbolicFileMode

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

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 POSIX

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

Methods included from SymbolicFileMode

#normalize_symbolic_mode, #symbolic_mode_to_int, #valid_symbolic_mode?

Instance Method Details

#instance_docs(type) ⇒ Object

Collect the docs for all of our instances.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/puppet/util/instance_loader.rb', line 36

def instance_docs(type)
  docs = ""

  # Load all instances.
  instance_loader(type).loadall

  # Use this method so they all get loaded
  loaded_instances(type).sort { |a,b| a.to_s <=> b.to_s }.each do |name|
    mod = self.loaded_instance(name)
    docs << "#{name}\n#{"-" * name.to_s.length}\n"

    docs << Puppet::Util::Docs.scrub(mod.doc) << "\n\n"
  end

  docs
end

#instance_hash(type) ⇒ Object

Return the instance hash for our type.



54
55
56
# File 'lib/puppet/util/instance_loader.rb', line 54

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

#instance_load(type, path, options = {}) ⇒ 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, options = {})
  @autoloaders ||= {}
  @instances ||= {}
  type = type.intern
  @instances[type] = {}
  @autoloaders[type] = Puppet::Util::Autoload.new(self, path, options)

  # 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.



59
60
61
# File 'lib/puppet/util/instance_loader.rb', line 59

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

#instance_loading?(type) ⇒ Boolean

Are we instance-loading this type?

Returns:

  • (Boolean)


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.



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/puppet/util/instance_loader.rb', line 64

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)
      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