Class: RFacter::Util::Resolution Private

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Core::Resolvable, Core::Suitable
Defined in:
lib/rfacter/util/resolution.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.

This represents a fact resolution. A resolution is a concrete implementation of a fact. A single fact can have many resolutions and the correct resolution will be chosen at runtime. Each time Facter.add is called, a new resolution is created and added to the set of resolutions for the fact named in the call. Each resolution has a weight, which defines its priority over other resolutions, and a set of confinements, which defines the conditions under which it will be chosen. All confinements must be satisfied for a fact to be considered suitable.

Since:

  • 0.1.0

Instance Attribute Summary collapse

Attributes included from Core::Suitable

#weight

Attributes included from Core::Resolvable

#timeout

Instance Method Summary collapse

Methods included from Core::Suitable

#confine, #has_weight, #suitable?

Methods included from Core::Resolvable

#flush, #limit, #on_flush, #value

Constructor Details

#initialize(name, fact, config: RFacter::Config.config, **options) ⇒ void

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.

Create a new resolution mechanism.

Parameters:

  • name (String)

    The name of the resolution.

Since:

  • 0.1.0



54
55
56
57
58
59
60
61
62
# File 'lib/rfacter/util/resolution.rb', line 54

def initialize(name, fact, config: RFacter::Config.config, **options)
  @name = name
  @fact = fact
  @config = config
  @confines = []
  @value = nil
  @timeout = 0
  @weight = nil
end

Instance Attribute Details

#codeObject

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



26
27
28
# File 'lib/rfacter/util/resolution.rb', line 26

def code
  @code
end

#factRFacter::Util::Fact (readonly)

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:

Since:

  • 0.1.0



40
41
42
# File 'lib/rfacter/util/resolution.rb', line 40

def fact
  @fact
end

#nameString

The name of this resolution. The resolution name should be unique with respect to the given fact.

Returns:

  • (String)


36
37
38
# File 'lib/rfacter/util/resolution.rb', line 36

def name
  @name
end

#value=(value) ⇒ Object (writeonly)

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



27
28
29
# File 'lib/rfacter/util/resolution.rb', line 27

def value=(value)
  @value = value
end

Instance Method Details

#evaluate(&block) ⇒ void

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.

This method returns an undefined value.

Evaluate the given block in the context of this resolution. If a block has already been evaluated emit a warning to that effect.

Since:

  • 0.1.0



72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/rfacter/util/resolution.rb', line 72

def evaluate(&block)
  if @last_evaluated
    msg = "Already evaluated #{@name}"
    msg << " at #{@last_evaluated}" if msg.is_a? String
    msg << ", reevaluating anyways"
    logger.warn msg
  end

  instance_eval(&block)

  @last_evaluated = block.source_location.join(':')
end

#exec(command) ⇒ Object

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



46
47
48
# File 'lib/rfacter/util/resolution.rb', line 46

def exec(command)
  ::RFacter::DSL::Facter::Core::Execution.exec(command)
end

#resolution_typeObject

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



64
65
66
# File 'lib/rfacter/util/resolution.rb', line 64

def resolution_type
  :simple
end

#set_options(options) ⇒ Object

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



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/rfacter/util/resolution.rb', line 85

def set_options(options)
  if options[:name]
    @name = options.delete(:name)
  end

  if options.has_key?(:value)
    @value = options.delete(:value)
  end

  if options.has_key?(:timeout)
    @timeout = options.delete(:timeout)
  end

  if options.has_key?(:weight)
    @weight = options.delete(:weight)
  end

  if not options.keys.empty?
    raise ArgumentError, "Invalid resolution options #{options.keys.inspect}"
  end
end

#setcode(string) ⇒ void #setcode(&block) ⇒ void

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.

This method returns an undefined value.

Sets the code block or external program that will be evaluated to get the value of the fact.

Overloads:

  • #setcode(string) ⇒ void

    Sets an external program to call to get the value of the resolution

    Parameters:

    • string (String)

      the external program to run to get the value

  • #setcode(&block) ⇒ void

    Sets the resolution’s value by evaluating a block at runtime

    Parameters:

    • block (Proc)

      The block to determine the resolution’s value. This block is run when the fact is evaluated. Errors raised from inside the block are rescued and printed to stderr.

Since:

  • 0.1.0



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/rfacter/util/resolution.rb', line 122

def setcode(string = nil, &block)
  if string
    @code = Proc.new do
      output = exec(string)
      if output.nil? or output.empty?
        nil
      else
        output
      end
    end
  elsif block_given?
    @code = block
  else
    raise ArgumentError, "You must pass either code or a block"
  end
end

#which(command) ⇒ Object

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
# File 'lib/rfacter/util/resolution.rb', line 42

def which(command)
  ::RFacter::DSL::Facter::Core::Execution.which(command)
end