summaryrefslogtreecommitdiffstats
path: root/lib/puppet/util
diff options
context:
space:
mode:
authorluke <luke@980ebf18-57e1-0310-9a29-db15c13687c0>2007-03-19 08:15:36 +0000
committerluke <luke@980ebf18-57e1-0310-9a29-db15c13687c0>2007-03-19 08:15:36 +0000
commit5b2ffbcb5d2e4113efec9c8ba882c5506d53c242 (patch)
treeb4127af05cf5682f38fbca928c9fb53e0b42f52b /lib/puppet/util
parent80dac92b3a5ebd2cb8904505845d63759b5cebb3 (diff)
downloadpuppet-5b2ffbcb5d2e4113efec9c8ba882c5506d53c242.tar.gz
puppet-5b2ffbcb5d2e4113efec9c8ba882c5506d53c242.tar.xz
puppet-5b2ffbcb5d2e4113efec9c8ba882c5506d53c242.zip
Adding provider features. Woot!
git-svn-id: https://reductivelabs.com/svn/puppet/trunk@2313 980ebf18-57e1-0310-9a29-db15c13687c0
Diffstat (limited to 'lib/puppet/util')
-rw-r--r--lib/puppet/util/provider_features.rb108
1 files changed, 108 insertions, 0 deletions
diff --git a/lib/puppet/util/provider_features.rb b/lib/puppet/util/provider_features.rb
new file mode 100644
index 000000000..c378ff805
--- /dev/null
+++ b/lib/puppet/util/provider_features.rb
@@ -0,0 +1,108 @@
+# Provides feature definitions.
+module Puppet::Util::ProviderFeatures
+ class ProviderFeature
+ require 'puppet/util/methodhelper'
+ require 'puppet/util'
+ include Puppet::Util
+ include Puppet::Util::MethodHelper
+ attr_accessor :name, :docs, :methods
+ def initialize(name, docs, hash)
+ self.name = symbolize(name)
+ self.docs = docs
+ hash = symbolize_options(hash)
+ set_options(hash)
+ end
+ end
+
+ # Define one or more features. At a minimum, features require a name
+ # and docs, and at this point they should also specify a list of methods
+ # required to determine if the feature is present.
+ def feature(name, docs, hash)
+ @features ||= {}
+ if @features.include?(name)
+ raise Puppet::DevError, "Feature %s is already defined" % name
+ end
+ begin
+ obj = ProviderFeature.new(name, docs, hash)
+ @features[obj.name] = obj
+ rescue ArgumentError => detail
+ error = ArgumentError.new(
+ "Could not create feature %s: %s" % [name, detail]
+ )
+ error.set_backtrace(detail.backtrace)
+ raise error
+ end
+ end
+
+ # Return a hash of all feature documentation.
+ def featuredocs
+ str = ""
+ @features ||= {}
+ return nil if @features.empty?
+ @features.each do |name, feature|
+ doc = feature.docs.gsub(/\n\s+/, " ")
+ str += " - **%s**: %s\n" % [name, doc]
+ end
+ str
+ end
+
+ # Generate a module that sets up the boolean methods to test for given
+ # features.
+ def feature_module
+ unless defined? @feature_module
+ @features ||= {}
+ @feature_module = ::Module.new
+ const_set("FeatureModule", @feature_module)
+ features = @features
+ # Create a feature? method that can be passed a feature name and
+ # determine if the feature is present.
+ @feature_module.send(:define_method, :feature?) do |name|
+ method = name.to_s + "?"
+ if respond_to?(method) and send(method)
+ return true
+ else
+ return false
+ end
+ end
+
+ # Create a method that will list all functional features.
+ @feature_module.send(:define_method, :features) do
+ return false unless defined?(features)
+ features.keys.find_all { |n| feature?(n) }.sort { |a,b|
+ a.to_s <=> b.to_s
+ }
+ end
+
+ # Create a boolean method for each feature so you can test them
+ # individually as you might need.
+ @features.each do |name, feature|
+ method = name.to_s + "?"
+ @feature_module.send(:define_method, method) do
+ set = nil
+ feature.methods.each do |m|
+ if is_a?(Class)
+ unless public_method_defined?(m)
+ set = false
+ break
+ end
+ else
+ unless respond_to?(m)
+ set = false
+ break
+ end
+ end
+ end
+
+ if set.nil?
+ true
+ else
+ false
+ end
+ end
+ end
+ end
+ @feature_module
+ end
+end
+
+# $Id$