Class: Kotoshu::Configuration::Resolver
- Inherits:
-
Object
- Object
- Kotoshu::Configuration::Resolver
- Defined in:
- lib/kotoshu/configuration/resolver.rb
Overview
Canon-style resolver for configuration values.
Implements the priority order: CLI > ENV > Programmatic > Defaults
This ensures that:
-
CLI arguments have highest priority (explicit user intent)
-
Environment variables override programmatic settings
-
Programmatic/API settings override defaults
-
Defaults are the fallback
Instance Attribute Summary collapse
-
#cli ⇒ Hash
readonly
CLI argument settings.
-
#defaults ⇒ Hash
readonly
Default values.
-
#env ⇒ Hash
readonly
Environment variable overrides.
-
#programmatic ⇒ Hash
readonly
Programmatic/API settings.
Instance Method Summary collapse
-
#get(key) ⇒ Object
Resolve a configuration value using priority order.
-
#get_all(key) ⇒ Hash
Get all values for a key across all priority levels.
-
#initialize(env: {}, programmatic: {}, cli: {}, defaults: {}) ⇒ Resolver
constructor
Create a new resolver.
-
#key?(key) ⇒ Boolean
Check if a key has a value set at any priority level.
-
#merge(env: {}, programmatic: {}, cli: {}) ⇒ Resolver
Create a new resolver with merged values.
Constructor Details
#initialize(env: {}, programmatic: {}, cli: {}, defaults: {}) ⇒ Resolver
Create a new resolver.
44 45 46 47 48 49 |
# File 'lib/kotoshu/configuration/resolver.rb', line 44 def initialize(env: {}, programmatic: {}, cli: {}, defaults: {}) @env = env @programmatic = programmatic @cli = cli @defaults = defaults end |
Instance Attribute Details
#cli ⇒ Hash (readonly)
Returns CLI argument settings.
33 34 35 |
# File 'lib/kotoshu/configuration/resolver.rb', line 33 def cli @cli end |
#defaults ⇒ Hash (readonly)
Returns Default values.
36 37 38 |
# File 'lib/kotoshu/configuration/resolver.rb', line 36 def defaults @defaults end |
#env ⇒ Hash (readonly)
Returns Environment variable overrides.
27 28 29 |
# File 'lib/kotoshu/configuration/resolver.rb', line 27 def env @env end |
#programmatic ⇒ Hash (readonly)
Returns Programmatic/API settings.
30 31 32 |
# File 'lib/kotoshu/configuration/resolver.rb', line 30 def programmatic @programmatic end |
Instance Method Details
#get(key) ⇒ Object
Resolve a configuration value using priority order.
Priority: CLI > ENV > Programmatic > Defaults
60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/kotoshu/configuration/resolver.rb', line 60 def get(key) # CLI has highest priority when explicitly set return @cli[key] if @cli.key?(key) # Environment variables override programmatic env_key = env_key_for(key) return ENV[env_key] if ENV.key?(env_key) # Programmatic settings override defaults return @programmatic[key] if @programmatic.key?(key) @defaults[key] end |
#get_all(key) ⇒ Hash
Get all values for a key across all priority levels.
89 90 91 92 93 94 95 96 |
# File 'lib/kotoshu/configuration/resolver.rb', line 89 def get_all(key) { cli: @cli[key], env: ENV[env_key_for(key)], programmatic: @programmatic[key], default: @defaults[key] } end |
#key?(key) ⇒ Boolean
Check if a key has a value set at any priority level.
78 79 80 81 82 83 |
# File 'lib/kotoshu/configuration/resolver.rb', line 78 def key?(key) @cli.key?(key) || ENV.key?(env_key_for(key)) || @programmatic.key?(key) || @defaults.key?(key) end |
#merge(env: {}, programmatic: {}, cli: {}) ⇒ Resolver
Create a new resolver with merged values.
104 105 106 107 108 109 110 111 |
# File 'lib/kotoshu/configuration/resolver.rb', line 104 def merge(env: {}, programmatic: {}, cli: {}) self.class.new( env: @env.merge(env), programmatic: @programmatic.merge(programmatic), cli: @cli.merge(cli), defaults: @defaults ) end |