Skip to content

Commit 9a28668

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 f826656 commit 9a28668

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
@@ -486,6 +486,15 @@ def root
486486
config.root
487487
end
488488

489+
# @api public
490+
def register(key, *)
491+
super
492+
493+
hooks[:after_register].each { |hook| instance_exec(key, &hook) }
494+
495+
self
496+
end
497+
489498
# @api public
490499
def resolve(key)
491500
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)