Skip to content

Commit 42195c8

Browse files
committed
Refactor #finalize! to the point where rubocop is happy
And yes, it was worth it
1 parent 1191f46 commit 42195c8

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

lib/dry/system/container.rb

+16-12
Original file line numberDiff line numberDiff line change
@@ -319,20 +319,18 @@ def finalize!(freeze: true, eager_load: true, &)
319319

320320
configured!
321321

322-
hooks[:before_finalize].each { |hook| instance_eval(&hook) }
323-
yield(self) if block_given?
322+
run_hooks(:finalize) do
323+
yield(self) if block_given?
324324

325-
providers.finalize!
326-
auto_registrar.finalize!
327-
manifest_registrar.finalize!
328-
importer.finalize!
325+
[providers, auto_registrar, manifest_registrar, importer].each(&:finalize!)
329326

330-
keys.each { |key| resolve(key) } if eager_load
327+
keys.each { resolve(_1) } if eager_load
331328

332-
@__finalized__ = true
329+
@__finalized__ = true
330+
331+
self.freeze if freeze
332+
end
333333

334-
self.freeze if freeze
335-
hooks[:after_finalize].each { |hook| instance_eval(&hook) }
336334
self
337335
end
338336

@@ -624,8 +622,8 @@ def load_component(key)
624622
load_local_component(component)
625623
elsif manifest_registrar.file_exists?(component)
626624
manifest_registrar.(component)
627-
elsif importer.namespace?(component.identifier.root_key)
628-
load_imported_component(component.identifier, namespace: component.identifier.root_key)
625+
elsif importer.namespace?(component.root_key)
626+
load_imported_component(component.identifier, namespace: component.root_key)
629627
elsif importer.namespace?(nil)
630628
load_imported_component(component.identifier, namespace: nil)
631629
end
@@ -661,6 +659,12 @@ def find_component(key)
661659
end
662660
} || IndirectComponent.new(Identifier.new(key))
663661
end
662+
663+
def run_hooks(event)
664+
hooks[:"before_#{event}"].each { instance_eval(&_1) }
665+
yield
666+
hooks[:"after_#{event}"].each { instance_eval(&_1) }
667+
end
664668
end
665669

666670
# Default hooks

0 commit comments

Comments
 (0)