| Class | Facter::Util::Fact |
| In: |
lib/facter/util/fact.rb
|
| Parent: | Object |
| TIMEOUT | = | 5 |
| ldapname | [RW] | |
| name | [RW] |
Create a new fact, with no resolution mechanisms.
# File lib/facter/util/fact.rb, line 10
10: def initialize(name, options = {})
11: @name = name.to_s.downcase.intern
12:
13: # LAK:NOTE: This is slow for many options, but generally we won't have any and at
14: # worst we'll have one. If we add more, this should be made more efficient.
15: options.each do |name, value|
16: case name
17: when :ldapname; self.ldapname = value
18: else
19: raise ArgumentError, "Invalid fact option '%s'" % name
20: end
21: end
22:
23: @ldapname ||= @name.to_s
24:
25: @resolves = []
26: @searching = false
27:
28: @value = nil
29: end
Add a new resolution mechanism. This requires a block, which will then be evaluated in the context of the new mechanism.
# File lib/facter/util/fact.rb, line 33
33: def add(&block)
34: raise ArgumentError, "You must pass a block to Fact<instance>.add" unless block_given?
35:
36: resolve = Facter::Util::Resolution.new(@name)
37:
38: resolve.instance_eval(&block)
39:
40: @resolves << resolve
41:
42: # Immediately sort the resolutions, so that we always have
43: # a sorted list for looking up values.
44: # We always want to look them up in the order of number of
45: # confines, so the most restricted resolution always wins.
46: @resolves.sort! { |a, b| b.length <=> a.length }
47:
48: return resolve
49: end
Flush any cached values.
# File lib/facter/util/fact.rb, line 52
52: def flush
53: @value = nil
54: @suitable = nil
55: end
Return the value for a given fact. Searches through all of the mechanisms and returns either the first value or nil.
# File lib/facter/util/fact.rb, line 59
59: def value
60: return @value if @value
61:
62: if @resolves.length == 0
63: Facter.debug "No resolves for %s" % @name
64: return nil
65: end
66:
67: searching do
68: @value = nil
69:
70: foundsuits = false
71: @value = @resolves.inject(nil) { |result, resolve|
72: next unless resolve.suitable?
73: foundsuits = true
74:
75: tmp = resolve.value
76:
77: break tmp unless tmp.nil? or tmp == ""
78: }
79:
80: unless foundsuits
81: Facter.debug "Found no suitable resolves of %s for %s" % [@resolves.length, @name]
82: end
83: end
84:
85: if @value.nil?
86: # nothing
87: Facter.debug("value for %s is still nil" % @name)
88: return nil
89: else
90: return @value
91: end
92: end