Class: Facter::Util::Fact

Inherits:
Object
  • Object
show all
Defined in:
lib/facter/custom_facts/util/fact.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, 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 Facter.add for the public API for creating facts.

Parameters:

  • name (String)

    the fact name

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

    optional parameters



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/facter/custom_facts/util/fact.rb', line 32

def initialize(name, options = {})
  @name = LegacyFacter::Util::Normalization.normalize(name.to_s.downcase).intern

  @options = options.dup

  @resolves = []
  @searching = false
  @used_resolution_weight = 0

  @value = nil
end

Instance Attribute Details

#locationString (readonly)

The location from where fact is resolved

Returns:

  • (String)


14
15
16
# File 'lib/facter/custom_facts/util/fact.rb', line 14

def location
  @location
end

#nameString (readonly)

The name of the fact

Returns:

  • (String)


18
19
20
# File 'lib/facter/custom_facts/util/fact.rb', line 18

def name
  @name
end

#optionsObject

Fact options e.g. fact_type



21
22
23
# File 'lib/facter/custom_facts/util/fact.rb', line 21

def options
  @options
end

#used_resolution_weightObject

Weight of the resolution that was used to obtain the fact value



24
25
26
# File 'lib/facter/custom_facts/util/fact.rb', line 24

def used_resolution_weight
  @used_resolution_weight
end

Instance Method Details

#add(options = {}, &block) ⇒ Facter::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:



53
54
55
56
57
58
59
60
# File 'lib/facter/custom_facts/util/fact.rb', line 53

def add(options = {}, &block)
  @options = @options.merge(options)

  @location = options[:file]
  @location ||= block.source_location if block_given?

  define_resolution(nil, options, &block)
end

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

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:



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/facter/custom_facts/util/fact.rb', line 70

def define_resolution(resolution_name, options = {}, &block)
  resolution_type = options.delete(:type) || :simple

  resolve = create_or_return_resolution(resolution_name, resolution_type)

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

  resolve
rescue StandardError => e
  msg = "Unable to add resolve #{resolution_name.inspect} for fact '#{@name}': #{e.message}"
  msg += "\n#{e.backtrace.join("\n")}" if Options[:trace]
  log.error(msg, true)
  nil
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.



103
104
105
106
# File 'lib/facter/custom_facts/util/fact.rb', line 103

def flush
  @resolves.each(&:flush)
  @value = nil
end

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

Retrieve an existing resolution by name

Parameters:

  • name (String)

Returns:



92
93
94
95
96
# File 'lib/facter/custom_facts/util/fact.rb', line 92

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

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

#valueObject

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.



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/facter/custom_facts/util/fact.rb', line 113

def value
  return @value unless @value.nil?

  if @resolves.empty?
    log.debug format('No resolves for %<name>s', name: @name)
    return nil
  end

  searching do
    suitable_resolutions = sort_by_weight(find_suitable_resolutions(@resolves))

    Facter::Framework::Benchmarking::Timer.measure(@name) do
      @value = find_first_real_value(suitable_resolutions)
    end

    announce_when_no_suitable_resolution(suitable_resolutions)
    announce_when_no_value_found(@value)

    @value = resolve_value
  end

  @value
end