Module: ContainerEnv

Defined in:
lib/container_env.rb,
lib/container_env/cache.rb,
lib/container_env/fetcher.rb,
lib/container_env/version.rb,
lib/container_env/configuration.rb

Overview

ENV wrapper with Docker secrets support, optional caching, and thread safety.

Defined Under Namespace

Classes: Cache, Configuration, Fetcher

Constant Summary collapse

VERSION =
'0.1.0'

Class Method Summary collapse

Class Method Details

.[](key) ⇒ Object



19
20
21
# File 'lib/container_env.rb', line 19

def [](key)
  fetcher[key]
end

.clear_cache!Object

Clears cached values without touching configuration or the fetcher. Use this in test after-hooks when pairing with ClimateControl (or any other tool that modifies ENV in-place), so the next fetch sees the restored ENV rather than a stale cached value.

RSpec.configure do |config|
  config.after { ContainerEnv.clear_cache! }
end


56
57
58
# File 'lib/container_env.rb', line 56

def clear_cache!
  mutex.synchronize { @cache&.clear }
end

.configurationObject



36
37
38
# File 'lib/container_env.rb', line 36

def configuration
  mutex.synchronize { @configuration ||= Configuration.new }
end

.configureObject

Yields configuration, then atomically rebuilds the cache and invalidates the fetcher so next call picks up the new settings. Holds the mutex for the entire block, preventing concurrent configuration reads mid-update (#5).



26
27
28
29
30
31
32
33
34
# File 'lib/container_env.rb', line 26

def configure
  mutex.synchronize do
    @configuration ||= Configuration.new
    yield @configuration
    @cache = Cache.new(ttl: @configuration.cache_ttl, max_size: @configuration.cache_max_size)
    @fetcher = nil # force rebuild so it adopts the new cache (#1)
  end
  nil
end

.fetch(key) ⇒ Object



15
16
17
# File 'lib/container_env.rb', line 15

def fetch(key, *, &)
  fetcher.fetch(key, *, &)
end

.reset!Object



40
41
42
43
44
45
46
# File 'lib/container_env.rb', line 40

def reset!
  mutex.synchronize do
    @configuration = nil
    @cache = nil
    @fetcher = nil
  end
end