Class: Facter::Util::DirectoryLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/facter/util/directory_loader.rb

Defined Under Namespace

Classes: NoSuchDirectoryError

Constant Summary collapse

EXTERNAL_FACT_WEIGHT =

This value makes it highly likely that external facts will take precedence over all other facts

10000

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dir, weight = nil) ⇒ DirectoryLoader

Returns a new instance of DirectoryLoader.



36
37
38
39
# File 'lib/facter/util/directory_loader.rb', line 36

def initialize(dir, weight = nil)
  @directory = dir
  @weight = weight || EXTERNAL_FACT_WEIGHT
end

Instance Attribute Details

#directoryObject (readonly)

Directory for fact loading



34
35
36
# File 'lib/facter/util/directory_loader.rb', line 34

def directory
  @directory
end

Class Method Details

.default_loaderObject



49
50
51
52
53
54
# File 'lib/facter/util/directory_loader.rb', line 49

def self.default_loader
  loaders = Facter::Util::Config.external_facts_dirs.collect do |dir|
    Facter::Util::DirectoryLoader.new(dir)
  end
  Facter::Util::CompositeLoader.new(loaders)
end

.loader_for(dir) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/facter/util/directory_loader.rb', line 41

def self.loader_for(dir)
  if File.directory?(dir)
    Facter::Util::DirectoryLoader.new(dir)
  else
    raise NoSuchDirectoryError
  end
end

Instance Method Details

#load(collection) ⇒ Object

Load facts from files in fact directory using the relevant parser classes to parse them.



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/facter/util/directory_loader.rb', line 58

def load(collection)
  weight = @weight
  entries.each do |file|
    parser = Facter::Util::Parser.parser_for(file)
    if parser == nil
      next
    end

    data = parser.results
    if data == false
      Facter.warn "Could not interpret fact file #{file}"
    elsif data == {} or data == nil
      Facter.warn "Fact file #{file} was parsed but returned an empty data set"
    else
      data.each { |p,v| collection.add(p, :value => v) { has_weight(weight) } }
    end
  end
end