summaryrefslogtreecommitdiffstats
path: root/lib/puppet/parser/ast/hostclass.rb
blob: 65dd27542a2171deb53043e927232bc3c32e0f29 (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
require 'puppet/parser/ast/component'

class Puppet::Parser::AST
    # The code associated with a class.  This is different from components
    # in that each class is a singleton -- only one will exist for a given
    # node.
    class HostClass < AST::Component
        @name = :class

        # Are we a child of the passed class?  Do a recursive search up our
        # parentage tree to figure it out.
        def child_of?(klass)
            return false unless self.parentclass

            if klass == self.parentclass
                return true
            else
                return self.parentclass.child_of?(klass)
            end
        end

        # Evaluate the code associated with this class.
        def evaluate(hash)
            scope = hash[:scope]
            args = hash[:arguments]

            # Verify that we haven't already been evaluated
            if scope.setclass?(self)
                Puppet.debug "%s class already evaluated" % @type
                return nil
            end

            if @parentclass
                if pklass = self.parentclass
                    pklass.safeevaluate :scope => scope

                    scope = parent_scope(scope, pklass)
                else
                    parsefail "Could not find class %s" % @parentclass
                end
            end

            unless hash[:nosubscope]
                scope = subscope(scope)
            end

            # Set the class before we do anything else, so that it's set
            # during the evaluation and can be inspected.
            scope.setclass(self)

            # Now evaluate our code, yo.
            if self.code
                return self.code.evaluate(:scope => scope)
            else
                return nil
            end
        end

        def initialize(hash)
            @parentclass = nil
            super
        end

        def parent_scope(scope, klass)
            if s = scope.class_scope(klass)
                return s
            else
                raise Puppet::DevError, "Could not find scope for %s" % klass.fqname
            end
        end
    end
end

# $Id$