Class: Tabry::ConfigLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/tabry/config_loader.rb

Defined Under Namespace

Classes: ConfigNotFound

Constant Summary collapse

EXTENSIONS =
%w[json yml yaml].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:) ⇒ ConfigLoader

Returns a new instance of ConfigLoader.



17
18
19
# File 'lib/tabry/config_loader.rb', line 17

def initialize(name:)
  @name = name
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



15
16
17
# File 'lib/tabry/config_loader.rb', line 15

def name
  @name
end

Class Method Details

.load(**args) ⇒ Object



11
12
13
# File 'lib/tabry/config_loader.rb', line 11

def self.load(**args)
  new(**args).load
end

Instance Method Details

#loadObject

Raises:



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/tabry/config_loader.rb', line 21

def load
  return load_from_file(name) if name =~ /json$/i || name =~ /\.ya?ml$/i

  load_paths.each do |path|
    EXTENSIONS.each do |extension|
      basename = "/#{name}.#{extension}"

      # First check if the path _is_ the file "mycmd.json", "mycmd.yml" we are looking for
      return load_from_file(path) if path.end_with?(basename) && File.exist?(path)

      # Then look for a file "mycmd.json", "mycmd.yml" etc. in the
      # directory (assuming it is a directory)
      filename = "#{path}/#{basename}"
      return load_from_file(filename) if File.exist?(filename)
    end
  end

  raise ConfigNotFound, "Could not find Tabry config #{name}.(#{EXTENSIONS.join(", ")}) in paths: #{load_paths.inspect}"
end