| Class | Facter::Util::Collection |
| In: |
lib/facter/util/collection.rb
|
| Parent: | Object |
Manage which facts exist and how we access them. Largely just a wrapper around a hash of facts.
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.
# File lib/facter/util/collection.rb, line 16
16: def add(name, options = {}, &block)
17: name = canonize(name)
18:
19: unless fact = @facts[name]
20: fact = Facter::Util::Fact.new(name)
21:
22: @facts[name] = fact
23: end
24:
25: # Set any fact-appropriate options.
26: options.each do |opt, value|
27: method = opt.to_s + "="
28: if fact.respond_to?(method)
29: fact.send(method, value)
30: options.delete(opt)
31: end
32: end
33:
34: if block
35: resolve = fact.add(&block)
36: # Set any resolve-appropriate options
37: options.each do |opt, value|
38: method = opt.to_s + "="
39: if resolve.respond_to?(method)
40: resolve.send(method, value)
41: options.delete(opt)
42: end
43: end
44: end
45:
46: unless options.empty?
47: raise ArgumentError, "Invalid facter option(s) %s" % options.keys.collect { |k| k.to_s }.join(",")
48: end
49:
50: return fact
51: end
Iterate across all of the facts.
# File lib/facter/util/collection.rb, line 56
56: def each
57: @facts.each do |name, fact|
58: value = fact.value
59: unless value.nil?
60: yield name.to_s, value
61: end
62: end
63: end
Flush all cached values.
# File lib/facter/util/collection.rb, line 75
75: def flush
76: @facts.each { |name, fact| fact.flush }
77: end
Load all known facts.
# File lib/facter/util/collection.rb, line 89
89: def load_all
90: loader.load_all
91: end
The thing that loads facts if we don‘t have them.
# File lib/facter/util/collection.rb, line 94
94: def loader
95: unless defined?(@loader)
96: @loader = Facter::Util::Loader.new
97: end
98: @loader
99: end
Return a hash of all of our facts.
# File lib/facter/util/collection.rb, line 102
102: def to_hash
103: @facts.inject({}) do |h, ary|
104: value = ary[1].value
105: if ! value.nil?
106: # For backwards compatibility, convert the fact name to a string.
107: h[ary[0].to_s] = value
108: end
109: h
110: end
111: end