summaryrefslogtreecommitdiffstats
path: root/lib/puppet/parameter/value_collection.rb
blob: 436226ebe422fc1c105bf48baabb558ab511d69b (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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
require 'puppet/parameter/value'

# A collection of values and regexes, used for specifying
# what values are allowed in a given parameter.
class Puppet::Parameter::ValueCollection

    def aliasvalue(name, other)
        other = other.to_sym
        unless value = match?(other)
            raise Puppet::DevError, "Cannot alias nonexistent value %s" % other
        end

        value.alias(name)
    end

    # Return a doc string for all of the values in this parameter/property.
    def doc
        unless defined?(@doc)
            @doc = ""
            unless values.empty?
                @doc += "  Valid values are "
                @doc += @strings.collect do |value|
                    if aliases = value.aliases and ! aliases.empty?
                        "``%s`` (also called ``%s``)" % [value.name, aliases.join(", ")]
                    else
                        "``%s``" % value.name
                    end
                end.join(", ") + "."
            end

            unless regexes.empty?
                @doc += "  Values can match ``" + regexes.join("``, ``") + "``."
            end
        end

        @doc
    end

    # Does this collection contain any value definitions?
    def empty?
        @values.empty?
    end

    def initialize
        # We often look values up by name, so a hash makes more sense.
        @values = {}

        # However, we want to retain the ability to match values in order,
        # but we always prefer directly equality (i.e., strings) over regex matches.
        @regexes = []
        @strings = []
    end

    # Can we match a given value?
    def match?(test_value)
        # First look for normal values
        if value = @strings.find { |v| v.match?(test_value) }
            return value
        end

        # Then look for a regex match
        @regexes.find { |v| v.match?(test_value) }
    end

    # If the specified value is allowed, then munge appropriately.
    def munge(value)
        return value if empty?

        if instance = match?(value)
            if instance.regex?
                return value
            else
                return instance.name
            end
        else
            return value
        end
    end

    # Define a new valid value for a property.  You must provide the value itself,
    # usually as a symbol, or a regex to match the value.
    #
    # The first argument to the method is either the value itself or a regex.
    # The second argument is an option hash; valid options are:
    # * <tt>:event</tt>: The event that should be returned when this value is set.
    # * <tt>:call</tt>: When to call any associated block.  The default value
    #   is ``instead``, which means to call the value instead of calling the
    #   provider.  You can also specify ``before`` or ``after``, which will
    #   call both the block and the provider, according to the order you specify
    #   (the ``first`` refers to when the block is called, not the provider).
    def newvalue(name, options = {}, &block)
        value = Puppet::Parameter::Value.new(name)
        @values[value.name] = value
        if value.regex?
            @regexes << value
        else
            @strings << value
        end

        options.each { |opt, arg| value.send(opt.to_s + "=", arg) }
        if block_given?
            value.block = block
        else
            value.call = options[:call] || :none
        end

        if block_given? and ! value.regex?
            value.method ||= "set_" + value.name.to_s
        end

        value
    end

    # Define one or more new values for our parameter.
    def newvalues(*names)
        names.each { |name| newvalue(name) }
    end

    def regexes
        @regexes.collect { |r| r.name.inspect }
    end

    # Verify that the passed value is valid.
    def validate(value)
        return if empty?

        unless @values.detect { |name, v| v.match?(value) }
            str = "Invalid value %s. " % [value.inspect]

            unless values.empty?
                str += "Valid values are %s. " % values.join(", ")
            end

            unless regexes.empty?
                str += "Valid values match %s." % regexes.join(", ")
            end

            raise ArgumentError, str
        end
    end

    # Return a single value instance.
    def value(name)
        @values[name]
    end

    # Return the list of valid values.
    def values
        @strings.collect { |s| s.name }
    end
end