File: boolean.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 (45 lines) | stat: -rw-r--r-- 1,658 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
require_relative '../../puppet/confine'

# Common module for the Boolean confines. It currently
# contains just enough code to implement PUP-9336.
class Puppet::Confine
  module Boolean
    # Returns the passing value for the Boolean confine.
    def passing_value
      raise NotImplementedError, "The Boolean confine %{confine} must provide the passing value." % { confine: self.class.name }
    end

    # The Boolean confines 'true' and 'false' let the user specify
    # two types of values:
    #     * A lambda for lazy evaluation. This would be something like
    #         confine :true => lambda { true }
    #
    #     * A single Boolean value, or an array of Boolean values. This would
    #     be something like
    #         confine :true => true OR confine :true => [true, false, false, true]
    #
    # This override distinguishes between the two cases.
    def values
      # Note that Puppet::Confine's constructor ensures that @values
      # will always be an array, even if a lambda's passed in. This is
      # why we have the length == 1 check.
      unless @values.length == 1 && @values.first.respond_to?(:call)
        return @values
      end

      # We have a lambda. Here, we want to enforce "cache positive"
      # behavior, which is to cache the result _if_ it evaluates to
      # the passing value (i.e. the class name).

      return @cached_value unless @cached_value.nil?

      # Double negate to coerce the value into a Boolean
      calculated_value = !! @values.first.call
      if calculated_value == passing_value
        @cached_value = [calculated_value]
      end

      [calculated_value]
    end
  end
end