Class: LegacyFacter::Util::Confine
- Inherits:
-
Object
- Object
- LegacyFacter::Util::Confine
- Includes:
- Values
- Defined in:
- lib/facter/custom_facts/util/confine.rb
Instance Attribute Summary collapse
-
#fact ⇒ Object
Returns the value of attribute fact.
-
#values ⇒ Object
Returns the value of attribute values.
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.
22 23 24 25 26 27 28 29 |
# File 'lib/facter/custom_facts/util/confine.rb', line 22 def initialize(fact = nil, *values, &block) raise ArgumentError, 'The fact name must be provided' unless fact || block_given? raise ArgumentError, 'One or more values or a block must be provided' if values.empty? && !block_given? @fact = fact @values = values @block = block end |
Instance Attribute Details
#fact ⇒ Object
Returns the value of attribute fact.
9 10 11 |
# File 'lib/facter/custom_facts/util/confine.rb', line 9 def fact @fact end |
#values ⇒ Object
Returns the value of attribute values.
9 10 11 |
# File 'lib/facter/custom_facts/util/confine.rb', line 9 def values @values end |
Instance Method Details
#to_s ⇒ Object
31 32 33 34 35 |
# File 'lib/facter/custom_facts/util/confine.rb', line 31 def to_s return @block.to_s if @block format("'%<fact>s' '%<values>s'", fact: @fact, values: @values.join(',')) end |
#true? ⇒ Boolean
Evaluate the fact, returning true or false. if we have a block parameter then we only evaluate that instead
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 66 67 68 69 70 71 72 |
# File 'lib/facter/custom_facts/util/confine.rb', line 39 def true? if @block && !@fact begin return !!@block.call rescue StandardError => e log.debug "Confine raised #{e.class} #{e}" return false end end unless (fact = Facter[@fact]) log.debug format('No fact for %<fact>s', fact: @fact) return false end value = convert(fact.value) return false if value.nil? # We call the block with both the downcased and raw fact value for # backwards-compatibility. if @block begin return !!@block.call(value) || !!@block.call(fact.value) # rubocop:disable Style/DoubleNegation rescue StandardError => e log.debug "Confine raised #{e.class} #{e}" return false end end # we're intentionally using case equality (triple equals) because we # support matching against anything that supports === such as ranges, # regular expressions, etc @values.any? { |v| convert(v) === value } # rubocop:disable Style/CaseEquality end |