diff options
author | luke <luke@980ebf18-57e1-0310-9a29-db15c13687c0> | 2007-05-07 18:59:40 +0000 |
---|---|---|
committer | luke <luke@980ebf18-57e1-0310-9a29-db15c13687c0> | 2007-05-07 18:59:40 +0000 |
commit | f42a755cc2e4bc77880dec9571ccd11f33f2737f (patch) | |
tree | 733e1c1681b9c325f269da81ab5faf1f58494a67 | |
parent | 53f16125761400df4fb1b99159c6b3bab8f5f397 (diff) | |
download | puppet-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
-rwxr-xr-x | lib/puppet/network/handler/report.rb | 32 | ||||
-rwxr-xr-x | lib/puppet/util/instance_loader.rb | 74 | ||||
-rwxr-xr-x | test/util/instance_loader.rb | 53 |
3 files changed, 135 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$ diff --git a/test/util/instance_loader.rb b/test/util/instance_loader.rb new file mode 100755 index 000000000..fa08d1bd6 --- /dev/null +++ b/test/util/instance_loader.rb @@ -0,0 +1,53 @@ +#!/usr/bin/env ruby + +$:.unshift("../lib").unshift("../../lib") if __FILE__ =~ /\.rb$/ + +require 'puppet' +require 'puppet/util/instance_loader' +require 'puppettest' + +class TestInstanceloader < Test::Unit::TestCase + include PuppetTest + + def setup + super + @loader = Class.new do + extend Puppet::Util::InstanceLoader + + def self.newstuff(name, value) + instance_hash(:stuff)[name] = value + end + end + + assert_nothing_raised("Could not create autoloader") do + @loader.autoload(:stuff, "puppet/stuff") + end + end + + # Make sure we correctly create our autoload instance. This covers the basics. + def test_autoload + # Make sure we can retrieve the loader + assert_instance_of(Puppet::Util::Autoload, @loader.instance_loader(:stuff), "Could not get autoloader") + + # Make sure we can get the instance hash + assert(@loader.instance_hash(:stuff), "Could not get instance hash") + + # Make sure it defines the instance retrieval method + assert(@loader.respond_to?(:stuff), "Did not define instance retrieval method") + end + + def test_loaded_instances + assert_equal([], @loader.loaded_instances(:stuff), "Incorrect loaded instances") + + @loader.newstuff(:testing, "a value") + + assert_equal([:testing], @loader.loaded_instances(:stuff), "Incorrect loaded instances") + + assert_equal("a value", @loader.loaded_instance(:stuff, :testing), "Got incorrect loaded instance") + end + + def test_autoloading + end +end + +# $Id$ |