Module: Reek::Configuration::ConfigurationFileFinder

Extended by:
ConfigurationValidator
Defined in:
lib/reek/configuration/configuration_file_finder.rb

Overview

ConfigurationFileFinder is responsible for finding Reek’s configuration.

There are 3 ways of passing ‘reek` a configuration file:

  1. Using the cli “-c” switch

  2. Having a file .reek.yml either in your current working directory or in a parent directory

  3. Having a file .reek.yml in your HOME directory

The order in which ConfigurationFileFinder tries to find such a configuration file is exactly like above.

Constant Summary collapse

DEFAULT_FILE_NAME =
'.reek.yml'

Class Method Summary collapse

Methods included from ConfigurationValidator

key_to_smell_detector, smell_type?, with_valid_directory

Class Method Details

.find(path: nil, current: Pathname.pwd, home: Pathname.new(Dir.home)) ⇒ File|nil

Tries to find a configuration file via:

* given path (e.g. via cli switch)
* ascending down from the current directory
* looking into the home directory

Returns:

  • (File|nil)


44
45
46
# File 'lib/reek/configuration/configuration_file_finder.rb', line 44

def find(path: nil, current: Pathname.pwd, home: Pathname.new(Dir.home))
  path || find_by_dir(current) || find_in_dir(home)
end

.find_and_load(path: nil) ⇒ Hash

Finds and loads a configuration file from a given path.

Returns:

  • (Hash)


31
32
33
# File 'lib/reek/configuration/configuration_file_finder.rb', line 31

def find_and_load(path: nil)
  load_from_file(find(path: path))
end

.find_by_dir(start) ⇒ File|nil (private)

Recursively traverse directories down to find a configuration file.

Returns:

  • (File|nil)


76
77
78
79
80
81
# File 'lib/reek/configuration/configuration_file_finder.rb', line 76

def find_by_dir(start)
  start.ascend do |dir|
    file = find_in_dir(dir)
    return file if file
  end
end

.find_in_dir(dir) ⇒ File|nil (private)

Checks a given directory for a configuration file and returns it.

Returns:

  • (File|nil)


89
90
91
92
# File 'lib/reek/configuration/configuration_file_finder.rb', line 89

def find_in_dir(dir)
  file = dir + DEFAULT_FILE_NAME
  file if file.file?
end

.load_from_file(path) ⇒ Hash

Loads a configuration file from a given path. Raises on invalid data.

Parameters:

  • path (String)

Returns:

  • (Hash)


56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/reek/configuration/configuration_file_finder.rb', line 56

def load_from_file(path)
  return {} unless path

  begin
    configuration = YAML.load_file(path) || {}
    SchemaValidator.new(configuration).validate
  rescue StandardError => error
    raise Errors::ConfigFileError, "Invalid configuration file #{path}, error is #{error}"
  end

  ConfigurationConverter.new(configuration).convert
end