From 299e25bb4d497d06d2b4cc6db621c0dff464f323 Mon Sep 17 00:00:00 2001 From: Tim Riley Date: Sun, 26 Jun 2022 10:20:03 +1000 Subject: [PATCH] Accept block given to `#merge` This will allow greater control to the caller over whether to favor new items or old items. --- lib/dry/container/mixin.rb | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/lib/dry/container/mixin.rb b/lib/dry/container/mixin.rb index 3e60b7c..6b57b10 100644 --- a/lib/dry/container/mixin.rb +++ b/lib/dry/container/mixin.rb @@ -145,15 +145,16 @@ def [](key) # @return [Dry::Container::Mixin] self # # @api public - def merge(other, namespace: nil) + def merge(other, namespace: nil, &block) if namespace _container.merge!( - other._container.each_with_object(::Concurrent::Hash.new) do |a, h| - h[PREFIX_NAMESPACE.call(namespace, a.first, config)] = a.last - end + other._container.each_with_object(::Concurrent::Hash.new) { |(key, item), hsh| + hsh[PREFIX_NAMESPACE.call(namespace, key, config)] = item + }, + &block ) else - _container.merge!(other._container) + _container.merge!(other._container, &block) end self