Skip to content

Commit ba7fd11

Browse files
author
Philip Müller
authored
Merge pull request #27 from mclark/default-namespace-support
Default namespace support
2 parents e78af0c + 32f964a commit ba7fd11

4 files changed

Lines changed: 52 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.to_s.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: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,31 @@ 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+
Object.const_set(:Api, Module.new)
47+
48+
resolver = ConstantResolver.new(
49+
root_path: "test/fixtures/constant_discovery/valid/",
50+
load_paths: {
51+
"app/models" => Object,
52+
"app/rest_api" => Api,
53+
"app/internal" => "::Company::Internal",
54+
},
55+
)
56+
57+
constant = resolver.resolve("Order")
58+
assert_equal("::Order", constant.name)
59+
assert_equal("app/models/order.rb", constant.location)
60+
61+
constant = resolver.resolve("Api::Repositories")
62+
assert_equal("::Api::Repositories", constant.name)
63+
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)
68+
end
69+
4570
def test_resolve_returns_constant_context
4671
context = @resolver.resolve("Order")
4772
assert_instance_of(ConstantResolver::ConstantContext, 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
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)