Class: Overcommit::ConfigurationLoader

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

Overview

Manages configuration file loading.

Constant Summary collapse

DEFAULT_CONFIG_PATH =
File.join(Overcommit::HOME, 'config', 'default.yml')

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logger) ⇒ ConfigurationLoader

Create a configuration loader which writes warnings/errors to the given Logger instance.



37
38
39
# File 'lib/overcommit/configuration_loader.rb', line 37

def initialize(logger)
  @log = logger
end

Class Method Details

.default_configurationOvercommit::Configuration

Loads and returns the default configuration.



12
13
14
# File 'lib/overcommit/configuration_loader.rb', line 12

def default_configuration
  @default_config ||= load_from_file(DEFAULT_CONFIG_PATH, default: true)
end

.load_from_file(file, options = {}) ⇒ Overcommit::Configuration

Loads configuration from file.

Parameters:

  • file (String)

    path to file

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

    a customizable set of options

  • logger (Hash)

    a customizable set of options

Returns:



23
24
25
26
27
28
29
30
31
32
# File 'lib/overcommit/configuration_loader.rb', line 23

def load_from_file(file, options = {})
  hash =
    if yaml = YAML.load_file(file)
      yaml.to_hash
    else
      {}
    end

  Overcommit::Configuration.new(hash, options)
end

Instance Method Details

#load_file(file) ⇒ Object

Loads a configuration, ensuring it extends the default configuration.



56
57
58
59
60
61
62
63
64
# File 'lib/overcommit/configuration_loader.rb', line 56

def load_file(file)
  config = self.class.load_from_file(file, default: false, logger: @log)

  self.class.default_configuration.merge(config)
rescue => error
  raise Overcommit::Exceptions::ConfigurationError,
        "Unable to load configuration from '#{file}': #{error}",
        error.backtrace
end

#load_repo_configOvercommit::Configuration

Loads and returns the configuration for the repository we’re running in.



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

def load_repo_config
  overcommit_yml = File.join(Overcommit::Utils.repo_root,
                             Overcommit::CONFIG_FILE_NAME)

  if File.exist?(overcommit_yml)
    load_file(overcommit_yml)
  else
    self.class.default_configuration
  end
end