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, options = {}) ⇒ 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, options = {}) ⇒ ConfigurationLoader
Create a configuration loader which writes warnings/errors to the given Logger instance.
47 48 49 50 |
# File 'lib/overcommit/configuration_loader.rb', line 47 def initialize(logger, = {}) @log = logger @options = end |
Class Method Details
.default_configuration ⇒ Overcommit::Configuration
Loads and returns the default configuration.
14 15 16 |
# File 'lib/overcommit/configuration_loader.rb', line 14 def default_configuration @default_configuration ||= load_from_file(DEFAULT_CONFIG_PATH, default: true, verify: false) end |
.load_from_file(file, options = {}) ⇒ Overcommit::Configuration
Loads configuration from file.
26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/overcommit/configuration_loader.rb', line 26 def load_from_file(file, = {}) # Psych 4 introduced breaking behavior that doesn't support aliases by # default. Explicitly enable aliases if the option is available. yaml = begin YAML.load_file(file, aliases: true) rescue ArgumentError YAML.load_file(file) end hash = yaml ? yaml.to_hash : {} Overcommit::Configuration.new(hash, ) end |
Instance Method Details
#load_file(file) ⇒ Object
Loads a configuration, ensuring it extends the default configuration.
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/overcommit/configuration_loader.rb', line 67 def load_file(file) config = self.class.load_from_file(file, default: false, logger: @log) config = self.class.default_configuration.merge(config) if @options.fetch(:verify) { config.verify_signatures? } verify_signatures(config) end config rescue Overcommit::Exceptions::ConfigurationSignatureChanged raise rescue StandardError => e raise Overcommit::Exceptions::ConfigurationError, "Unable to load configuration from '#{file}': #{e}", e.backtrace end |
#load_repo_config ⇒ Overcommit::Configuration
Loads and returns the configuration for the repository we’re running in.
55 56 57 58 59 60 61 62 63 64 |
# File 'lib/overcommit/configuration_loader.rb', line 55 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 |