Skip to content

Commit

Permalink
allow both objects and string namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
mclark committed Mar 29, 2022
1 parent e403a16 commit 32f964a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/constant_resolver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def coerce_load_paths(load_paths)
load_paths = Hash[load_paths.map { |p| [p, "Object"] }] unless load_paths.respond_to?(:transform_keys)

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

load_paths
end
Expand Down
11 changes: 9 additions & 2 deletions test/constant_resolver_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,14 @@ def test_discovers_simple_constant
end

def test_resolves_constants_from_non_default_root_path_namespace
Object.const_set(:Api, Module.new)

resolver = ConstantResolver.new(
root_path: "test/fixtures/constant_discovery/valid/",
load_paths: {
"app/models" => "::Object",
"app/rest_api" => "::Api",
"app/models" => Object,
"app/rest_api" => Api,
"app/internal" => "::Company::Internal",
},
)

Expand All @@ -58,6 +61,10 @@ def test_resolves_constants_from_non_default_root_path_namespace
constant = resolver.resolve("Api::Repositories")
assert_equal("::Api::Repositories", constant.name)
assert_equal("app/rest_api/repositories.rb", constant.location)

constant = resolver.resolve("Company::Internal::Main")
assert_equal("::Company::Internal::Main", constant.name)
assert_equal("app/internal/main.rb", constant.location)
end

def test_resolve_returns_constant_context
Expand Down
8 changes: 8 additions & 0 deletions test/fixtures/constant_discovery/valid/app/internal/main.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

module Company
module Internal
class Main
end
end
end

0 comments on commit 32f964a

Please sign in to comment.