Skip to content

Commit

Permalink
Run after_finalize hooks before freeze
Browse files Browse the repository at this point in the history
This hook is much less useful that it might be if you could still make
changes to the container when it ran.
  • Loading branch information
alassek committed Feb 24, 2025
1 parent 8d11966 commit 437665e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/dry/system/container.rb
Original file line number Diff line number Diff line change
Expand Up @@ -325,10 +325,10 @@ def finalize!(freeze: true, &)
[providers, auto_registrar, manifest_registrar, importer].each(&:finalize!)

@__finalized__ = true

self.freeze if freeze
end

self.freeze if freeze

self
end

Expand Down
28 changes: 28 additions & 0 deletions spec/unit/container/hooks/after_hooks_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,33 @@
system.register(:foo) { "bar" }
}.to yield_with_args(:foo)
end

it "provides the fully-qualified key" do
expect { |hook|
system.after(:register, &hook)
system.namespace :foo do
register(:bar) { "baz" }
end
}.to yield_with_args("foo.bar")
end
end

describe "after_finalize hook" do
it "executes after finalization" do
expect { |hook|
system.after(:finalize, &hook)
system.finalize!
}.to yield_control
end

it "executes before the container is frozen" do
is_frozen = nil

system.after(:finalize) { is_frozen = frozen? }
system.finalize!

expect(is_frozen).to eq false
expect(system).to be_frozen
end
end
end

0 comments on commit 437665e

Please sign in to comment.