summaryrefslogtreecommitdiffstats
path: root/lib/puppet/util/instance_loader.rb
blob: bf4f9b77f9e4d95cde09ed349e1182c0b0f40e4a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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?(symbolize(type))
    end

    # Define a new type of autoloading.
    def instance_load(type, path, options = {})
        @autoloaders ||= {}
        @instances ||= {}
        type = symbolize(type)
        @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

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

    # Collect the docs for all of our instances.
    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 += "%s\n%s\n" % [name, "-" * name.to_s.length]

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

        docs
    end

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

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

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