Class: RFacter::Util::Loader Private

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/rfacter/util/loader.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Load facts on demand.

Since:

  • 0.1.0

Instance Method Summary collapse

Constructor Details

#initialize(config: RFacter::Config.config, **opts) ⇒ Loader

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Loader.

Since:

  • 0.1.0



17
18
19
20
# File 'lib/rfacter/util/loader.rb', line 17

def initialize(config: RFacter::Config.config, **opts)
  @config = config
  @loaded = []
end

Instance Method Details

#load(fact, collection) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Load all resolutions for a single fact.

Parameters:

  • fact (Symbol)

Since:

  • 0.1.0



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/rfacter/util/loader.rb', line 25

def load(fact, collection)
  # Now load from the search path
  shortname = fact.to_s.downcase

  filename = shortname + ".rb"

  paths = search_path
  unless paths.nil?
    paths.each do |dir|
      # Load individual files
      file = File.join(dir, filename)

      load_file(file, collection) if File.file?(file)
    end
  end
end

#load_all(collection) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Load all facts from all directories.

Since:

  • 0.1.0



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/rfacter/util/loader.rb', line 43

def load_all(collection)
  return if defined?(@loaded_all)

  paths = search_path
  unless paths.nil?
    paths.each do |dir|
      # dir is already an absolute path
      Dir.glob(File.join(dir, '*.rb')).each do |path|
        # exclude dirs that end with .rb
        load_file(path, collection) if File.file?(path)
      end
    end
  end

  @loaded_all = true
end

#load_file(file, collection) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Load a file and record is paths to prevent duplicate loads.

Parameters:

  • file (String)

    The *absolute path* to the file to load

Since:

  • 0.1.0



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/rfacter/util/loader.rb', line 94

def load_file(file, collection)
  return if @loaded.include? file

  # We have to specify Kernel.load, because we have a load method.
  begin
    # Store the file path so we don't try to reload it
    @loaded << file

    RFacter::DSL::COLLECTION.bind(collection) do
      collection.instance_eval(File.read(file), file)
    end
  rescue Exception => detail
    # Don't store the path if the file can't be loaded
    # in case it's loadable later on.
    @loaded.delete(file)
    logger.log_exception(detail, "Error loading fact #{file}: #{detail.message}")
  end
end

#search_pathArray<String>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

List directories to search for fact files.

Search paths are gathered from the following sources:

  1. A core set of facts from the rfacter/facts directory

  2. ENV is split and used verbatim

A warning will be generated for paths that are not absolute directories.

Returns:

  • (Array<String>)

Since:

  • 0.1.0



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/rfacter/util/loader.rb', line 71

def search_path
  search_paths = [File.expand_path('../../facts', __FILE__)]

  if ENV.include?("RFACTERLIB")
    search_paths += ENV["RFACTERLIB"].split(File::PATH_SEPARATOR)
  end

  search_paths.delete_if { |path| ! valid_search_path?(path) }

  search_paths.uniq
end

#valid_search_path?(path) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Validate that the given path is valid, ie it is an absolute path.

Parameters:

  • path (String)

Returns:

  • (Boolean)

Since:

  • 0.1.0



87
88
89
# File 'lib/rfacter/util/loader.rb', line 87

def valid_search_path?(path)
  Pathname.new(path).absolute? && File.directory?(path)
end