Class: StratoEnv
- Inherits:
-
Object
- Object
- StratoEnv
- Defined in:
- lib/strato_env.rb,
lib/strato_env/version.rb,
lib/strato_env/ssm_fetcher.rb
Overview
StratoEnv loads layered configuration into ENV at boot.
A loader composes one or more paths worth of key/value pairs and writes them to ENV. The actual fetching of values is delegated to a fetcher, any callable that takes a path and returns a Hash<String, String>. The default fetcher is SSMFetcher, which reads from AWS SSM Parameter Store, but you can inject any callable (a Proc, a lambda, a Method, or a class with #call defined). This makes it easy to:
-
Test loader logic without touching AWS by passing a stub fetcher.
-
Add other backends (Secrets Manager, Vault, a YAML file) without changing the loader.
-
Preview values before applying them via #fetch.
When multiple paths are passed, they form layers: later paths override earlier ones (right-most wins on collision).
Defined Under Namespace
Classes: SSMFetcher
Constant Summary collapse
- VERSION =
"0.1.0".freeze
Class Method Summary collapse
Instance Method Summary collapse
-
#apply(values) ⇒ Array<String>
Write the given hash to ENV.
-
#fetch(paths:) ⇒ Hash<String, String>
Fetch values for the given paths and return them as a merged hash without touching ENV.
-
#initialize(fetcher: SSMFetcher.new) ⇒ StratoEnv
constructor
A new instance of StratoEnv.
-
#load(paths:) ⇒ Array<String>
Fetch values for the given paths and write them to ENV.
Constructor Details
#initialize(fetcher: SSMFetcher.new) ⇒ StratoEnv
Returns a new instance of StratoEnv.
40 41 42 |
# File 'lib/strato_env.rb', line 40 def initialize(fetcher: SSMFetcher.new) @fetcher = fetcher end |
Class Method Details
.apply(values) ⇒ Object
81 82 83 84 |
# File 'lib/strato_env.rb', line 81 def self.apply(values) values.each { |name, value| ENV[name] = value } values.keys end |
.fetch(paths:) ⇒ Object
78 |
# File 'lib/strato_env.rb', line 78 def self.fetch(paths:) = new.fetch(paths: paths) |
.load(paths:) ⇒ Object
75 |
# File 'lib/strato_env.rb', line 75 def self.load(paths:) = new.load(paths: paths) |
Instance Method Details
#apply(values) ⇒ Array<String>
Write the given hash to ENV. Useful for composing with #fetch when you want custom logic between fetching and applying (e.g. diffing across namespaces and warning on overrides).
72 |
# File 'lib/strato_env.rb', line 72 def apply(values) = self.class.apply(values) |
#fetch(paths:) ⇒ Hash<String, String>
Fetch values for the given paths and return them as a merged hash without touching ENV. Useful for previewing, testing, or applying the values to somewhere other than ENV.
62 63 64 |
# File 'lib/strato_env.rb', line 62 def fetch(paths:) Array(paths).map { |path| @fetcher.call(path) }.reduce({}, :merge) end |
#load(paths:) ⇒ Array<String>
Fetch values for the given paths and write them to ENV.
Equivalent to apply(fetch(paths: paths)).
51 52 53 |
# File 'lib/strato_env.rb', line 51 def load(paths:) apply(fetch(paths: paths)) end |