Class: RFacter::Util::Confine Private

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Values
Defined in:
lib/rfacter/util/confine.rb

Overview

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

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

Since:

  • 0.1.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Values

convert, deep_freeze, deep_merge, flatten_structure

Constructor Details

#initialize(fact = nil, *values, config: RFacter::Config.config, **options, &block) ⇒ Confine

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.

Add the restriction. Requires the fact name, an operator, and the value we’re comparing to.

Parameters:

  • fact (Symbol) (defaults to: nil)

    Name of the fact

  • values (Array)

    One or more values to match against. They can be any type that provides a === method.

  • 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.

Raises:

  • (ArgumentError)

Since:

  • 0.1.0



31
32
33
34
35
36
37
38
39
40
# File 'lib/rfacter/util/confine.rb', line 31

def initialize(fact = nil, *values, config: RFacter::Config.config, **options, &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
  @config = config
  @block = block
end

Instance Attribute Details

#factObject

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.

Since:

  • 0.1.0



18
19
20
# File 'lib/rfacter/util/confine.rb', line 18

def fact
  @fact
end

#valuesObject

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.

Since:

  • 0.1.0



18
19
20
# File 'lib/rfacter/util/confine.rb', line 18

def values
  @values
end

Instance Method Details

#to_sObject

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.

Since:

  • 0.1.0



42
43
44
45
# File 'lib/rfacter/util/confine.rb', line 42

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

#true?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.

Evaluate the fact, returning true or false. if we have a block paramter then we only evaluate that instead

Returns:

  • (Boolean)

Since:

  • 0.1.0



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/rfacter/util/confine.rb', line 49

def true?
  if @block and not @fact then
    begin
      return !! @block.call
    rescue StandardError => error
      logger.debug "Confine raised #{error.class} #{error}"
      return false
    end
  end

  unless fact = RFacter::DSL::Facter[@fact]
    logger.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
      logger.debug "Confine raised #{error.class} #{error}"
      return false
    end
  end

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