Class: Appom::Configuration::Config

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

Instance Method Summary collapse

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_fileObject (readonly)

Returns the value of attribute config_file.



22
23
24
# File 'lib/appom/configuration.rb', line 22

def config_file
  @config_file
end

#dataObject (readonly)

Returns the value of attribute data.



22
23
24
# File 'lib/appom/configuration.rb', line 22

def data
  @data
end

#environmentObject (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

Examples:

Get a nested value

config.get('appom.max_wait_time', 30)
config.get('appium.server_url')

Parameters:

  • key_path (String, Symbol)

    The configuration key path using dot notation

  • default (Object) (defaults to: nil)

    Default value to return if key is not found

Returns:

  • (Object)

    The configuration value or default



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

Examples:

Check if key exists

config.key?('appom.max_wait_time') # => true
config.key?('missing.key')         # => false

Parameters:

  • key_path (String, Symbol)

    The configuration key path using dot notation

Returns:

  • (Boolean)

    True if key exists, false otherwise



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

Examples:

Merge additional configuration

config.merge!('appom' => { 'log_level' => 'debug' })

Parameters:

  • other_config (Hash)

    Configuration hash to merge

Returns:

  • (Config)

    Self for method chaining



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

Examples:

Reload configuration after file changes

config.reload!

Returns:

  • (Config)

    Self for method chaining



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

Examples:

Save configuration

config.save!
config.save!('backup_config.yml')

Parameters:

  • file_path (String, nil) (defaults to: nil)

    Optional file path to save to, uses current config file if nil



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

Examples:

Set a nested value

config.set('appom.max_wait_time', 45)
config.set('custom.setting', 'value')

Parameters:

  • key_path (String, Symbol)

    The configuration key path using dot notation

  • value (Object)

    The value to set

Returns:

  • (Object)

    The value that was set



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_hHash

Get all configuration as hash

Examples:

Get configuration as hash

config_hash = config.to_h

Returns:

  • (Hash)

    Deep copy of configuration data



131
132
133
# File 'lib/appom/configuration.rb', line 131

def to_h
  @data.dup
end

#validate!Boolean

Validate configuration against schema

Examples:

Validate current configuration

config.validate!

Returns:

  • (Boolean)

    True if validation passes

Raises:



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