summaryrefslogtreecommitdiffstats
path: root/lib/facter/util/confine.rb
blob: 4cbb32cc20edb7bbcea08085136d3ead5e04e0f8 (plain)
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
# 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.
    def initialize(fact, *values)
        raise ArgumentError, "The fact name must be provided" unless fact
        raise ArgumentError, "One or more values must be provided" if values.empty?
        @fact = fact
        @values = values
    end

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

    # Evaluate the fact, returning true or false.
    def true?
        unless fact = Facter[@fact]
            Facter.debug "No fact for %s" % @fact
            return false
        end
        value = convert(fact.value)

        return false if value.nil?

        @values.each do |v|
            v = convert(v)
            next unless v.class == value.class
            return true if value == v
        end
        return false
    end
end