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.

Methods

[]   add   each   fact   flush   list   load_all   loader   new   to_hash   value  

Included Modules

Enumerable

Public Class methods

[Source]

    # File lib/facter/util/collection.rb, line 79
79:     def initialize
80:         @facts = Hash.new
81:     end

Public Instance methods

Return a fact object by name. If you use this, you still have to call ‘value’ on it to retrieve the actual value.

[Source]

    # File lib/facter/util/collection.rb, line 10
10:     def [](name)
11:         value(name)
12:     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.

[Source]

    # 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.

[Source]

    # 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

Return a fact by name.

[Source]

    # File lib/facter/util/collection.rb, line 66
66:     def fact(name)
67:         name = canonize(name)
68: 
69:         loader.load(name) unless @facts[name]
70: 
71:         return @facts[name]
72:     end

Flush all cached values.

[Source]

    # File lib/facter/util/collection.rb, line 75
75:     def flush
76:         @facts.each { |name, fact| fact.flush }
77:     end

Return a list of all of the facts.

[Source]

    # File lib/facter/util/collection.rb, line 84
84:     def list
85:         return @facts.keys
86:     end

Load all known facts.

[Source]

    # 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.

[Source]

    # 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.

[Source]

     # 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

[Source]

     # File lib/facter/util/collection.rb, line 113
113:     def value(name)
114:         if fact = fact(name)
115:             fact.value
116:         end
117:     end

[Validate]