Class Puppet::Util::Feature
In: lib/puppet/util/feature.rb
Parent: Object

Created by Luke Kanies on 2006-11-07. Copyright (c) 2006. All rights reserved.

Methods

add   load   method_missing   new   test  

Attributes

path  [R] 

Public Class methods

Create a new feature collection.

[Source]

    # File lib/puppet/util/feature.rb, line 39
39:     def initialize(path)
40:         @path = path
41:         @results = {}
42:         @loader = Puppet::Util::Autoload.new(self, @path)
43:     end

Public Instance methods

Create a new feature test. You have to pass the feature name, and it must be unique. You can either provide a block that will get executed immediately to determine if the feature is present, or you can pass an option to determine it. Currently, the only supported option is ‘libs’ (must be passed as a symbol), which will make sure that each lib loads successfully.

[Source]

    # File lib/puppet/util/feature.rb, line 14
14:     def add(name, options = {})
15:         method = name.to_s + "?"
16:         if self.class.respond_to?(method)
17:             raise ArgumentError, "Feature %s is already defined" % name
18:         end
19: 
20:         if block_given?
21:             begin
22:                 result = yield
23:             rescue => detail
24:                 warn "Failed to load feature test for %s: %s" % [name, detail]
25:                 result = false
26:             end
27:             @results[name] = result
28:         end
29:         
30:         meta_def(method) do
31:             unless @results.include?(name)
32:                 @results[name] = test(name, options)
33:             end
34:             @results[name]
35:         end
36:     end

[Source]

    # File lib/puppet/util/feature.rb, line 45
45:     def load
46:         @loader.loadall
47:     end

[Source]

    # File lib/puppet/util/feature.rb, line 49
49:     def method_missing(method, *args)
50:         return super unless method.to_s =~ /\?$/
51: 
52:         feature = method.to_s.sub(/\?$/, '')
53:         @loader.load(feature)
54: 
55:         if respond_to?(method)
56:             return self.send(method)
57:         else
58:             return false
59:         end
60:     end

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.

[Source]

    # File lib/puppet/util/feature.rb, line 65
65:     def test(name, options)
66:         result = true
67:         if ary = options[:libs]
68:             ary = [ary] unless ary.is_a?(Array)
69:             
70:             ary.each do |lib|
71:                 unless lib.is_a?(String)
72:                     raise ArgumentError, "Libraries must be passed as strings not %s" % lib.class
73:                 end
74:             
75:                 begin
76:                     require lib
77:                 rescue Exception
78:                     Puppet.debug "Failed to load library '%s' for feature '%s'" % [lib, name]
79:                     result = false
80:                 end
81:             end
82:         end
83:         result
84:     end

[Validate]