summaryrefslogtreecommitdiffstats
path: root/lib/puppet/util
diff options
context:
space:
mode:
authorluke <luke@980ebf18-57e1-0310-9a29-db15c13687c0>2007-05-07 18:59:40 +0000
committerluke <luke@980ebf18-57e1-0310-9a29-db15c13687c0>2007-05-07 18:59:40 +0000
commitf42a755cc2e4bc77880dec9571ccd11f33f2737f (patch)
tree733e1c1681b9c325f269da81ab5faf1f58494a67 /lib/puppet/util
parent53f16125761400df4fb1b99159c6b3bab8f5f397 (diff)
downloadpuppet-f42a755cc2e4bc77880dec9571ccd11f33f2737f.tar.gz
puppet-f42a755cc2e4bc77880dec9571ccd11f33f2737f.tar.xz
puppet-f42a755cc2e4bc77880dec9571ccd11f33f2737f.zip
Adding a module to abstract using Autoload to load and manage instances
git-svn-id: https://reductivelabs.com/svn/puppet/trunk@2476 980ebf18-57e1-0310-9a29-db15c13687c0
Diffstat (limited to 'lib/puppet/util')
-rwxr-xr-xlib/puppet/util/instance_loader.rb74
1 files changed, 74 insertions, 0 deletions
diff --git a/lib/puppet/util/instance_loader.rb b/lib/puppet/util/instance_loader.rb
new file mode 100755
index 000000000..3613b4e39
--- /dev/null
+++ b/lib/puppet/util/instance_loader.rb
@@ -0,0 +1,74 @@
+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
+ # Define a new type of autoloading.
+ def autoload(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 = ""
+
+ # 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)
+ 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
+
+# $Id$