Class: Facter::Util::Collection

### Home Classes Methods

### In Files

- lib/facter/util/collection.rb

### Parent

Object

### Methods

- ::new
- #[]
- #add
- #each
- #external_loader
- #fact
- #flush
- #internal_loader
- #list
- #load
- #load_all
- #to_hash
- #value

### Included Modules

- Enumerable

### Files

- LICENSE
- Rakefile
- THANKS

### Class/Module Index

Quicksearch

- Facter
- Facter::Application
- Facter::Manufacturer
- Facter::Memory
- Facter::NetMask
- Facter::Util
- Facter::Util::CFPropertyList
- Facter::Util::CFPropertyList::CFArray
- Facter::Util::CFPropertyList::CFBoolean
- Facter::Util::CFPropertyList::CFData
- Facter::Util::CFPropertyList::CFDate
- Facter::Util::CFPropertyList::CFDictionary
- Facter::Util::CFPropertyList::CFInteger
- Facter::Util::CFPropertyList::CFReal
- Facter::Util::CFPropertyList::CFString
- Facter::Util::CFPropertyList::CFType
- Facter::Util::CFPropertyList::XML
- Facter::Util::Collection
- Facter::Util::CompositeLoader
- Facter::Util::Config
- Facter::Util::Confine
- Facter::Util::DirectoryLoader
- Facter::Util::DirectoryLoader::NoSuchDirectoryError
- Facter::Util::EC2
- Facter::Util::Fact
- Facter::Util::FileRead
- Facter::Util::IP
- Facter::Util::IP::Windows
- Facter::Util::IP::Windows::Bindings4
- Facter::Util::IP::Windows::Bindings6
- Facter::Util::Loader
- Facter::Util::Macaddress
- Facter::Util::Macaddress::Darwin
- Facter::Util::Macaddress::Windows
- Facter::Util::Macosx
- Facter::Util::MonkeyPatches
- Facter::Util::MonkeyPatches::Lines
- Facter::Util::NothingLoader
- Facter::Util::Parser
- Facter::Util::Parser::Base
- Facter::Util::Parser::JsonParser
- Facter::Util::Parser::KeyValuePairOutputFormat
- Facter::Util::Parser::NothingParser
- Facter::Util::Parser::PowershellParser
- Facter::Util::Parser::ScriptParser
- Facter::Util::Parser::TextParser
- Facter::Util::Parser::YamlParser
- Facter::Util::Processor
- Facter::Util::Registry
- Facter::Util::Resolution
- Facter::Util::SolarisZones
- Facter::Util::Uptime
- Facter::Util::Values
- Facter::Util::Virtual
- Facter::Util::Vlans
- Facter::Util::WMI
- Facter::Util::Xendomains
- Plist
- Plist::Emit
- Plist::Listener
- Plist::PArray
- Plist::PData
- Plist::PDate
- Plist::PDict
- Plist::PFalse
- Plist::PInteger
- Plist::PKey
- Plist::PList
- Plist::PReal
- Plist::PString
- Plist::PTag
- Plist::PTrue
- Plist::StreamParser
- CFFormatError
- CFPlistError
- CFTypeError
- IO
- Object
- String

No matching classes.

# Facter::Util::Collection

Manage which facts exist and how we access them. Largely just a wrapper around a hash of facts.

### Public Class Methods

new(internal_loader, external_loader) click to toggle source

    # File lib/facter/util/collection.rb, line 9
    def initialize(internal_loader, external_loader)
      @facts = Hash.new
      @internal_loader = internal_loader
      @external_loader = external_loader
    end

### Public Instance Methods

[](name) click to toggle source

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

    # File lib/facter/util/collection.rb, line 17
    def [](name)
      value(name)
    end

add(name, options = {}, &block) click to toggle source

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 23
    def add(name, options = {}, &block)
      name = canonize(name)

      unless fact = @facts[name]
        fact = Facter::Util::Fact.new(name)

        @facts[name] = fact
      end

      # Set any fact-appropriate options.
      options.each do |opt, value|
        method = opt.to_s + "="
        if fact.respond_to?(method)
          fact.send(method, value)
          options.delete(opt)
        end
      end

      if block_given?
        resolve = fact.add(&block)
      else
        resolve = fact.add
      end

      # Set any resolve-appropriate options
      if resolve
        # If the resolve was actually added, set any resolve-appropriate options
        options.each do |opt, value|
          method = opt.to_s + "="
          if resolve.respond_to?(method)
            resolve.send(method, value)
            options.delete(opt)
          end
        end
      end

      unless options.empty?
        raise ArgumentError, "Invalid facter option(s) %s" % options.keys.collect { |k| k.to_s }.join(",")
      end

      return fact
    end

each() click to toggle source

Iterate across all of the facts.

    # File lib/facter/util/collection.rb, line 69
    def each
      load_all
      @facts.each do |name, fact|
        value = fact.value
        unless value.nil?
          yield name.to_s, value
        end
      end
    end

external_loader() click to toggle source

    # File lib/facter/util/collection.rb, line 122
    def external_loader
      @external_loader
    end

fact(name) click to toggle source

Return a fact by name.

    # File lib/facter/util/collection.rb, line 80
    def fact(name)
      name = canonize(name)

      # Try to load the fact if necessary
      load(name) unless @facts[name]

      # Try HARDER
      internal_loader.load_all unless @facts[name]

      if @facts.empty?
        Facter.warnonce("No facts loaded from #{internal_loader.search_path.join(File::PATH_SEPARATOR)}")
      end

      @facts[name]
    end

flush() click to toggle source

Flush all cached values.

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

internal_loader() click to toggle source

    # File lib/facter/util/collection.rb, line 118
    def internal_loader
      @internal_loader
    end

list() click to toggle source

Return a list of all of the facts.

    # File lib/facter/util/collection.rb, line 102
    def list
      load_all
      return @facts.keys
    end

load(name) click to toggle source

    # File lib/facter/util/collection.rb, line 107
    def load(name)
      internal_loader.load(name)
      external_loader.load(self)
    end

load_all() click to toggle source

Load all known facts.

    # File lib/facter/util/collection.rb, line 113
    def load_all
      internal_loader.load_all
      external_loader.load(self)
    end

to_hash() click to toggle source

Return a hash of all of our facts.

    # File lib/facter/util/collection.rb, line 127
    def 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

value(name) click to toggle source

    # File lib/facter/util/collection.rb, line 138
    def value(name)
      if fact = fact(name)
        fact.value
      end
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.