summaryrefslogtreecommitdiffstats
path: root/lib/puppet/util/methodhelper.rb
blob: 32fca187710d70ec5604f8c78ea77fa3ff320c45 (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
# Where we store helper methods related to, um, methods.
module Puppet::Util::MethodHelper
    def requiredopts(*names)
        names.each do |name|
            if self.send(name).nil?
                devfail("%s is a required option for %s" % [name, self.class])
            end
        end
    end

    # Iterate over a hash, treating each member as an attribute.
    def set_options(options)
        options.each do |param,value|
            method = param.to_s + "="
            begin
                self.send(method, value)
            rescue NoMethodError
                raise ArgumentError, "Invalid parameter %s to object class %s" %
                        [param,self.class.to_s]
            end
        end
    end

    # Take a hash and convert all of the keys to symbols if possible.
    def symbolize_options(options)
        options.inject({}) do |hash, opts|
            if opts[0].respond_to? :intern
                hash[opts[0].intern] = opts[1]
            else
                hash[opts[0]] = opts[1]
            end
            hash
        end
    end
end