Class: Uniword::Configuration::ConfigurationLoader
- Inherits:
-
Object
- Object
- Uniword::Configuration::ConfigurationLoader
- Defined in:
- lib/uniword/configuration/configuration_loader.rb
Overview
Loads and manages external configuration files.
Responsibility: Load configuration from external YAML files. Single Responsibility - only handles configuration loading.
Follows “Configuration over Convention” principle - all configurable behavior is defined in external YAML files, not hardcoded in classes.
Constant Summary collapse
- CONFIG_DIR =
Default configuration directory
File.("../../../config", __dir__)
Class Method Summary collapse
-
.get(config, key_path, default = nil) ⇒ Object
Get a configuration value with dot notation.
-
.load(name) ⇒ Hash
Load configuration from a named file in the config directory.
-
.load_file(path) ⇒ Hash
Load configuration from a specific file path.
-
.merge(base, override) ⇒ Hash
Merge two configuration hashes.
Class Method Details
.get(config, key_path, default = nil) ⇒ Object
Get a configuration value with dot notation
74 75 76 77 78 79 80 81 82 |
# File 'lib/uniword/configuration/configuration_loader.rb', line 74 def get(config, key_path, default = nil) keys = key_path.split(".") value = keys.reduce(config) do |hash, key| return default unless hash.is_a?(Hash) hash[key.to_sym] || hash[key.to_s] end value || default end |
.load(name) ⇒ Hash
Load configuration from a named file in the config directory
34 35 36 37 |
# File 'lib/uniword/configuration/configuration_loader.rb', line 34 def load(name) file_path = File.join(CONFIG_DIR, "#{name}.yml") load_file(file_path) end |
.load_file(path) ⇒ Hash
Load configuration from a specific file path
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/uniword/configuration/configuration_loader.rb', line 47 def load_file(path) validate_file_exists(path) content = File.read(path) parsed = YAML.safe_load(content, permitted_classes: [Symbol]) validate_configuration(parsed, path) # Convert string keys to symbols for easier access deep_symbolize_keys(parsed) rescue Psych::SyntaxError => e raise ConfigurationError, "Invalid YAML in #{path}: #{e.}" rescue StandardError => e raise ConfigurationError, "Failed to load configuration from #{path}: #{e.}" end |
.merge(base, override) ⇒ Hash
Merge two configuration hashes
92 93 94 |
# File 'lib/uniword/configuration/configuration_loader.rb', line 92 def merge(base, override) deep_merge(base.dup, override) end |