Class: RFacter::Util::Fact Private

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/rfacter/util/fact.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 class represents a fact. Each fact has a name and multiple resolutions.

Create facts using Facter.add

Since:

  • 0.1.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

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.

Creates a new fact, with no resolution mechanisms. See DSL::Facter.add for the public API for creating facts.

Parameters:

  • name (String)

    the fact name

  • options (Hash)

    optional parameters

Since:

  • 0.1.0



29
30
31
32
33
34
35
36
37
# File 'lib/rfacter/util/fact.rb', line 29

def initialize(name, config: RFacter::Config.config, **options)
  @name = name.to_s.downcase.intern
  @config = config

  @resolves = []
  @searching = false

  @value = nil
end

Instance Attribute Details

#nameString

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.

The name of the fact

Returns:

  • (String)

Since:

  • 0.1.0



23
24
25
# File 'lib/rfacter/util/fact.rb', line 23

def name
  @name
end

Instance Method Details

#add(options = {}, &block) ⇒ RFacter::Util::Resolution

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.

Adds a new resolution. This requires a block, which will then be evaluated in the context of the new resolution.

Parameters:

  • options (Hash) (defaults to: {})

    A hash of options to set on the resolution

Returns:

Since:

  • 0.1.0



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

def add(options = {}, &block)
  define_resolution(nil, options, &block)
end

#define_resolution(resolution_name, options = {}, &block) ⇒ RFacter::Util::Resolution

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.

Define a new named resolution or return an existing resolution with the given name.

Parameters:

  • resolution_name (String)

    The name of the resolve to define or look up

  • options (Hash) (defaults to: {})

    A hash of options to set on the resolution

Returns:

Since:

  • 0.1.0



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/rfacter/util/fact.rb', line 56

def define_resolution(resolution_name, options = {}, &block)

  resolution_type = options.delete(:type) || :simple

  resolve = create_or_return_resolution(resolution_name, resolution_type)

  resolve.set_options(options) unless options.empty?
  resolve.evaluate(&block) if block

  resolve
rescue => e
  logger.log_exception(e, "Unable to add resolve #{resolution_name.inspect} for fact #{@name}: #{e.message}")
end

#flushvoid

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.

Flushes any cached values.

Since:

  • 0.1.0



85
86
87
88
# File 'lib/rfacter/util/fact.rb', line 85

def flush
  @resolves.each { |r| r.flush }
  @value = nil
end

#resolution(name) ⇒ RFacter::Util::Resolution?

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.

Retrieve an existing resolution by name

Parameters:

  • name (String)

Returns:

Since:

  • 0.1.0



76
77
78
79
80
# File 'lib/rfacter/util/fact.rb', line 76

def resolution(name)
  return nil if name.nil?

  @resolves.find { |resolve| resolve.name == name }
end

#valueObject

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 value for this fact. This searches all resolutions by suitability and weight (see Resolution). If no suitable resolution is found, it returns nil.

Since:

  • 0.1.0



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/rfacter/util/fact.rb', line 93

def value
  return @value if @value

  if @resolves.empty?
    logger.debug("No resolves for #{@name}")
    return nil
  end

  searching do

    suitable_resolutions = sort_by_weight(find_suitable_resolutions(@resolves))
    @value = find_first_real_value(suitable_resolutions)

    announce_when_no_suitable_resolution(suitable_resolutions)
    announce_when_no_value_found(@value)

    @value
  end
end