summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorluke <luke@1f5c1d6a-bddf-0310-8f58-fc49e503516a>2006-05-23 15:14:11 +0000
committerluke <luke@1f5c1d6a-bddf-0310-8f58-fc49e503516a>2006-05-23 15:14:11 +0000
commit22bd24b2a2d29431320f67ebbb93d36fbae1c9ba (patch)
treef67d901857a16afb36c245b63095d3b953b5fb34 /lib
parentfe782b9596ee8b727f0b91e08318f27a99ab5250 (diff)
downloadfacter-22bd24b2a2d29431320f67ebbb93d36fbae1c9ba.tar.gz
facter-22bd24b2a2d29431320f67ebbb93d36fbae1c9ba.tar.xz
facter-22bd24b2a2d29431320f67ebbb93d36fbae1c9ba.zip
Added "architecture" fact, added the ability to autoload facts from separate files, and added the ability to retrieve fact values via a method for each fact.
git-svn-id: http://reductivelabs.com/svn/facter/trunk@101 1f5c1d6a-bddf-0310-8f58-fc49e503516a
Diffstat (limited to 'lib')
-rw-r--r--lib/facter.rb47
1 files changed, 44 insertions, 3 deletions
diff --git a/lib/facter.rb b/lib/facter.rb
index 8415f81..a246845 100644
--- a/lib/facter.rb
+++ b/lib/facter.rb
@@ -55,14 +55,26 @@ FACTERVERSION = '1.1.4'
# Return a fact object by name. If you use this, you still have to call
# 'value' on it to retrieve the actual value.
def Facter.[](name)
- @@facts[name.to_s.downcase]
+ name = name.to_s.downcase
+ unless @@facts.include?(name)
+ # Try autoloading the fact.
+ begin
+ require "facter/#{name}"
+ unless @@facts.include?(name)
+ warn "Loaded facter/#{name} but fact was not added"
+ end
+ rescue LoadError
+ # Just ignore it
+ end
+ end
+ @@facts[name]
end
# Add a resolution mechanism for a named fact. This does not distinguish
# between adding a new fact and adding a new way to resolve a fact.
def Facter.add(name, &block)
fact = nil
- dcname = name.downcase
+ dcname = name.to_s.downcase
if @@facts.include?(dcname)
fact = @@facts[dcname]
@@ -93,6 +105,14 @@ FACTERVERSION = '1.1.4'
end
}
end
+
+ def method_missing(name, *args)
+ if fact = self[name]
+ return fact.value
+ else
+ super
+ end
+ end
end
def Facter.value(name)
@@ -146,6 +166,14 @@ FACTERVERSION = '1.1.4'
return @@facts.keys
end
+ # Return a hash of all of our facts.
+ def Facter.to_hash
+ self.inject({}) do |h, ary|
+ h[ary[0]] = ary[1]
+ h
+ end
+ end
+
# Compare one value to another.
def <=>(other)
return self.value <=> other
@@ -158,7 +186,7 @@ FACTERVERSION = '1.1.4'
# Create a new fact, with no resolution mechanisms.
def initialize(name)
- @name = name.downcase
+ @name = name.downcase if name.is_a? String
if @@facts.include?(@name)
raise ArgumentError, "A fact named %s already exists" % name
else
@@ -520,6 +548,19 @@ FACTERVERSION = '1.1.4'
#tag("operatingsystem","SunOS")
end
+ Facter.add("Architecture") do
+ tag("operatingsystem","Debian")
+ setcode do
+ model = Facter.hardwaremodel
+ case model
+ when 'x86_64': "amd64"
+ when /(i[3456]86|pentium)/: "i386"
+ else
+ model
+ end
+ end
+ end
+
Facter.add("CfKey") do
setcode do
value = nil