Class: Appom::Configuration::Config
- Inherits:
-
Object
- Object
- Appom::Configuration::Config
- Includes:
- Logging
- Defined in:
- lib/appom/configuration.rb
Overview
Configuration management with environment-specific settings
Constant Summary collapse
- DEFAULT_CONFIG_FILE =
'appom.yml'- DEFAULT_CONFIG_PATHS =
[ './config/appom.yml', './appom.yml', './test/appom.yml', './spec/appom.yml', ].freeze
Instance Attribute Summary collapse
-
#config_file ⇒ Object
readonly
Returns the value of attribute config_file.
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#environment ⇒ Object
readonly
Returns the value of attribute environment.
Instance Method Summary collapse
-
#get(key_path, default = nil) ⇒ Object
Get configuration value with dot notation.
-
#initialize(config_file: nil, environment: nil) ⇒ Config
constructor
A new instance of Config.
-
#key?(key_path) ⇒ Boolean
Check if configuration key exists.
-
#merge!(other_config) ⇒ Config
Merge configuration hash.
-
#reload! ⇒ Config
Reload configuration from file.
-
#save!(file_path = nil) ⇒ void
Save current configuration to file.
-
#set(key_path, value) ⇒ Object
Set configuration value with dot notation.
-
#to_h ⇒ Hash
Get all configuration as hash.
-
#validate! ⇒ Boolean
Validate configuration against schema.
Methods included from Logging
level, level=, #log_debug, #log_element_action, #log_error, #log_info, #log_wait_end, #log_wait_start, #log_warn, #logger
Constructor Details
#initialize(config_file: nil, environment: nil) ⇒ Config
Returns a new instance of Config.
24 25 26 27 28 29 |
# File 'lib/appom/configuration.rb', line 24 def initialize(config_file: nil, environment: nil) @config_file = config_file || find_config_file @environment = environment || detect_environment @data = {} load_configuration end |
Instance Attribute Details
#config_file ⇒ Object (readonly)
Returns the value of attribute config_file.
22 23 24 |
# File 'lib/appom/configuration.rb', line 22 def config_file @config_file end |
#data ⇒ Object (readonly)
Returns the value of attribute data.
22 23 24 |
# File 'lib/appom/configuration.rb', line 22 def data @data end |
#environment ⇒ Object (readonly)
Returns the value of attribute environment.
22 23 24 |
# File 'lib/appom/configuration.rb', line 22 def environment @environment end |
Instance Method Details
#get(key_path, default = nil) ⇒ Object
Get configuration value with dot notation
40 41 42 43 44 |
# File 'lib/appom/configuration.rb', line 40 def get(key_path, default = nil) keys = key_path.to_s.split('.') value = keys.reduce(@data) { |hash, key| hash&.dig(key) } value.nil? ? default : value end |
#key?(key_path) ⇒ Boolean
Check if configuration key exists
143 144 145 |
# File 'lib/appom/configuration.rb', line 143 def key?(key_path) !get(key_path).nil? end |
#merge!(other_config) ⇒ Config
Merge configuration hash
73 74 75 76 |
# File 'lib/appom/configuration.rb', line 73 def merge!(other_config) @data = deep_merge(@data, other_config) self end |
#reload! ⇒ Config
Reload configuration from file
84 85 86 87 88 |
# File 'lib/appom/configuration.rb', line 84 def reload! reload_configuration log_info("Configuration reloaded from #{@config_file}") self end |
#save!(file_path = nil) ⇒ void
This method returns an undefined value.
Save current configuration to file
118 119 120 121 122 123 |
# File 'lib/appom/configuration.rb', line 118 def save!(file_path = nil) target_file = file_path || @config_file File.write(target_file, YAML.dump(@data)) log_info("Configuration saved to #{target_file}") end |
#set(key_path, value) ⇒ Object
Set configuration value with dot notation
55 56 57 58 59 60 61 62 63 64 |
# File 'lib/appom/configuration.rb', line 55 def set(key_path, value) keys = key_path.to_s.split('.') last_key = keys.pop target = keys.reduce(@data) do |hash, key| hash[key] ||= {} end target[last_key] = value end |
#to_h ⇒ Hash
Get all configuration as hash
131 132 133 |
# File 'lib/appom/configuration.rb', line 131 def to_h @data.dup end |
#validate! ⇒ Boolean
Validate configuration against schema
97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/appom/configuration.rb', line 97 def validate! # rubocop:disable Naming/PredicateMethod schema = ConfigSchema.new errors = schema.validate(@data) if errors.any? raise Appom::ConfigurationError.new('validation', @data, "Configuration validation failed: #{errors.join(', ')}",) end log_info('Configuration validation passed') true end |