summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorluke <luke@1f5c1d6a-bddf-0310-8f58-fc49e503516a>2006-05-23 19:45:38 +0000
committerluke <luke@1f5c1d6a-bddf-0310-8f58-fc49e503516a>2006-05-23 19:45:38 +0000
commit99b61e7c525400a851d1a39d36fec7f062168bc8 (patch)
treeda514476089922ce97f03670d67e167d1ef323af /lib
parent97f1a5eb1bdf008a60dc051d47095a4d250ba9a6 (diff)
downloadfacter-99b61e7c525400a851d1a39d36fec7f062168bc8.tar.gz
facter-99b61e7c525400a851d1a39d36fec7f062168bc8.tar.xz
facter-99b61e7c525400a851d1a39d36fec7f062168bc8.zip
Adding final autoloading work.
git-svn-id: http://reductivelabs.com/svn/facter/trunk@106 1f5c1d6a-bddf-0310-8f58-fc49e503516a
Diffstat (limited to 'lib')
-rw-r--r--lib/facter.rb79
1 files changed, 56 insertions, 23 deletions
diff --git a/lib/facter.rb b/lib/facter.rb
index 14e2788..f99fefe 100644
--- a/lib/facter.rb
+++ b/lib/facter.rb
@@ -110,30 +110,29 @@ FACTERVERSION = '1.1.4'
if fact = self[name]
return fact.value
else
- p @@facts.keys
super
end
end
end
- def Facter.value(name)
- if @@facts.include?(name.to_s.downcase)
- @@facts[name.to_s.downcase].value
- else
- nil
+ # Load a file by path
+ def Facter.autoload(file)
+ name = File.basename(file).sub(".rb",'')
+ begin
+ require file
+ unless @@facts.include?(name)
+ warn "Loaded %s but it did not define fact %s" % [file, name]
+ end
+ rescue LoadError => detail
+ warn "Failed to load %s: %s" % [file, detail]
end
end
- # Flush all cached values.
- def Facter.flush
- @@facts.each { |fact| fact.flush }
- end
-
- # Remove them all.
- def Facter.reset
- @@facts.each { |name,fact|
- @@facts.delete(name)
- }
+ # Clear all facts. Mostly used for testing.
+ def Facter.clear
+ Facter.reset
+ Facter.flush
+ @@facts.clear
end
# Set debugging on or off.
@@ -162,11 +161,23 @@ FACTERVERSION = '1.1.4'
end
end
+ # Flush all cached values.
+ def Facter.flush
+ @@facts.each { |fact| fact.flush }
+ end
+
# Return a list of all of the facts.
def Facter.list
return @@facts.keys
end
+ # Remove them all.
+ def Facter.reset
+ @@facts.each { |name,fact|
+ @@facts.delete(name)
+ }
+ end
+
# Return a hash of all of our facts.
def Facter.to_hash
self.inject({}) do |h, ary|
@@ -175,6 +186,14 @@ FACTERVERSION = '1.1.4'
end
end
+ def Facter.value(name)
+ if @@facts.include?(name.to_s.downcase)
+ @@facts[name.to_s.downcase].value
+ else
+ nil
+ end
+ end
+
# Compare one value to another.
def <=>(other)
return self.value <=> other
@@ -834,14 +853,28 @@ FACTERVERSION = '1.1.4'
tag("operatingsystem","Linux")
setcode "whoami"
end
+
+ locals = []
+
+ # Now see if we can find any other facts
+ $:.each do |dir|
+ fdir = File.join(dir, "facter")
+ if FileTest.exists?(fdir)
+ Dir.chdir(fdir) do
+ Dir.glob("*.rb").each do |file|
+ if file == "local.rb"
+ # Just require it normally
+ require File.join(fdir, file)
+ else
+ # We assume it's a new fact to be
+ # autoloaded, so ask Facter to load it for us
+ Facter.autoload(File.join(fdir, file))
+ end
+ end
+ end
+ end
+ end
end
Facter.load
end
-
-# try to load a local fact library, if there happens to be one
-begin
- require 'facter/local'
-rescue LoadError
- # no worries
-end