summaryrefslogtreecommitdiffstats
path: root/lib/puppet/util/variables.rb
blob: 7e04cf1c0e4c71986c59f85e318a0e78d35c3814 (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
module Puppet::Util::Variables
    def inithooks
        @instance_init_hooks.dup
    end

    def initvars
        return unless defined? @class_init_hooks
        self.inithooks.each do |var, value|
            if value.is_a?(Class)
                instance_variable_set("@" + var.to_s, value.new)
            else
                instance_variable_set("@" + var.to_s, value)
            end
        end
    end

    def instancevar(hash)
        @instance_init_hooks ||= {}

        unless method_defined?(:initvars)
            define_method(:initvars) do
                self.class.inithooks.each do |var, value|
                    if value.is_a?(Class)
                        instance_variable_set("@" + var.to_s, value.new)
                    else
                        instance_variable_set("@" + var.to_s, value)
                    end
                end
            end
        end
        hash.each do |var, value|
            raise("Already initializing %s" % var) if @instance_init_hooks[var]

            @instance_init_hooks[var] = value
        end
    end
end

# $Id$