Class: Sxn::Config::Manager
- Inherits:
-
Object
- Object
- Sxn::Config::Manager
- Defined in:
- lib/sxn/config.rb
Overview
Main configuration manager that integrates discovery, caching, and validation
Features:
-
Hierarchical configuration loading with caching
-
Configuration validation and migration
-
Environment variable overrides
-
Thread-safe configuration access
Constant Summary collapse
- DEFAULT_CACHE_TTL =
5 minutes
300
Instance Attribute Summary collapse
-
#cache ⇒ Object
readonly
Returns the value of attribute cache.
-
#current_config ⇒ Object
readonly
Returns the value of attribute current_config.
-
#discovery ⇒ Object
readonly
Returns the value of attribute discovery.
-
#validator ⇒ Object
readonly
Returns the value of attribute validator.
Instance Method Summary collapse
-
#cache_stats ⇒ Hash
Get cache statistics.
-
#config(cli_options: {}, force_reload: false) ⇒ Hash
Get the current configuration with caching.
-
#config_file_paths ⇒ Array<String>
Get all configuration file paths in precedence order.
-
#debug_info ⇒ Hash
Get configuration summary for debugging.
-
#errors(cli_options: {}) ⇒ Array<String>
Get validation errors for current configuration.
-
#get(key_path, default: nil) ⇒ Object
Get configuration value by key path.
-
#initialize(start_directory: Dir.pwd, cache_ttl: DEFAULT_CACHE_TTL) ⇒ Manager
constructor
A new instance of Manager.
-
#invalidate_cache ⇒ Boolean
Invalidate configuration cache.
-
#reload(cli_options: {}) ⇒ Hash
Reload configuration from disk.
-
#set(key_path, value) ⇒ Object
Set configuration value by key path (for runtime modifications).
-
#valid?(cli_options: {}) ⇒ Boolean
Check if configuration is valid.
Constructor Details
#initialize(start_directory: Dir.pwd, cache_ttl: DEFAULT_CACHE_TTL) ⇒ Manager
Returns a new instance of Manager.
21 22 23 24 25 26 27 |
# File 'lib/sxn/config.rb', line 21 def initialize(start_directory: Dir.pwd, cache_ttl: DEFAULT_CACHE_TTL) @discovery = ConfigDiscovery.new(start_directory) @cache = ConfigCache.new(ttl: cache_ttl) @validator = ConfigValidator.new @current_config = nil @config_mutex = Mutex.new end |
Instance Attribute Details
#cache ⇒ Object (readonly)
Returns the value of attribute cache.
19 20 21 |
# File 'lib/sxn/config.rb', line 19 def cache @cache end |
#current_config ⇒ Object (readonly)
Returns the value of attribute current_config.
19 20 21 |
# File 'lib/sxn/config.rb', line 19 def current_config @current_config end |
#discovery ⇒ Object (readonly)
Returns the value of attribute discovery.
19 20 21 |
# File 'lib/sxn/config.rb', line 19 def discovery @discovery end |
#validator ⇒ Object (readonly)
Returns the value of attribute validator.
19 20 21 |
# File 'lib/sxn/config.rb', line 19 def validator @validator end |
Instance Method Details
#cache_stats ⇒ Hash
Get cache statistics
123 124 125 126 127 128 129 |
# File 'lib/sxn/config.rb', line 123 def cache_stats config_files = discovery.find_config_files cache.stats(config_files).merge( config_files: config_files, discovery_time: measure_discovery_time ) end |
#config(cli_options: {}, force_reload: false) ⇒ Hash
Get the current configuration with caching
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/sxn/config.rb', line 33 def config(cli_options: {}, force_reload: false) @config_mutex.synchronize do # Check if we need to reload due to file changes if @current_config && !force_reload config_files = discovery.find_config_files unless cache.valid?(config_files) @current_config = nil # Invalidate memory cache end end return @current_config if @current_config && !force_reload @current_config = load_and_validate_config(, force_reload) end end |
#config_file_paths ⇒ Array<String>
Get all configuration file paths in precedence order
142 143 144 |
# File 'lib/sxn/config.rb', line 142 def config_file_paths discovery.find_config_files end |
#debug_info ⇒ Hash
Get configuration summary for debugging
148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/sxn/config.rb', line 148 def debug_info config_files = discovery.find_config_files { start_directory: discovery.start_directory.to_s, config_files: config_files, cache_stats: cache.stats, validation_errors: validator.errors, environment_variables: discovery.send(:load_env_config), discovery_performance: measure_discovery_time } end |
#errors(cli_options: {}) ⇒ Array<String>
Get validation errors for current configuration
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/sxn/config.rb', line 102 def errors(cli_options: {}) # Try to load the actual configuration that would be used config(cli_options: ) [] # : Array[String] # If config() succeeds, there are no errors rescue ConfigurationError => e # Parse the validation errors from the exception message = e. if .include?("Configuration validation failed:") # Extract the numbered error list lines = .split("\n") errors = lines[1..].map { |line| line.strip.sub(/^\d+\.\s*/, "") } errors.reject(&:empty?) else [e.] end rescue StandardError => e [e.] end |
#get(key_path, default: nil) ⇒ Object
Get configuration value by key path
60 61 62 63 64 65 66 67 68 69 |
# File 'lib/sxn/config.rb', line 60 def get(key_path, default: nil) current_config = config keys = key_path.split(".") keys.reduce(current_config) do |current, key| break default unless current.is_a?(Hash) && current.key?(key) current[key] end end |
#invalidate_cache ⇒ Boolean
Invalidate configuration cache
133 134 135 136 137 138 |
# File 'lib/sxn/config.rb', line 133 def invalidate_cache @config_mutex.synchronize do @current_config = nil cache.invalidate end end |
#reload(cli_options: {}) ⇒ Hash
Reload configuration from disk
52 53 54 |
# File 'lib/sxn/config.rb', line 52 def reload(cli_options: {}) config(cli_options: , force_reload: true) end |
#set(key_path, value) ⇒ Object
Set configuration value by key path (for runtime modifications)
75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/sxn/config.rb', line 75 def set(key_path, value) @config_mutex.synchronize do # Don't call config() here as it would cause deadlock # Get the current config directly if it exists current_config = @current_config || load_and_validate_config({}, false) keys = key_path.split(".") target = keys[0..-2].reduce(current_config) do |current, key| current[key] ||= {} # : Hash[String, untyped] end target[keys.last] = value value end end |
#valid?(cli_options: {}) ⇒ Boolean
Check if configuration is valid
92 93 94 95 96 97 |
# File 'lib/sxn/config.rb', line 92 def valid?(cli_options: {}) config(cli_options: ) true rescue ConfigurationError false end |