summaryrefslogtreecommitdiffstats
path: root/lib/puppet/element.rb
blob: 3991160db5aed54cc8eef2a16d31eedc413a0106 (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# included so we can test object types
require 'puppet'

# the base class for both types and states
# very little functionality; basically just defines the interface
# and provides a few simple across-the-board functions like 'noop'
class Puppet::Element
    include Puppet
    attr_writer :noop

    class << self
        attr_accessor :doc, :nodoc
    end

    # all of our subclasses must respond to each of these methods...
    @@interface_methods = [
        :retrieve, :insync?, :sync, :evaluate
    ]

    # so raise an error if a method that isn't overridden gets called
    @@interface_methods.each { |method|
        self.send(:define_method,method) {
            raise Puppet::DevError, "%s has not overridden %s" %
                [self.class,method]
        }
    }

    # create instance methods for each of the log levels, too
    Puppet::Log.eachlevel { |level|
        define_method(level,proc { |args|
            if args.is_a?(Array)
                args = args.join(" ")
            end
            Puppet::Log.create(
                :level => level,
                :source => self,
                :message => args
            )
        })
    }

    # for testing whether we should actually do anything
    def noop
        unless defined? @noop
            @noop = false
        end
        return @noop || Puppet[:noop] || false
    end

    # return the full path to us, for logging and rollback
    # some classes (e.g., FileTypeRecords) will have to override this
    def path
        unless defined? @path
            if defined? @parent and @parent
                if self.is_a?(Puppet::Type::Component)
                    @path = [@parent.path, self.name]
                else
                    @path = [@parent.path, self.class.name.to_s + "=" + self.name]
                end
            else
                # The top-level name is always puppet[top], so we don't bother with
                # that.  And we don't add the hostname here, it gets added
                # in the log server thingy.
                if self.name == "puppet[top]"
                    @path = ["/"]
                else
                    # We assume that if we don't have a parent that we should not
                    # cache the path
                    Puppet.warning "%s has no parent" % self.name
                    @path = [self.class.name.to_s + "=" + self.name]
                end
            end
        end

        return @path.join("/")
    end

end

# $Id$