Class: RFacter::Util::Fact Private
- Inherits:
-
Object
- Object
- RFacter::Util::Fact
- 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
Instance Attribute Summary collapse
-
#name ⇒ String
private
The name of the fact.
Instance Method Summary collapse
-
#add(options = {}, &block) ⇒ RFacter::Util::Resolution
private
Adds a new resolution.
-
#define_resolution(resolution_name, options = {}, &block) ⇒ RFacter::Util::Resolution
private
Define a new named resolution or return an existing resolution with the given name.
-
#flush ⇒ void
private
Flushes any cached values.
-
#initialize(name, config: RFacter::Config.config, **options) ⇒ Fact
constructor
private
Creates a new fact, with no resolution mechanisms.
-
#resolution(name) ⇒ RFacter::Util::Resolution?
private
Retrieve an existing resolution by name.
-
#value ⇒ Object
private
Returns the value for this fact.
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.
29 30 31 32 33 34 35 36 37 |
# File 'lib/rfacter/util/fact.rb', line 29 def initialize(name, config: RFacter::Config.config, **) @name = name.to_s.downcase.intern @config = config @resolves = [] @searching = false @value = nil end |
Instance Attribute Details
#name ⇒ String
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
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.
46 47 48 |
# File 'lib/rfacter/util/fact.rb', line 46 def add( = {}, &block) define_resolution(nil, , &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.
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, = {}, &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 => e logger.log_exception(e, "Unable to add resolve #{resolution_name.inspect} for fact #{@name}: #{e.}") 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.
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
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 |
#value ⇒ 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.
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.
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 |