Class: Facter::Util::Confine
- Inherits:
-
Object
- Object
- Facter::Util::Confine
- Includes:
- Values
- Defined in:
- lib/facter/util/confine.rb
Instance Attribute Summary collapse
Instance Method Summary collapse
-
#initialize(fact = nil, *values, &block) ⇒ Confine
constructor
Add the restriction.
- #to_s ⇒ Object
-
#true? ⇒ Boolean
Evaluate the fact, returning true or false.
Methods included from Values
convert, deep_freeze, deep_merge, flatten_structure
Constructor Details
#initialize(fact = nil, *values, &block) ⇒ Confine
Add the restriction. Requires the fact name, an operator, and the value we’re comparing to.
20 21 22 23 24 25 26 27 28 |
# File 'lib/facter/util/confine.rb', line 20 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 |
Instance Attribute Details
#fact ⇒ Object
7 8 9 |
# File 'lib/facter/util/confine.rb', line 7 def fact @fact end |
#values ⇒ Object
7 8 9 |
# File 'lib/facter/util/confine.rb', line 7 def values @values end |
Instance Method Details
#to_s ⇒ Object
30 31 32 33 |
# File 'lib/facter/util/confine.rb', line 30 def to_s return @block.to_s if @block return "'%s' '%s'" % [@fact, @values.join(",")] end |
#true? ⇒ Boolean
Evaluate the fact, returning true or false. if we have a block paramter then we only evaluate that instead
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 |
# File 'lib/facter/util/confine.rb', line 37 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 |