Skip to content

Commit 2065557

Browse files
authored
Fix lazy loading of imported components (#287)
This allows imported components to be found when both strings _and_ symbols are given as the namespace in `Container.import`.
1 parent 5ad6767 commit 2065557

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

lib/dry/system/importer.rb

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def initialize(container)
3636

3737
# @api private
3838
def register(namespace:, container:, keys: nil)
39-
registry[namespace] = Item.new(
39+
registry[namespace_key(namespace)] = Item.new(
4040
namespace: namespace,
4141
container: container,
4242
import_keys: keys
@@ -45,12 +45,12 @@ def register(namespace:, container:, keys: nil)
4545

4646
# @api private
4747
def [](name)
48-
registry.fetch(name)
48+
registry.fetch(namespace_key(name))
4949
end
5050

5151
# @api private
5252
def key?(name)
53-
registry.key?(name)
53+
registry.key?(namespace_key(name))
5454
end
5555
alias_method :namespace?, :key?
5656

@@ -76,6 +76,17 @@ def import(namespace, keys: Undefined)
7676

7777
private
7878

79+
# Returns nil if given a nil (i.e. root) namespace. Otherwise, converts the namespace to a
80+
# string.
81+
#
82+
# This ensures imported components are found when either symbols or strings to given as the
83+
# namespace in {Container.import}.
84+
def namespace_key(namespace)
85+
return nil if namespace.nil?
86+
87+
namespace.to_s
88+
end
89+
7990
def keys_to_import(keys, item)
8091
keys
8192
.then { (arr = item.import_keys) ? _1 & arr : _1 }

0 commit comments

Comments
 (0)