A simple, configurable object container implemented in Ruby
⚠️ Notice that dry-container no longer depends on dry-configurable. If you happen to rely on the advanced configurable functionality, add dry-configurable as a dependency and require it before requiring dry-container.
#merge
(via #83) (@timriley)Configuration
mod (see #84 for more details) (@solnic).resolve
accepts an optional fallback block, similar to how Hash#fetch
works (flash-gordon)
container.resolve('missing_key') { :fallback } # => :fallback
.decorate
can (again) work with static values. Also, it can take a block instead of with
(flash-gordon)
container.register('key', 'value')
container.decorate('key') { |v| "<'#{v}'>" }
container.resolve('key') # => "<'value'>"
Symbols are now coerced to strings when resolving stubbed dependencies (cthulhu666)
Stubbing keys not present in container will raise an error (flash-gordon)
This means after upgrading you may see errors like this
ArgumentError (cannot stub "something" - no such key in container)
Be sure you register dependencies before using them. The new behavior will likely save quite a bit of time when debugging ill-configured container setups.
Namespace DSL resolves keys relative to the current namespace, see the corresponding changes (yuszuv)
Registered objects can be decorated with the following API (igor-alexandrov)
class CreateUser
def call(params)
# ...
end
end
container.register('create_user') { CreateUser.new }
container.decorate('create_user', with: ShinyLogger.new)
# Now subsequent resolutions will return a wrapped object
container.resolve('create_user')
# => #<ShinyLogger @obj=#<CreateUser:0x...>]>
Freezing a container now prevents further registrations (flash-gordon)