Skip to content

Commit 32f964a

Browse files
committed
allow both objects and string namespaces
1 parent e403a16 commit 32f964a

3 files changed

Lines changed: 18 additions & 3 deletions

File tree

lib/constant_resolver.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ def coerce_load_paths(load_paths)
123123
load_paths = Hash[load_paths.map { |p| [p, "Object"] }] unless load_paths.respond_to?(:transform_keys)
124124

125125
load_paths.transform_keys! { |p| p.end_with?("/") ? p : p + "/" }
126-
load_paths.transform_values! { |ns| ns.delete_prefix("::") }
126+
load_paths.transform_values! { |ns| ns.to_s.delete_prefix("::") }
127127

128128
load_paths
129129
end

test/constant_resolver_test.rb

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,14 @@ def test_discovers_simple_constant
4343
end
4444

4545
def test_resolves_constants_from_non_default_root_path_namespace
46+
Object.const_set(:Api, Module.new)
47+
4648
resolver = ConstantResolver.new(
4749
root_path: "test/fixtures/constant_discovery/valid/",
4850
load_paths: {
49-
"app/models" => "::Object",
50-
"app/rest_api" => "::Api",
51+
"app/models" => Object,
52+
"app/rest_api" => Api,
53+
"app/internal" => "::Company::Internal",
5154
},
5255
)
5356

@@ -58,6 +61,10 @@ def test_resolves_constants_from_non_default_root_path_namespace
5861
constant = resolver.resolve("Api::Repositories")
5962
assert_equal("::Api::Repositories", constant.name)
6063
assert_equal("app/rest_api/repositories.rb", constant.location)
64+
65+
constant = resolver.resolve("Company::Internal::Main")
66+
assert_equal("::Company::Internal::Main", constant.name)
67+
assert_equal("app/internal/main.rb", constant.location)
6168
end
6269

6370
def test_resolve_returns_constant_context
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# frozen_string_literal: true
2+
3+
module Company
4+
module Internal
5+
class Main
6+
end
7+
end
8+
end

0 commit comments

Comments
 (0)