summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rwxr-xr-xlib/puppet/network/handler/report.rb32
-rwxr-xr-xlib/puppet/util/instance_loader.rb74
2 files changed, 82 insertions, 24 deletions
diff --git a/lib/puppet/network/handler/report.rb b/lib/puppet/network/handler/report.rb
index 574970fc4..e21acb810 100755
--- a/lib/puppet/network/handler/report.rb
+++ b/lib/puppet/network/handler/report.rb
@@ -1,7 +1,10 @@
+require 'puppet/util/instance_loader'
+
# A simple server for triggering a new run on a Puppet client.
class Puppet::Network::Handler
class Report < Handler
extend Puppet::Util::ClassGen
+ extend Puppet::Util::InstanceLoader
module ReportBase
include Puppet::Util::Docs
@@ -20,8 +23,8 @@ class Puppet::Network::Handler
iface.add_method("string report(array)")
}
- @reports = {}
- @reportloader = Puppet::Util::Autoload.new(self, "puppet/reports")
+ # Set up autoloading and retrieving of reports.
+ autoload :report, 'puppet/reports'
class << self
attr_reader :hooks
@@ -31,7 +34,7 @@ class Puppet::Network::Handler
def self.newreport(name, options = {}, &block)
name = symbolize(name)
- mod = genmodule(name, :extend => ReportBase, :hash => @reports, :block => block)
+ mod = genmodule(name, :extend => ReportBase, :hash => instance_hash(:report), :block => block)
if options[:useyaml]
mod.useyaml = true
@@ -42,31 +45,12 @@ class Puppet::Network::Handler
end
end
- # Load a report.
- def self.report(name)
- name = name.intern if name.is_a? String
- unless @reports.include? name
- if @reportloader.load(name)
- unless @reports.include? name
- Puppet.warning(
- "Loaded report file for %s but report was not defined" %
- name
- )
- return nil
- end
- else
- return nil
- end
- end
- @reports[symbolize(name)]
- end
-
# Collect the docs for all of our reports.
def self.reportdocs
docs = ""
# Use this method so they all get loaded
- reports.sort { |a,b| a.to_s <=> b.to_s }.each do |name|
+ loaded_instances(:report).sort { |a,b| a.to_s <=> b.to_s }.each do |name|
mod = self.report(name)
docs += "%s\n%s\n" % [name, "-" * name.to_s.length]
@@ -78,7 +62,7 @@ class Puppet::Network::Handler
# List each of the reports.
def self.reports
- @reportloader.loadall
+ instance_loader(:report).loadall
@reports.keys
end
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$