summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLuke Kanies <luke@madstop.com>2007-09-11 18:48:30 -0500
committerLuke Kanies <luke@madstop.com>2007-09-11 18:48:30 -0500
commit3b3065bc73e409874a9e6f1be755754fb2b226bf (patch)
treea166514550bc27731b8f06554fe93e889cd38f62
parent65c1501504dd7e9166176661f9ed9f80300954db (diff)
downloadpuppet-3b3065bc73e409874a9e6f1be755754fb2b226bf.tar.gz
puppet-3b3065bc73e409874a9e6f1be755754fb2b226bf.tar.xz
puppet-3b3065bc73e409874a9e6f1be755754fb2b226bf.zip
Refactoring the feature support so it loads libraries when a feature is asked about, rather than when it is defined.
-rw-r--r--lib/puppet/util/feature.rb53
-rwxr-xr-xtest/util/features.rb2
2 files changed, 31 insertions, 24 deletions
diff --git a/lib/puppet/util/feature.rb b/lib/puppet/util/feature.rb
index 30c38e286..2669d1ab1 100644
--- a/lib/puppet/util/feature.rb
+++ b/lib/puppet/util/feature.rb
@@ -16,8 +16,7 @@ class Puppet::Util::Feature
if self.class.respond_to?(method)
raise ArgumentError, "Feature %s is already defined" % name
end
-
- result = true
+
if block_given?
begin
result = yield
@@ -25,33 +24,21 @@ class Puppet::Util::Feature
warn "Failed to load feature test for %s: %s" % [name, detail]
result = false
end
- end
-
- if ary = options[:libs]
- ary = [ary] unless ary.is_a?(Array)
-
- ary.each do |lib|
- unless lib.is_a?(String)
- raise ArgumentError, "Libraries must be passed as strings not %s" % lib.class
- end
-
- begin
- require lib
- rescue Exception
- Puppet.debug "Failed to load library '%s' for feature '%s'" % [lib, name]
- result = false
- end
- end
+ @results[name] = result
end
meta_def(method) do
- result
+ unless @results.include?(name)
+ @results[name] = test(name, options)
+ end
+ @results[name]
end
end
# Create a new feature collection.
def initialize(path)
@path = path
+ @results = {}
@loader = Puppet::Util::Autoload.new(self, @path)
end
@@ -71,6 +58,28 @@ class Puppet::Util::Feature
return false
end
end
-end
-# $Id$
+ # Actually test whether the feature is present. We only want to test when
+ # someone asks for the feature, so we don't unnecessarily load
+ # files.
+ def test(name, options)
+ result = true
+ if ary = options[:libs]
+ ary = [ary] unless ary.is_a?(Array)
+
+ ary.each do |lib|
+ unless lib.is_a?(String)
+ raise ArgumentError, "Libraries must be passed as strings not %s" % lib.class
+ end
+
+ begin
+ require lib
+ rescue Exception
+ Puppet.debug "Failed to load library '%s' for feature '%s'" % [lib, name]
+ result = false
+ end
+ end
+ end
+ result
+ end
+end
diff --git a/test/util/features.rb b/test/util/features.rb
index 14e93c537..1e5858877 100755
--- a/test/util/features.rb
+++ b/test/util/features.rb
@@ -93,5 +93,3 @@ class TestFeatures < Test::Unit::TestCase
end
end
end
-
-# $Id$