Skip to content

Commit

Permalink
Merge pull request #27 from mclark/default-namespace-support
Browse files Browse the repository at this point in the history
Default namespace support
  • Loading branch information
Philip Müller authored Apr 1, 2022
2 parents e78af0c + 32f964a commit ba7fd11
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 4 deletions.
17 changes: 13 additions & 4 deletions lib/constant_resolver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,9 @@ def camelize(string)
# )
def initialize(root_path:, load_paths:, inflector: DefaultInflector.new)
root_path += "/" unless root_path.end_with?("/")
load_paths = load_paths.map { |p| p.end_with?("/") ? p : p + "/" }.uniq

@root_path = root_path
@load_paths = load_paths
@load_paths = coerce_load_paths(load_paths)
@file_map = nil
@inflector = inflector
end
Expand Down Expand Up @@ -76,10 +75,11 @@ def file_map
@file_map = {}
duplicate_files = {}

@load_paths.each do |load_path|
@load_paths.each_pair do |load_path, default_ns|
Dir[glob_path(load_path)].each do |file_path|
root_relative_path = file_path.delete_prefix!(@root_path)
const_name = @inflector.camelize(root_relative_path.delete_prefix(load_path).delete_suffix!(".rb"))
const_name = "#{default_ns}::#{const_name}" unless default_ns == "Object"
existing_entry = @file_map[const_name]

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

Expand All @@ -119,6 +119,15 @@ def config

private

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.to_s.delete_prefix("::") }

load_paths
end

def ambiguous_constant_message(const_name, paths)
<<~MSG.chomp
"#{const_name}" could refer to any of
Expand Down
25 changes: 25 additions & 0 deletions test/constant_resolver_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,31 @@ def test_discovers_simple_constant
assert_equal("app/models/order.rb", constant.location)
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/internal" => "::Company::Internal",
},
)

constant = resolver.resolve("Order")
assert_equal("::Order", constant.name)
assert_equal("app/models/order.rb", constant.location)

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
context = @resolver.resolve("Order")
assert_instance_of(ConstantResolver::ConstantContext, 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# frozen_string_literal: true

module Api
class Repositories
end
end

0 comments on commit ba7fd11

Please sign in to comment.