Skip to content

Commit e403a16

Browse files
committed
add support for default namespaces
1 parent e78af0c commit e403a16

3 files changed

Lines changed: 37 additions & 4 deletions

File tree

lib/constant_resolver.rb

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,9 @@ def camelize(string)
4141
# )
4242
def initialize(root_path:, load_paths:, inflector: DefaultInflector.new)
4343
root_path += "/" unless root_path.end_with?("/")
44-
load_paths = load_paths.map { |p| p.end_with?("/") ? p : p + "/" }.uniq
4544

4645
@root_path = root_path
47-
@load_paths = load_paths
46+
@load_paths = coerce_load_paths(load_paths)
4847
@file_map = nil
4948
@inflector = inflector
5049
end
@@ -76,10 +75,11 @@ def file_map
7675
@file_map = {}
7776
duplicate_files = {}
7877

79-
@load_paths.each do |load_path|
78+
@load_paths.each_pair do |load_path, default_ns|
8079
Dir[glob_path(load_path)].each do |file_path|
8180
root_relative_path = file_path.delete_prefix!(@root_path)
8281
const_name = @inflector.camelize(root_relative_path.delete_prefix(load_path).delete_suffix!(".rb"))
82+
const_name = "#{default_ns}::#{const_name}" unless default_ns == "Object"
8383
existing_entry = @file_map[const_name]
8484

8585
if existing_entry
@@ -102,7 +102,7 @@ def file_map
102102
raise(Error, <<~MSG)
103103
Could not find any ruby files. Searched in:
104104
105-
- #{@load_paths.map { |load_path| glob_path(load_path) }.join("\n- ")}
105+
- #{@load_paths.keys.map { |load_path| glob_path(load_path) }.join("\n- ")}
106106
MSG
107107
end
108108

@@ -119,6 +119,15 @@ def config
119119

120120
private
121121

122+
def coerce_load_paths(load_paths)
123+
load_paths = Hash[load_paths.map { |p| [p, "Object"] }] unless load_paths.respond_to?(:transform_keys)
124+
125+
load_paths.transform_keys! { |p| p.end_with?("/") ? p : p + "/" }
126+
load_paths.transform_values! { |ns| ns.delete_prefix("::") }
127+
128+
load_paths
129+
end
130+
122131
def ambiguous_constant_message(const_name, paths)
123132
<<~MSG.chomp
124133
"#{const_name}" could refer to any of

test/constant_resolver_test.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,24 @@ def test_discovers_simple_constant
4242
assert_equal("app/models/order.rb", constant.location)
4343
end
4444

45+
def test_resolves_constants_from_non_default_root_path_namespace
46+
resolver = ConstantResolver.new(
47+
root_path: "test/fixtures/constant_discovery/valid/",
48+
load_paths: {
49+
"app/models" => "::Object",
50+
"app/rest_api" => "::Api",
51+
},
52+
)
53+
54+
constant = resolver.resolve("Order")
55+
assert_equal("::Order", constant.name)
56+
assert_equal("app/models/order.rb", constant.location)
57+
58+
constant = resolver.resolve("Api::Repositories")
59+
assert_equal("::Api::Repositories", constant.name)
60+
assert_equal("app/rest_api/repositories.rb", constant.location)
61+
end
62+
4563
def test_resolve_returns_constant_context
4664
context = @resolver.resolve("Order")
4765
assert_instance_of(ConstantResolver::ConstantContext, context)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# frozen_string_literal: true
2+
3+
module Api
4+
class Repositories
5+
end
6+
end

0 commit comments

Comments
 (0)