Module: LegacyFacter::Core::Suitable

Included in:
Facter::Core::Aggregate, Facter::Util::Resolution
Defined in:
lib/facter/custom_facts/core/suitable.rb

Instance Method Summary collapse

Instance Method Details

#confine(confines) ⇒ void #confine(confines, &block) {|value| ... } ⇒ void #confine(&block) ⇒ void

This method returns an undefined value.

Sets the conditions for this resolution to be used. This method accepts multiple forms of arguments to determine suitability.

Overloads:

  • #confine(confines) ⇒ void

    Confine a fact to a specific fact value or values. This form takes a hash of fact names and values. Every fact must match the values given for that fact, otherwise this resolution will not be considered suitable. The values given for a fact can be an array, in which case the value of the fact must be in the array for it to match.

    Examples:

    Confining to Linux

    Facter.add(:powerstates) do
      # This resolution only makes sense on linux systems
      confine :kernel => "Linux"
      setcode do
        File.read('/sys/power/states')
      end
    end

    Parameters:

    • confines (Hash{String,Symbol=>String,Array<String>})

      set of facts identified by the hash keys whose fact value must match the argument value.

  • #confine(confines, &block) {|value| ... } ⇒ void

    Confine a fact to a block with the value of a specified fact yielded to the block.

    Examples:

    Confine the fact to a host with an ipaddress in a specific

    subnet
      confine :ipaddress do |addr|
        require 'ipaddr'
        IPAddr.new('192.168.0.0/16').include? addr
      end

    Parameters:

    • confines (String, Symbol)

      the fact name whose value should be yielded to the block

    • block (Proc)

      determines the suitability of the fact. If the block evaluates to ‘false` or `nil` then the confined fact will not be evaluated.

    Yields:

    • (value)

      the value of the fact identified by confines

  • #confine(&block) ⇒ void

    Confine a fact to a block. The fact will be evaluated only if the block evaluates to something other than ‘false` or `nil`.

    Examples:

    Confine the fact to systems with a specific file.

    confine { File.readable? '/bin/foo' }

    Parameters:

    • block (Proc)

      determines the suitability of the fact. If the block evaluates to ‘false` or `nil` then the confined fact will not be evaluated.



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/facter/custom_facts/core/suitable.rb', line 74

def confine(confines = nil, &block)
  case confines
  when Hash
    confines.each do |fact, values|
      @confines.push LegacyFacter::Util::Confine.new(fact, *values)
    end
  else
    if block
      if confines
        @confines.push LegacyFacter::Util::Confine.new(confines, &block)
      else
        @confines.push LegacyFacter::Util::Confine.new(&block)
      end
    end
  end
end

#has_weight(weight) ⇒ void

This method returns an undefined value.

Sets the weight of this resolution. If multiple suitable resolutions are found, the one with the highest weight will be used. If weight is not given, the number of confines set on a resolution will be used as its weight (so that the most specific resolution is used).

Parameters:

  • weight (Integer)

    the weight of this resolution



21
22
23
24
# File 'lib/facter/custom_facts/core/suitable.rb', line 21

def has_weight(weight)
  @weight = weight
  self
end

#suitable?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Is this resolution mechanism suitable on the system in question?

Returns:

  • (Boolean)


105
106
107
# File 'lib/facter/custom_facts/core/suitable.rb', line 105

def suitable?
  @confines.all?(&:true?)
end

#weightInteger

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the importance of this resolution. If the weight was not given, the number of confines is used instead (so that a more specific resolution wins over a less specific one).

Returns:

  • (Integer)

    the weight of this resolution



98
99
100
# File 'lib/facter/custom_facts/core/suitable.rb', line 98

def weight
  @weight || @confines.length
end