Class: Facter::Util::Fact
- Inherits:
-
Object
- Object
- Facter::Util::Fact
- Defined in:
- lib/facter/util/fact.rb
Overview
This class represents a fact. Each fact has a name and multiple resolutions.
Create facts using Facter.add
Instance Attribute Summary collapse
- #ldapname ⇒ String deprecated Deprecated.
-
#name ⇒ String
The name of the fact.
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.
27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/facter/util/fact.rb', line 27 def initialize(name, = {}) @name = name.to_s.downcase.intern extract_ldapname_option!() @ldapname ||= @name.to_s @resolves = [] @searching = false @value = nil end |
Instance Attribute Details
#ldapname ⇒ String
18 19 20 |
# File 'lib/facter/util/fact.rb', line 18 def ldapname @ldapname end |
#name ⇒ String
The name of the fact
14 15 16 |
# File 'lib/facter/util/fact.rb', line 14 def name @name 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.
49 50 51 |
# File 'lib/facter/util/fact.rb', line 49 def add( = {}, &block) 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.
61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/facter/util/fact.rb', line 61 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 Facter.log_exception(e, "Unable to add resolve #{resolution_name.inspect} for fact #{@name}: #{e.message}") 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.
124 125 126 127 128 129 |
# File 'lib/facter/util/fact.rb', line 124 def extract_ldapname_option!() if [:ldapname] Facter.warnonce("ldapname is deprecated and will be removed in a future version") self.ldapname = .delete(:ldapname) end 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.
92 93 94 95 |
# File 'lib/facter/util/fact.rb', line 92 def flush @resolves.each { |r| r.flush } @value = nil end |
#resolution(name) ⇒ Facter::Util::Resolution?
Retrieve an existing resolution by name
81 82 83 84 85 |
# File 'lib/facter/util/fact.rb', line 81 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.
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/facter/util/fact.rb', line 102 def value return @value if @value if @resolves.empty? Facter.debug "No resolves for %s" % @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 |