Class: Overcommit::ConfigurationLoader
- Inherits:
-
Object
- Object
- Overcommit::ConfigurationLoader
- 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
-
.default_configuration ⇒ Overcommit::Configuration
Loads and returns the default configuration.
-
.load_from_file(file, options = {}) ⇒ Overcommit::Configuration
Loads configuration from file.
Instance Method Summary collapse
-
#initialize(logger) ⇒ ConfigurationLoader
constructor
Create a configuration loader which writes warnings/errors to the given Logger instance.
-
#load_file(file) ⇒ Object
Loads a configuration, ensuring it extends the default configuration.
-
#load_repo_config ⇒ Overcommit::Configuration
Loads and returns the configuration for the repository we’re running in.
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_configuration ⇒ Overcommit::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.
23 24 25 26 27 28 29 30 31 32 |
# File 'lib/overcommit/configuration_loader.rb', line 23 def load_from_file(file, = {}) hash = if yaml = YAML.load_file(file) yaml.to_hash else {} end Overcommit::Configuration.new(hash, ) 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_config ⇒ Overcommit::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 |