Parent

Facter::Util::Loader

Load facts on demand.

Public Class Methods

new() click to toggle source
   # File lib/facter/util/loader.rb, line 6
6:   def initialize
7:     @loaded = []
8:   end

Public Instance Methods

load(fact) click to toggle source

Load all resolutions for a single fact.

    # File lib/facter/util/loader.rb, line 11
11:   def load(fact)
12:     # Now load from the search path
13:     shortname = fact.to_s.downcase
14:     load_env(shortname)
15: 
16:     filename = shortname + ".rb"
17:     search_path.each do |dir|
18:       # Load individual files
19:       file = File.join(dir, filename)
20: 
21:       load_file(file) if FileTest.exist?(file)
22: 
23:       # And load any directories matching the name
24:       factdir = File.join(dir, shortname)
25:       load_dir(factdir) if FileTest.directory?(factdir)
26:     end
27:   end
load_all() click to toggle source

Load all facts from all directories.

    # File lib/facter/util/loader.rb, line 30
30:   def load_all
31:     return if defined?(@loaded_all)
32: 
33:     load_env
34: 
35:     search_path.each do |dir|
36:       next unless FileTest.directory?(dir)
37: 
38:       Dir.entries(dir).sort.each do |file|
39:         path = File.join(dir, file)
40:         if File.directory?(path)
41:           load_dir(path)
42:         elsif file =~ /\.rb$/
43:           load_file(File.join(dir, file))
44:         end
45:       end
46:     end
47: 
48:     @loaded_all = true
49:   end
search_path() click to toggle source

The list of directories we’re going to search through for facts.

    # File lib/facter/util/loader.rb, line 52
52:   def search_path
53:     result = []
54:     result += $LOAD_PATH.collect { |d| File.join(d, "facter") }
55:     if ENV.include?("FACTERLIB")
56:       result += ENV["FACTERLIB"].split(":")
57:     end
58: 
59:     # This allows others to register additional paths we should search.
60:     result += Facter.search_path
61: 
62:     result
63:   end

Private Instance Methods

load_dir(dir) click to toggle source
    # File lib/facter/util/loader.rb, line 67
67:   def load_dir(dir)
68:     return if dir =~ /\/\.+$/ or dir =~ /\/util$/ or dir =~ /\/lib$/
69: 
70:     Dir.entries(dir).find_all { |f| f =~ /\.rb$/ }.sort.each do |file|
71:       load_file(File.join(dir, file))
72:     end
73:   end
load_env(fact = nil) click to toggle source

Load facts from the environment. If no name is provided, all will be loaded.

     # File lib/facter/util/loader.rb, line 92
 92:   def load_env(fact = nil)
 93:     # Load from the environment, if possible
 94:     ENV.each do |name, value|
 95:       # Skip anything that doesn't match our regex.
 96:       next unless name =~ /^facter_?(\w+)$/
 97:       env_name = $1
 98: 
 99:       # If a fact name was specified, skip anything that doesn't
100:       # match it.
101:       next if fact and env_name != fact
102: 
103:       Facter.add($1) do
104:         has_weight 1_000_000
105:         setcode { value }
106:       end
107: 
108:       # Short-cut, if we are only looking for one value.
109:       break if fact
110:     end
111:   end
load_file(file) click to toggle source
    # File lib/facter/util/loader.rb, line 75
75:   def load_file(file)
76:     return if @loaded.include? file
77:     # We have to specify Kernel.load, because we have a load method.
78:     begin
79:       # Store the file path so we don't try to reload it
80:       @loaded << file
81:       Kernel.load(file)
82:     rescue ScriptError => detail
83:       # Don't store the path if the file can't be loaded
84:       # in case it's loadable later on.
85:       @loaded.delete(file)
86:       warn "Error loading fact #{file} #{detail}"
87:     end
88:   end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.