Class: Hanami::Settings::CompositeStore

Inherits:
Object
  • Object
show all
Defined in:
lib/hanami/settings/composite_store.rb

Overview

A settings store that chains multiple stores with fallback resolution.

Each store is tried in order. The first store to return a value wins. Stores must implement ‘#fetch` with the same signature as `Hash#fetch`.

Examples:

# config/app.rb
config.settings_store = Hanami::Settings::CompositeStore.new(
  Hanami::Settings::EnvStore.new,
  MyCustomStore.new
)

Since:

  • x.x.x

Constant Summary collapse

Undefined =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Since:

  • x.x.x

Dry::Core::Constants::Undefined

Instance Method Summary collapse

Constructor Details

#initialize(*stores) ⇒ CompositeStore

Returns a new instance of CompositeStore.

Parameters:

  • stores (Array<#fetch>)

    ordered list of stores to query

Since:

  • x.x.x



26
27
28
# File 'lib/hanami/settings/composite_store.rb', line 26

def initialize(*stores)
  @stores = stores
end

Instance Method Details

#fetch(name, *args) {|name| ... } ⇒ Object

Fetches a value by trying each store in order.

Parameters:

  • name (String, Symbol)

    the setting name

  • args (Array)

    optional default value

Yields:

  • (name)

    optional block for default value

Returns:

  • (Object)

    the setting value

Raises:

  • (KeyError)

    if no store has the key and no default is given

Since:

  • x.x.x



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/hanami/settings/composite_store.rb', line 40

def fetch(name, *args, &block)
  @stores.each do |store|
    value = store.fetch(name, Undefined)
    return value unless value.equal?(Undefined)
  end

  return args.first unless args.empty?
  return yield(name) if block

  raise KeyError, "key not found: #{name.inspect}"
end