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: begin 37: resolve = Facter::Util::Resolution.new(@name) 38: 39: resolve.instance_eval(&block) 40: 41: @resolves << resolve 42: 43: # Immediately sort the resolutions, so that we always have 44: # a sorted list for looking up values. 45: @resolves.sort! { |a, b| b.weight <=> a.weight } 46: 47: resolve 48: rescue => e 49: Facter.warn "Unable to add resolve for #{@name}: #{e}" 50: nil 51: end 52: end
Flush any cached values.
# File lib/facter/util/fact.rb, line 55 55: def flush 56: @value = nil 57: @suitable = nil 58: 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 62 62: def value 63: return @value if @value 64: 65: if @resolves.length == 0 66: Facter.debug "No resolves for %s" % @name 67: return nil 68: end 69: 70: searching do 71: @value = nil 72: 73: foundsuits = false 74: @value = @resolves.inject(nil) { |result, resolve| 75: next unless resolve.suitable? 76: foundsuits = true 77: 78: tmp = resolve.value 79: 80: break tmp unless tmp.nil? or tmp == "" 81: } 82: 83: unless foundsuits 84: Facter.debug "Found no suitable resolves of %s for %s" % [@resolves.length, @name] 85: end 86: end 87: 88: if @value.nil? 89: # nothing 90: Facter.debug("value for %s is still nil" % @name) 91: return nil 92: else 93: return @value 94: end 95: end
Lock our searching process, so we never ge stuck in recursion.
# File lib/facter/util/fact.rb, line 105 105: def searching 106: if searching? 107: Facter.debug "Caught recursion on %s" % @name 108: 109: # return a cached value if we've got it 110: if @value 111: return @value 112: else 113: return nil 114: end 115: end 116: 117: # If we've gotten this far, we're not already searching, so go ahead and do so. 118: @searching = true 119: begin 120: yield 121: ensure 122: @searching = false 123: end 124: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.