diff options
author | Luke Kanies <luke@madstop.com> | 2008-06-17 10:55:22 -0500 |
---|---|---|
committer | Luke Kanies <luke@madstop.com> | 2008-06-17 10:55:22 -0500 |
commit | 2ee5d29548b48a3c81f574c2be84db60f6ff306a (patch) | |
tree | 7cd05d8dc23303c157af565968f49b54cc2fd4e4 | |
parent | d322df9a367fde290a3aa3d1b15082939c8fff19 (diff) | |
download | facter-2ee5d29548b48a3c81f574c2be84db60f6ff306a.tar.gz facter-2ee5d29548b48a3c81f574c2be84db60f6ff306a.tar.xz facter-2ee5d29548b48a3c81f574c2be84db60f6ff306a.zip |
Refactoring how recursive searches are detected.
Using a yield hook instead of just setting @searching to be
true, which should be more consistent because @searching
will now always be turned off at the end of the search.
-rw-r--r-- | lib/facter/util/fact.rb | 56 |
1 files changed, 37 insertions, 19 deletions
diff --git a/lib/facter/util/fact.rb b/lib/facter/util/fact.rb index c96ca3b..214814c 100644 --- a/lib/facter/util/fact.rb +++ b/lib/facter/util/fact.rb @@ -57,26 +57,16 @@ class Facter::Util::Fact # Return the value for a given fact. Searches through all of the mechanisms # and returns either the first value or nil. def value - unless @value - # make sure we don't get stuck in recursive dependency loops - if @searching - Facter.debug "Caught recursion on %s" % @name - - # return a cached value if we've got it - if @value - return @value - else - return nil - end - end - @value = nil + return @value if @value - if @resolves.length == 0 - Facter.debug "No resolves for %s" % @name - return nil - end + if @resolves.length == 0 + Facter.debug "No resolves for %s" % @name + return nil + end + + searching do + @value = nil - @searching = true foundsuits = false @value = @resolves.inject(nil) { |result, resolve| next unless resolve.suitable? @@ -86,7 +76,6 @@ class Facter::Util::Fact break tmp unless tmp.nil? or tmp == "" } - @searching = false unless foundsuits Facter.debug "Found no suitable resolves of %s for %s" % [@resolves.length, @name] @@ -101,4 +90,33 @@ class Facter::Util::Fact return @value end end + + private + + # Are we in the midst of a search? + def searching? + @searching + end + + # Lock our searching process, so we never ge stuck in recursion. + def searching + if searching? + Facter.debug "Caught recursion on %s" % @name + + # return a cached value if we've got it + if @value + return @value + else + return nil + end + end + + # If we've gotten this far, we're not already searching, so go ahead and do so. + @searching = true + begin + yield + ensure + @searching = false + end + end end |