summaryrefslogtreecommitdiffstats
path: root/lib/puppet/util/classgen.rb
blob: 9c7e571f685579d44010356289850e2642890d47 (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
module Puppet
    class ConstantAlreadyDefined < Error; end
    class SubclassAlreadyDefined < Error; end
end

module Puppet::Util::ClassGen
    include Puppet::Util::MethodHelper
    include Puppet::Util

    # Create a new subclass.  Valid options are:
    # * <tt>:array</tt>: An array of existing classes.  If specified, the new
    #   class is added to this array.
    # * <tt>:attributes</tt>: A hash of attributes to set before the block is
    #   evaluated.
    # * <tt>:block</tt>: The block to evaluate in the context of the class.
    #   You can also just pass the block normally, but it will still be evaluated
    #   with <tt>class_eval</tt>.
    # * <tt>:constant</tt>: What to set the constant as.  Defaults to the
    #   capitalized name.
    # * <tt>:hash</tt>: A hash of existing classes.  If specified, the new
    #   class is added to this hash, and it is also used for overwrite tests.
    # * <tt>:overwrite</tt>: Whether to overwrite an existing class.
    # * <tt>:parent</tt>: The parent class for the generated class.  Defaults to
    #   self.
    # * <tt>:prefix</tt>: The constant prefix.  Default to nothing; if specified,
    #   the capitalized name is appended and the result is set as the constant.
    def genclass(name, options = {}, &block)
        options = symbolize_options(options)
        parent = options[:parent] || self

        name = symbolize(name.to_s.downcase)

        # Create the class, with the correct name.
        klass = Class.new(parent) do
            @name = name
        end

        unless const = options[:constant]
            prefix = options[:prefix] || ""
            const = prefix + name2const(name)
        end

        if const_defined? const
            if options[:overwrite]
                Puppet.info "Redefining %s subclass %s" % [parent, name]
                remove_const(const)
            else
                raise Puppet::ConstantAlreadyDefined,
                    "Class %s is already defined in %s" % [const, parent]
            end
        end
        const_set(const, klass)

        # Initialize any necessary variables.
        if klass.respond_to? :initvars
            klass.initvars
        end

        if attrs = options[:attributes]
            attrs.each do |param, value|
                method = param.to_s + "="
                if klass.respond_to? method
                    klass.send(method, value)
                end
            end
        end

        if klass.respond_to? :preinit
            klass.preinit
        end

        block ||= options[:block]

        # Evaluate the passed block if there is one.  This should usually
        # define all of the work.
        if block
            klass.class_eval(&block)
        end

        if klass.respond_to? :postinit
            klass.postinit
        end

        # If we were told to stick it in a hash, then do so
        if hash = options[:hash]
            if hash.include? name and ! options[:overwrite]
                raise SubclassAlreadyDefined,
                    "Already a generated class named %s" % name
            end

            hash[name] = klass
        end

        # If we were told to stick it in a hash, then do so
        if array = options[:array]
            if (klass.respond_to? :name and
                            array.find { |c| c.name == name } and
                            ! options[:overwrite])
                raise SubclassAlreadyDefined,
                    "Already a generated class named %s" % name
            end

            array << klass
        end

        return klass
    end

    # Remove an existing class
    def rmclass(name, options)
        options = symbolize_options(options)
        const = name2const(name)
        retval = false
        if const_defined? const
            remove_const(const)
            retval = true
        end

        if hash = options[:hash] and hash.include? name
            hash.delete(name)
            retval = true
        end

        # Let them know whether we did actually delete a subclass.
        return retval
    end

    private

    def name2const(name)
        name.to_s.capitalize
    end
end

# $Id$