Class: LintTrappings::ConfigurationLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/lint_trappings/configuration_loader.rb

Overview

Manages configuration file loading.

Instance Method Summary collapse

Constructor Details

#initialize(application) ⇒ ConfigurationLoader

Returns a new instance of ConfigurationLoader.


7
8
9
# File 'lib/lint_trappings/configuration_loader.rb', line 7

def initialize(application)
  @application = application
end

Instance Method Details

#load(options = {}) ⇒ LintTrappings::Configuration

Load configuration file given the current working directory the application is running within.

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :working_directory (String)

    directory to start searching from

Returns:

Raises:


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/lint_trappings/configuration_loader.rb', line 21

def load(options = {})
  config_file_names = @application.configuration_file_names
  working_directory = options.fetch(:working_directory, Dir.pwd)

  directory = File.expand_path(working_directory)
  config_file = possible_config_files(config_file_names, directory).find(&:file?)

  if config_file
    load_file(config_file.to_path)
  else
    raise NoConfigurationFileError,
          "No configuration file #{config_file_names.join('/')} found in " \
          'working directory or any parent directory'
  end
end

#load_file(path) ⇒ LintTrappings::Configuration

Loads a configuration, ensuring it extends the base configuration.

Parameters:

  • path (String)

Returns:

Raises:


45
46
47
48
49
50
51
52
53
54
# File 'lib/lint_trappings/configuration_loader.rb', line 45

def load_file(path)
  load_from_file(path)
rescue ::Psych::SyntaxError => error
  raise ConfigurationParseError,
        "Unable to parse configuration from '#{file}': #{error}",
        error.backtrace
rescue Errno::ENOENT => error
  raise NoConfigurationFileError,
        "Unable to load configuration from '#{file}': #{error}"
end