Skip to content

Commit b96e64c

Browse files
committed
Revert "Revert eager_load option in Container#finalize! (fix #281)"
This reverts commit 7b8ae92.
1 parent ddb8d29 commit b96e64c

File tree

4 files changed

+62
-1
lines changed

4 files changed

+62
-1
lines changed

lib/dry/system/container.rb

+3-1
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ def finalized?
314314
# @return [self] frozen container
315315
#
316316
# @api public
317-
def finalize!(freeze: true, &)
317+
def finalize!(freeze: true, eager_load: true, &)
318318
return self if finalized?
319319

320320
configured!
@@ -324,6 +324,8 @@ def finalize!(freeze: true, &)
324324

325325
[providers, auto_registrar, manifest_registrar, importer].each(&:finalize!)
326326

327+
keys.each { resolve(_1) } if eager_load
328+
327329
@__finalized__ = true
328330

329331
self.freeze if freeze
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# frozen_string_literal: true
2+
3+
RSpec.describe "Eager loading during finalization" do
4+
it "raises error when component cannot be found, due to missing inflection" do
5+
class Test::Container < Dry::System::Container
6+
configure do |config|
7+
config.root = SPEC_ROOT.join("fixtures").realpath
8+
9+
config.component_dirs.add "components" do |dir|
10+
dir.namespaces.add "test", key: nil
11+
end
12+
end
13+
end
14+
expect { Test::Container.finalize! }.to raise_error(Dry::System::ComponentNotLoadableError)
15+
end
16+
17+
it "does not raise error when constant can be found" do
18+
class Test::Container < Dry::System::Container
19+
configure do |config|
20+
config.root = SPEC_ROOT.join("fixtures").realpath
21+
22+
config.component_dirs.add "components" do |dir|
23+
dir.namespaces.add "test", key: nil
24+
end
25+
26+
config.inflector = Dry::Inflector.new { |i| i.acronym("ABC") }
27+
end
28+
end
29+
expect { Test::Container.finalize! }.to_not raise_error
30+
end
31+
end

spec/integration/container/auto_registration/memoize_spec.rb

+4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ class Test::Container < Dry::System::Container
1717
dir.namespaces.add "test", key: nil
1818
dir.memoize = true
1919
end
20+
21+
config.inflector = Dry::Inflector.new { |i| i.acronym("ABC") }
2022
end
2123
end
2224
end
@@ -52,6 +54,8 @@ class Test::Container < Dry::System::Container
5254
!component.key.match?(/bar/)
5355
end
5456
end
57+
58+
config.inflector = Dry::Inflector.new { |i| i.acronym("ABC") }
5559
end
5660
end
5761
end

spec/unit/container/configuration_spec.rb

+24
Original file line numberDiff line numberDiff line change
@@ -175,5 +175,29 @@ def hooks_trace
175175
.from([])
176176
.to [:after_configure]
177177
end
178+
179+
it "raises error for undefined constant (due to inflector missing acronym)" do
180+
expect {
181+
container.configure do |config|
182+
config.root = SPEC_ROOT.join("fixtures").realpath
183+
config.component_dirs.add "components" do |dir|
184+
dir.namespaces.add "test", key: nil
185+
end
186+
end
187+
container.finalize!
188+
}.to raise_error(Dry::System::ComponentNotLoadableError)
189+
end
190+
191+
it "does not raises error for undefined constant when eager_load is false" do
192+
expect {
193+
container.configure do |config|
194+
config.root = SPEC_ROOT.join("fixtures").realpath
195+
config.component_dirs.add "components" do |dir|
196+
dir.namespaces.add "test", key: nil
197+
end
198+
end
199+
container.finalize!(eager_load: false)
200+
}.to_not raise_error
201+
end
178202
end
179203
end

0 commit comments

Comments
 (0)