Sha256: 3fdbb793b97fba535a042bb91312c340a862ba1a78b4e89ea1d748971a4f2848

Contents?: true

Size: 1.9 KB

Versions: 121

Compression:

Stored size: 1.9 KB

Contents

# A restricting tag for fact resolution mechanisms.  The tag must be true
# for the resolution mechanism to be suitable.

require 'facter/util/values'

class Facter::Util::Confine
  attr_accessor :fact, :values

  include Facter::Util::Values

  # Add the restriction.  Requires the fact name, an operator, and the value
  # we're comparing to.
  #
  # @param fact [Symbol] Name of the fact
  # @param values [Array] One or more values to match against.
  #   They can be any type that provides a === method.
  # @param block [Proc] Alternatively a block can be supplied as a check.  The fact
  #   value will be passed as the argument to the block.  If the block returns
  #   true then the fact will be enabled, otherwise it will be disabled.
  def initialize(fact = nil, *values, &block)
    raise ArgumentError, "The fact name must be provided" unless fact or block_given?
    if values.empty? and not block_given?
      raise ArgumentError, "One or more values or a block must be provided"
    end
    @fact = fact
    @values = values
    @block = block
  end

  def to_s
    return @block.to_s if @block
    return "'%s' '%s'" % [@fact, @values.join(",")]
  end

  # Evaluate the fact, returning true or false.
  # if we have a block paramter then we only evaluate that instead
  def true?
    if @block and not @fact then
      begin
        return !! @block.call
      rescue StandardError => error
        Facter.debug "Confine raised #{error.class} #{error}"
        return false
      end
    end

    unless fact = Facter[@fact]
      Facter.debug "No fact for %s" % @fact
      return false
    end
    value = convert(fact.value)

    return false if value.nil?

    if @block then
      begin
        return !! @block.call(value)
      rescue StandardError => error
        Facter.debug "Confine raised #{error.class} #{error}"
        return false
      end
    end

    return @values.any? do |v| convert(v) === value end
  end
end

Version data entries

121 entries across 121 versions & 2 rubygems

Version Path
bolt-0.17.0 vendored/facter/lib/facter/util/confine.rb
bolt-0.16.4 vendored/facter/lib/facter/util/confine.rb
bolt-0.16.3 vendored/facter/lib/facter/util/confine.rb
bolt-0.16.2 vendored/facter/lib/facter/util/confine.rb
bolt-0.16.1 vendored/facter/lib/facter/util/confine.rb
bolt-0.16.0 vendored/facter/lib/facter/util/confine.rb
bolt-0.15.0 vendored/facter/lib/facter/util/confine.rb
bolt-0.14.0 vendored/facter/lib/facter/util/confine.rb
bolt-0.13.0 vendored/facter/lib/facter/util/confine.rb
bolt-0.12.0 vendored/facter/lib/facter/util/confine.rb
bolt-0.11.0 vendored/facter/lib/facter/util/confine.rb
bolt-0.10.0 vendored/facter/lib/facter/util/confine.rb
bolt-0.9.0 vendored/facter/lib/facter/util/confine.rb
bolt-0.8.0 vendored/facter/lib/facter/util/confine.rb
bolt-0.7.0 vendored/facter/lib/facter/util/confine.rb
bolt-0.6.1 vendored/facter/lib/facter/util/confine.rb
bolt-0.6.0 vendored/facter/lib/facter/util/confine.rb
bolt-0.5.1 vendored/facter/lib/facter/util/confine.rb
bolt-0.5.0 vendored/facter/lib/facter/util/confine.rb
facter-2.5.1 lib/facter/util/confine.rb