Skip to content

Commit 8d11966

Browse files
committed
Introduce after_register hook
Allows you to take action when a new key is registered, so that you can decorate the key without knowing its name ahead of time. See also hanami/hanami#1360
1 parent 17dcff1 commit 8d11966

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

lib/dry/system/container.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,15 @@ def root
484484
config.root
485485
end
486486

487+
# @api public
488+
def register(key, *)
489+
super
490+
491+
hooks[:after_register].each { |hook| instance_exec(key, &hook) }
492+
493+
self
494+
end
495+
487496
# @api public
488497
def resolve(key)
489498
load_component(key) unless finalized?
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# frozen_string_literal: true
2+
3+
RSpec.describe Dry::System::Container do
4+
subject(:system) do
5+
Class.new(described_class)
6+
end
7+
8+
describe "after_register hook" do
9+
it "executes after a new key is registered" do
10+
expect { |hook|
11+
system.after(:register, &hook)
12+
system.register(:foo) { "bar" }
13+
}.to yield_with_args(:foo)
14+
end
15+
end
16+
end

0 commit comments

Comments
 (0)