Class: Facter::Util::Fact
- Inherits:
-
Object
- Object
- Facter::Util::Fact
- Defined in:
- lib/facter/custom_facts/util/fact.rb
Instance Attribute Summary collapse
- #ldapname ⇒ String deprecated Deprecated.
-
#location ⇒ String
readonly
The location from where fact is resolved.
-
#name ⇒ String
readonly
The name of the fact.
-
#options ⇒ Object
Fact options e.g.
-
#used_resolution_weight ⇒ Object
Weight of the resolution that was used to obtain the fact value.
Instance Method Summary collapse
-
#add(options = {}, &block) ⇒ Facter::Util::Resolution
private
Adds a new resolution.
-
#define_resolution(resolution_name, options = {}, &block) ⇒ Facter::Util::Resolution
Define a new named resolution or return an existing resolution with the given name.
- #extract_ldapname_option!(options) ⇒ Object deprecated private Deprecated.
-
#flush ⇒ void
private
Flushes any cached values.
-
#initialize(name, options = {}) ⇒ Fact
constructor
private
Creates a new fact, with no resolution mechanisms.
-
#resolution(name) ⇒ Facter::Util::Resolution?
Retrieve an existing resolution by name.
-
#value ⇒ Object
Returns the value for this fact.
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.
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/facter/custom_facts/util/fact.rb', line 37 def initialize(name, = {}) @name = name.to_s.downcase.intern @options = .dup extract_ldapname_option!() @ldapname ||= @name.to_s @resolves = [] @searching = false @used_resolution_weight = 0 @value = nil @log = Facter::Log.new(self) end |
Instance Attribute Details
#ldapname ⇒ String
22 23 24 |
# File 'lib/facter/custom_facts/util/fact.rb', line 22 def ldapname @ldapname end |
#location ⇒ String (readonly)
The location from where fact is resolved
14 15 16 |
# File 'lib/facter/custom_facts/util/fact.rb', line 14 def location @location end |
#name ⇒ String (readonly)
The name of the fact
18 19 20 |
# File 'lib/facter/custom_facts/util/fact.rb', line 18 def name @name end |
#options ⇒ Object
Fact options e.g. fact_type
25 26 27 |
# File 'lib/facter/custom_facts/util/fact.rb', line 25 def @options end |
#used_resolution_weight ⇒ Object
Weight of the resolution that was used to obtain the fact value
28 29 30 |
# File 'lib/facter/custom_facts/util/fact.rb', line 28 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.
63 64 65 66 67 68 69 70 |
# File 'lib/facter/custom_facts/util/fact.rb', line 63 def add( = {}, &block) @options = @options.merge() @location = [:file] @location ||= block.source_location if block_given? define_resolution(nil, , &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.
80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/facter/custom_facts/util/fact.rb', line 80 def define_resolution(resolution_name, = {}, &block) resolution_type = .delete(:type) || :simple resolve = create_or_return_resolution(resolution_name, resolution_type) resolve.() unless .empty? resolve.evaluate(&block) if block resolve rescue StandardError => e LegacyFacter .log_exception(e, "Unable to add resolve #{resolution_name.inspect} for fact #{@name}: #{e.}") end |
#extract_ldapname_option!(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.
147 148 149 150 151 152 |
# File 'lib/facter/custom_facts/util/fact.rb', line 147 def extract_ldapname_option!() return unless [:ldapname] LegacyFacter.warnonce('ldapname is deprecated and will be removed in a future version') self.ldapname = .delete(:ldapname) end |
#flush ⇒ 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.
Flushes any cached values.
111 112 113 114 |
# File 'lib/facter/custom_facts/util/fact.rb', line 111 def flush @resolves.each(&:flush) @value = nil end |
#resolution(name) ⇒ Facter::Util::Resolution?
Retrieve an existing resolution by name
100 101 102 103 104 |
# File 'lib/facter/custom_facts/util/fact.rb', line 100 def resolution(name) return nil if name.nil? @resolves.find { |resolve| resolve.name == name } end |
#value ⇒ Object
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.
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/facter/custom_facts/util/fact.rb', line 121 def value return @value if @value if @resolves.empty? LegacyFacter.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 |