Class: LegacyFacter::Util::DirectoryLoader

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

Overview

Since:

  • 2.0.0

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

Since:

  • 2.0.0

10_000

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.

Since:

  • 2.0.0



35
36
37
38
# File 'lib/custom_facts/util/directory_loader.rb', line 35

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

Instance Attribute Details

#directoryObject (readonly)

Directory for fact loading

Since:

  • 2.0.0



33
34
35
# File 'lib/custom_facts/util/directory_loader.rb', line 33

def directory
  @directory
end

Class Method Details

.default_loaderObject

Since:

  • 2.0.0



46
47
48
49
50
51
# File 'lib/custom_facts/util/directory_loader.rb', line 46

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

.loader_for(dir) ⇒ Object

Raises:

Since:

  • 2.0.0



40
41
42
43
44
# File 'lib/custom_facts/util/directory_loader.rb', line 40

def self.loader_for(dir)
  raise NoSuchDirectoryError unless File.directory?(dir)

  LegacyFacter::Util::DirectoryLoader.new(dir)
end

Instance Method Details

#load(collection) ⇒ Object

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

Since:

  • 2.0.0



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/custom_facts/util/directory_loader.rb', line 55

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

    data = parser.results
    if data == false
      LegacyFacter.warn "Could not interpret fact file #{file}"
    elsif (data == {}) || data.nil?
      LegacyFacter.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