File: value.rb

package info (click to toggle)
puppet-agent 7.23.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 19,092 kB
  • sloc: ruby: 245,074; sh: 456; makefile: 38; xml: 33
file content (91 lines) | stat: -rw-r--r-- 2,392 bytes parent folder | download
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
require_relative '../../puppet/parameter/value_collection'

# Describes an acceptable value for a parameter or property.
# An acceptable value is either specified as a literal value or a regular expression.
# @note this class should be used via the api methods in {Puppet::Parameter} and {Puppet::Property}
# @api private
#
class Puppet::Parameter::Value
  attr_reader :name, :options, :event
  attr_accessor :block, :method, :required_features, :invalidate_refreshes

  # Adds an alias for this value.
  # Makes the given _name_ be an alias for this acceptable value.
  # @param name [Symbol] the additional alias this value should be known as
  # @api private
  #
  def alias(name)
    @aliases << convert(name)
  end

  # @return [Array<Symbol>] Returns all aliases (or an empty array).
  # @api private
  #
  def aliases
    @aliases.dup
  end

  # Stores the event that our value generates, if it does so.
  # @api private
  #
  def event=(value)
    @event = convert(value)
  end

  # Initializes the instance with a literal accepted value, or a regular expression.
  # If anything else is passed, it is turned into a String, and then made into a Symbol.
  # @param name [Symbol, Regexp, Object] the value to accept, Symbol, a regular expression, or object to convert.
  # @api private
  #
  def initialize(name)
    if name.is_a?(Regexp)
      @name = name
    else
      # Convert to a string and then a symbol, so things like true/false
      # still show up as symbols.
      @name = convert(name)
    end

    @aliases = []
  end

  # Checks if the given value matches the acceptance rules (literal value, regular expression, or one
  # of the aliases.
  # @api private
  #
  def match?(value)
    if regex?
      return true if name =~ value.to_s
    else
      return(name == convert(value) ? true : @aliases.include?(convert(value)))
    end
  end

  # @return [Boolean] whether the accepted value is a regular expression or not.
  # @api private
  #
  def regex?
    @name.is_a?(Regexp)
  end

  private

  # A standard way of converting all of our values, so we're always
  # comparing apples to apples.
  # @api private
  #
  def convert(value)
    case value
    when Symbol, ''             # can't intern an empty string
      value
    when String
      value.intern
    when true
      :true
    when false
      :false
    else
      value.to_s.intern
    end
  end
end