Class: ParamStore::Wrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/param_store/wrapper.rb

Instance Method Summary collapse

Constructor Details

#initialize(adapter_class, **opts) ⇒ Wrapper

Returns a new instance of Wrapper.



3
4
5
6
# File 'lib/param_store/wrapper.rb', line 3

def initialize(adapter_class, **opts)
  @adapter_class = adapter_class
  @opts = opts
end

Instance Method Details

#copy_to_env(*keys, **opts) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/param_store/wrapper.rb', line 17

def copy_to_env(*keys, **opts)
  require_keys = opts.delete(:require_keys)

  cache_all(*keys, **opts)

  require_keys!(*keys, **opts) if require_keys

  keys.each { |key| ENV[key] = cache[key] }
end

#fetch(key, *args, **opts, &block) ⇒ Object



8
9
10
11
12
13
14
15
# File 'lib/param_store/wrapper.rb', line 8

def fetch(key, *args, **opts, &block)
  key = key.to_s
  unless cache.key?(key)
    # cache params to minimize number of requests
    cache[key] = adapter_instance.fetch(key, *args, **opts, &block)
  end
  cache[key]
end

#require_keys!(*keys, **opts) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/param_store/wrapper.rb', line 27

def require_keys!(*keys, **opts)
  cache_all(*keys, **opts)

  missing = keys.flatten.map!(&:to_s) - cache.keys

  return if missing.none?

  raise "Missing keys: #{missing.join(', ')}"
end