From bfc4996e2cd22d3bae5c3955366c63fdd5277cf8 Mon Sep 17 00:00:00 2001 From: Luke Kanies Date: Wed, 14 May 2008 00:24:24 -0500 Subject: Moving Facter's container behaviour into a separate class. There's now no @@facts instance variable; instead, there's a collection, and it's responsible for keeping references to all of the facts. All of the old interface methods just delegate to the collection. --- lib/facter.rb | 85 +++++++++++++++-------------------------------------------- 1 file changed, 21 insertions(+), 64 deletions(-) (limited to 'lib/facter.rb') diff --git a/lib/facter.rb b/lib/facter.rb index ebc4299..36a68b8 100644 --- a/lib/facter.rb +++ b/lib/facter.rb @@ -19,6 +19,7 @@ module Facter require 'facter/fact' + require 'facter/collection' include Comparable include Enumerable @@ -38,20 +39,19 @@ module Facter # - @@facts = Hash.new { |hash, key| - key = key.to_s.downcase.intern - if hash.include?(key) - hash[key] - else - nil - end - } GREEN = "" RESET = "" @@debug = 0 # module methods + def self.collection + unless defined?(@collection) and @collection + @collection = Facter::Collection.new + end + @collection + end + # Return the version of the library. def self.version return FACTERVERSION @@ -70,38 +70,25 @@ module Facter # Return a fact object by name. If you use this, you still have to call # 'value' on it to retrieve the actual value. def self.[](name) - @@facts[name] + collection.fact(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 self.add(name, options = {}, &block) - unless fact = @@facts[name] - fact = Facter::Fact.new(name, options) - @@facts[name] = fact - end - - unless block - return fact + class << self + [:add, :fact, :flush, :list, :to_hash, :value].each do |method| + define_method(method) do |*args| + collection.send(method, *args) + end end + end - fact.add(&block) - return fact + # 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 self.add(name, options = {}, &block) + collection.add(name, options, &block) end class << self - include Enumerable - # Iterate across all of the facts. - def each - @@facts.each { |name,fact| - value = fact.value - if ! value.nil? - yield name.to_s, fact.value - end - } - end - # Allow users to call fact names directly on the Facter class, # either retrieving the value or comparing it to an existing value. def method_missing(name, *args) @@ -111,7 +98,7 @@ module Facter name = name.to_s.sub(/\?$/,'') end - if fact = @@facts[name] + if fact = @collection.fact(name) if question value = fact.value.downcase args.each do |arg| @@ -164,39 +151,9 @@ module Facter end end - # Flush all cached values. - def self.flush - @@facts.each { |name, fact| fact.flush } - end - - # Return a list of all of the facts. - def self.list - return @@facts.keys - end - # Remove them all. def self.reset - @@facts.clear - end - - # Return a hash of all of our facts. - def self.to_hash - @@facts.inject({}) do |h, ary| - value = ary[1].value - if ! value.nil? - # For backwards compatibility, convert the fact name to a string. - h[ary[0].to_s] = value - end - h - end - end - - def self.value(name) - if fact = @@facts[name] - fact.value - else - nil - end + @collection = nil end # Load all of the default facts -- cgit