summaryrefslogtreecommitdiffstats
path: root/lib/puppet/feature.rb
blob: 55fab5d3cec2d8e32f9e843583fb59129ce56655 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#  Created by Luke Kanies on 2006-11-07.
#  Copyright (c) 2006. All rights reserved.

class Puppet::Feature
    # 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.
    def add(name, options = {})
        method = name.to_s + "?"
        if self.class.respond_to?(method)
            raise ArgumentError, "Feature %s is already defined" % name
        end
        
        result = true
        if block_given?
            begin
                result = yield
            rescue => detail
                if Puppet[:trace]
                    puts detail.backtrace
                    Puppet.err "Failed to load %s: %s" % [name, detail]
                end
                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
        end
        
        meta_def(method) do
            result
        end
    end
    
    # Create a new feature collection.
    def initialize(path)
        @path = path
    end
    
    def load
        loader = Puppet::Autoload.new(self, @path)
        loader.loadall
    end
end

# $Id$