Skip to content

Commit a54e833

Browse files
committed
Run after_finalize hooks before freeze
This hook is much less useful that it might be if you could still make changes to the container when it ran.
1 parent 9a28668 commit a54e833

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

lib/dry/system/container.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,8 +329,8 @@ def finalize!(freeze: true, &block)
329329

330330
@__finalized__ = true
331331

332-
self.freeze if freeze
333332
hooks[:after_finalize].each { |hook| instance_eval(&hook) }
333+
self.freeze if freeze
334334
self
335335
end
336336

spec/unit/container/hooks/after_hooks_spec.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,33 @@
1212
system.register(:foo) { "bar" }
1313
}.to yield_with_args(:foo)
1414
end
15+
16+
it "provides the fully-qualified key" do
17+
expect { |hook|
18+
system.after(:register, &hook)
19+
system.namespace :foo do
20+
register(:bar) { "baz" }
21+
end
22+
}.to yield_with_args("foo.bar")
23+
end
24+
end
25+
26+
describe "after_finalize hook" do
27+
it "executes after finalization" do
28+
expect { |hook|
29+
system.after(:finalize, &hook)
30+
system.finalize!
31+
}.to yield_control
32+
end
33+
34+
it "executes before the container is frozen" do
35+
is_frozen = nil
36+
37+
system.after(:finalize) { is_frozen = frozen? }
38+
system.finalize!
39+
40+
expect(is_frozen).to eq false
41+
expect(system).to be_frozen
42+
end
1543
end
1644
end

0 commit comments

Comments
 (0)