Sha256: b11fc67c56de326708b645dbd19c767f5e21a9c938b5a76a68829f8673010248

Contents?: true

Size: 1.6 KB

Versions: 240

Compression:

Stored size: 1.6 KB

Contents

require '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

Version data entries

240 entries across 240 versions & 1 rubygems

Version Path
puppet-6.29.0 lib/puppet/confine/boolean.rb
puppet-6.29.0-x86-mingw32 lib/puppet/confine/boolean.rb
puppet-6.29.0-x64-mingw32 lib/puppet/confine/boolean.rb
puppet-6.29.0-universal-darwin lib/puppet/confine/boolean.rb
puppet-6.28.0 lib/puppet/confine/boolean.rb
puppet-6.28.0-x86-mingw32 lib/puppet/confine/boolean.rb
puppet-6.28.0-x64-mingw32 lib/puppet/confine/boolean.rb
puppet-6.28.0-universal-darwin lib/puppet/confine/boolean.rb
puppet-6.27.0 lib/puppet/confine/boolean.rb
puppet-6.27.0-x86-mingw32 lib/puppet/confine/boolean.rb
puppet-6.27.0-x64-mingw32 lib/puppet/confine/boolean.rb
puppet-6.27.0-universal-darwin lib/puppet/confine/boolean.rb
puppet-6.26.0 lib/puppet/confine/boolean.rb
puppet-6.26.0-x86-mingw32 lib/puppet/confine/boolean.rb
puppet-6.26.0-x64-mingw32 lib/puppet/confine/boolean.rb
puppet-6.26.0-universal-darwin lib/puppet/confine/boolean.rb
puppet-6.25.1 lib/puppet/confine/boolean.rb
puppet-6.25.1-x86-mingw32 lib/puppet/confine/boolean.rb
puppet-6.25.1-x64-mingw32 lib/puppet/confine/boolean.rb
puppet-6.25.1-universal-darwin lib/puppet/confine/boolean.rb