Class: Puppet::Parameter::ValueCollection

Inherits:
Object
  • Object
show all
Defined in:
lib/vendor/puppet/parameter/value_collection.rb

Overview

A collection of values and regexes, used for specifying what values are allowed in a given parameter.

Instance Method Summary collapse

Constructor Details

#initializeValueCollection

Returns a new instance of ValueCollection.



42
43
44
45
46
47
48
49
50
# File 'lib/vendor/puppet/parameter/value_collection.rb', line 42

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

Instance Method Details

#aliasvalue(name, other) ⇒ Object



7
8
9
10
11
12
13
14
# File 'lib/vendor/puppet/parameter/value_collection.rb', line 7

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

  value.alias(name)
end

#docObject

Return a doc string for all of the values in this parameter/property.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/vendor/puppet/parameter/value_collection.rb', line 17

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?
          "`#{value.name}` (also called `#{aliases.join(", ")}`)"
        else
          "`#{value.name}`"
        end
      end.join(", ") + "."
    end

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

  @doc
end

#empty?Boolean

Does this collection contain any value definitions?

Returns:

  • (Boolean)


38
39
40
# File 'lib/vendor/puppet/parameter/value_collection.rb', line 38

def empty?
  @values.empty?
end

#match?(test_value) ⇒ Boolean

Can we match a given value?

Returns:

  • (Boolean)


53
54
55
56
57
58
59
60
61
# File 'lib/vendor/puppet/parameter/value_collection.rb', line 53

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

#munge(value) ⇒ Object

If the specified value is allowed, then munge appropriately.



64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/vendor/puppet/parameter/value_collection.rb', line 64

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

#newvalue(name, options = {}, &block) ⇒ Object

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:

  • :event: The event that should be returned when this value is set.

  • :call: 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).



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/vendor/puppet/parameter/value_collection.rb', line 89

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

  value.method ||= "set_#{value.name}" if block_given? and ! value.regex?

  value
end

#newvalues(*names) ⇒ Object

Define one or more new values for our parameter.



111
112
113
# File 'lib/vendor/puppet/parameter/value_collection.rb', line 111

def newvalues(*names)
  names.each { |name| newvalue(name) }
end

#regexesObject



115
116
117
# File 'lib/vendor/puppet/parameter/value_collection.rb', line 115

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

#validate(value) ⇒ Object

Verify that the passed value is valid.



120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/vendor/puppet/parameter/value_collection.rb', line 120

def validate(value)
  return if empty?

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

    str += "Valid values are #{values.join(", ")}. " unless values.empty?

    str += "Valid values match #{regexes.join(", ")}." unless regexes.empty?

    raise ArgumentError, str
  end
end

#value(name) ⇒ Object

Return a single value instance.



135
136
137
# File 'lib/vendor/puppet/parameter/value_collection.rb', line 135

def value(name)
  @values[name]
end

#valuesObject

Return the list of valid values.



140
141
142
# File 'lib/vendor/puppet/parameter/value_collection.rb', line 140

def values
  @strings.collect { |s| s.name }
end