Class: Archaeo::Configuration
- Inherits:
-
Object
- Object
- Archaeo::Configuration
- Defined in:
- lib/archaeo/configuration.rb
Overview
Manages persistent configuration across sessions.
Loads settings from .archaeo.yml files, supports named profiles, and falls back to sensible defaults. Settings cascade: defaults < global config < profile overrides.
Constant Summary collapse
- DEFAULTS =
{ "output_dir" => "archive", "format" => "table", "rate_limit" => 0, "concurrency" => 1, "max_retries" => 3, }.freeze
Instance Method Summary collapse
- #get(key, profile: nil) ⇒ Object
-
#initialize(path: ".archaeo.yml") ⇒ Configuration
constructor
A new instance of Configuration.
- #profile(name) ⇒ Object
- #profiles ⇒ Object
- #save(path: nil) ⇒ Object
- #set(key, value, profile: nil) ⇒ Object
- #to_h ⇒ Object
Constructor Details
#initialize(path: ".archaeo.yml") ⇒ Configuration
Returns a new instance of Configuration.
20 21 22 23 |
# File 'lib/archaeo/configuration.rb', line 20 def initialize(path: ".archaeo.yml") @path = path @data = load_config end |
Instance Method Details
#get(key, profile: nil) ⇒ Object
25 26 27 28 29 |
# File 'lib/archaeo/configuration.rb', line 25 def get(key, profile: nil) keys = key.to_s.split(".") value = dig_nested(@data, keys, profile) value.nil? ? DEFAULTS[keys.last] : value end |
#profile(name) ⇒ Object
31 32 33 34 |
# File 'lib/archaeo/configuration.rb', line 31 def profile(name) profiles = @data["profiles"] || {} profiles[name.to_s] || {} end |
#profiles ⇒ Object
36 37 38 |
# File 'lib/archaeo/configuration.rb', line 36 def profiles (@data["profiles"] || {}).keys end |
#save(path: nil) ⇒ Object
59 60 61 62 |
# File 'lib/archaeo/configuration.rb', line 59 def save(path: nil) target = path || @path File.write(target, YAML.dump(@data)) end |
#set(key, value, profile: nil) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/archaeo/configuration.rb', line 40 def set(key, value, profile: nil) if profile @data["profiles"] ||= {} @data["profiles"][profile.to_s] ||= {} @data["profiles"][profile.to_s][key.to_s] = value else @data["defaults"] ||= {} @data["defaults"][key.to_s] = value end save_config end |
#to_h ⇒ Object
52 53 54 55 56 57 |
# File 'lib/archaeo/configuration.rb', line 52 def to_h { defaults: @data.fetch("defaults", {}), profiles: @data.fetch("profiles", {}), } end |