Class: Uniword::Configuration::ConfigurationLoader

Inherits:
Object
  • Object
show all
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.

Examples:

Load transformation rules

config = ConfigurationLoader.load('transformation_rules')
property_mappings = config['property_mappings']

Load with custom path

config = ConfigurationLoader.load_file('/custom/path/rules.yml')

Constant Summary collapse

CONFIG_DIR =

Default configuration directory

File.expand_path("../../../config", __dir__)

Class Method Summary collapse

Class Method Details

.get(config, key_path, default = nil) ⇒ Object

Get a configuration value with dot notation

Examples:

Get nested value

font = ConfigurationLoader.get(config, 'format_defaults.docx.default_font')

Parameters:

  • config (Hash)

    Configuration hash

  • key_path (String)

    Dot-separated key path (e.g., ‘format_defaults.docx.default_font’)

  • default (Object) (defaults to: nil)

    Default value if key not found

Returns:

  • (Object)

    Configuration value or default



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

Examples:

Load transformation rules

config = ConfigurationLoader.load('transformation_rules')

Parameters:

  • name (String)

    Configuration file name (without .yml)

Returns:

  • (Hash)

    Loaded configuration

Raises:



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

Examples:

Load from custom path

config = ConfigurationLoader.load_file('/custom/rules.yml')

Parameters:

  • path (String)

    Full path to configuration file

Returns:

  • (Hash)

    Loaded configuration

Raises:



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.message}"
rescue StandardError => e
  raise ConfigurationError,
        "Failed to load configuration from #{path}: #{e.message}"
end

.merge(base, override) ⇒ Hash

Merge two configuration hashes

Examples:

Merge configurations

merged = ConfigurationLoader.merge(default_config, user_config)

Parameters:

  • base (Hash)

    Base configuration

  • override (Hash)

    Override configuration

Returns:

  • (Hash)

    Merged configuration



92
93
94
# File 'lib/uniword/configuration/configuration_loader.rb', line 92

def merge(base, override)
  deep_merge(base.dup, override)
end