Skip to content

Commit

Permalink
Make private root_dirs internal and rename to roots
Browse files Browse the repository at this point in the history
  • Loading branch information
fxn committed Nov 1, 2022
1 parent eb020fd commit af1d6ac
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 24 deletions.
6 changes: 3 additions & 3 deletions lib/zeitwerk/loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ def setup
mutex.synchronize do
break if @setup

actual_root_dirs.each do |root_dir, namespace|
set_autoloads_in_dir(root_dir, namespace)
actual_roots.each do |root_dir, root_namespace|
set_autoloads_in_dir(root_dir, root_namespace)
end

on_setup_callbacks.each(&:call)
Expand Down Expand Up @@ -447,7 +447,7 @@ def raise_if_conflicting_directory(dir)
next if loader == self
next if loader.__ignores?(dir)

loader.root_dirs.each do |root_dir, _namespace|
loader.__roots.each do |root_dir, _root_namespace|
next if ignores?(root_dir)

root_dir_slash = root_dir + "/"
Expand Down
22 changes: 11 additions & 11 deletions lib/zeitwerk/loader/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ module Zeitwerk::Loader::Config
# This is a private collection maintained by the loader. The public
# interface for it is `push_dir` and `dirs`.
#
# @private
# @sig Hash[String, Module]
attr_reader :root_dirs
attr_reader :roots
internal :roots

# Absolute paths of files, directories, or glob patterns to be totally
# ignored.
Expand Down Expand Up @@ -86,7 +86,7 @@ def initialize
@logger = self.class.default_logger
@tag = SecureRandom.hex(3)
@initialized_at = Time.now
@root_dirs = {}
@roots = {}
@ignored_glob_patterns = Set.new
@ignored_paths = Set.new
@collapse_glob_patterns = Set.new
Expand Down Expand Up @@ -115,7 +115,7 @@ def push_dir(path, namespace: Object)
abspath = File.expand_path(path)
if dir?(abspath)
raise_if_conflicting_directory(abspath)
root_dirs[abspath] = namespace
roots[abspath] = namespace
else
raise Zeitwerk::Error, "the root directory #{abspath} does not exist"
end
Expand Down Expand Up @@ -148,9 +148,9 @@ def tag=(tag)
# @sig () -> Array[String] | Hash[String, Module]
def dirs(namespaces: false)
if namespaces
root_dirs.clone
roots.clone
else
root_dirs.keys
roots.keys
end.freeze
end

Expand Down Expand Up @@ -284,22 +284,22 @@ def log!

walk_up(abspath) do |abspath|
return true if ignored_paths.member?(abspath)
return false if root_dirs.key?(abspath)
return false if roots.key?(abspath)
end

false
end

# @sig () -> Array[String]
private def actual_root_dirs
root_dirs.reject do |root_dir, _namespace|
private def actual_roots
roots.reject do |root_dir, _root_namespace|
!dir?(root_dir) || ignored_paths.member?(root_dir)
end
end

# @sig (String) -> bool
private def root_dir?(dir)
root_dirs.key?(dir)
roots.key?(dir)
end

# @sig (String) -> bool
Expand All @@ -309,7 +309,7 @@ def log!

walk_up(abspath) do |abspath|
return true if eager_load_exclusions.member?(abspath)
return false if root_dirs.key?(abspath)
return false if roots.key?(abspath)
end

false
Expand Down
10 changes: 5 additions & 5 deletions lib/zeitwerk/loader/eager_load.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ def eager_load(force: false)

log("eager load start") if logger

actual_root_dirs.each do |root_dir, namespace|
actual_eager_load_dir(root_dir, namespace, force: force)
actual_roots.each do |root_dir, root_namespace|
actual_eager_load_dir(root_dir, root_namespace, force: force)
end

autoloaded_dirs.each do |autoloaded_dir|
Expand All @@ -40,7 +40,7 @@ def eager_load_dir(path)
return if ignored_paths.member?(dir)
return if eager_load_exclusions.member?(dir)

break if root_namespace = root_dirs[dir]
break if root_namespace = roots[dir]

unless collapse?(dir)
basename = File.basename(dir)
Expand Down Expand Up @@ -76,7 +76,7 @@ def eager_load_namespace(mod)
mod_name = real_mod_name(mod)
return unless mod_name

actual_root_dirs.each do |root_dir, root_namespace|
actual_roots.each do |root_dir, root_namespace|
if mod.equal?(Object)
# A shortcircuiting test depends on the invocation of this method.
# Please keep them in sync if refactored.
Expand Down Expand Up @@ -122,7 +122,7 @@ def load_file(path)
walk_up(File.dirname(abspath)) do |dir|
raise Zeitwerk::Error.new("#{abspath} is ignored") if ignored_paths.member?(dir)

break if root_namespace = root_dirs[dir]
break if root_namespace = roots[dir]

unless collapse?(dir)
basename = File.basename(dir)
Expand Down
2 changes: 1 addition & 1 deletion lib/zeitwerk/loader/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def ls(dir)
next if ignored_paths.member?(abspath)

if dir?(abspath)
next if root_dirs.key?(abspath)
next if roots.key?(abspath)
next if !has_at_least_one_ruby_file?(abspath)
else
next unless ruby?(abspath)
Expand Down
8 changes: 4 additions & 4 deletions test/lib/zeitwerk/test_push_dir.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ class TesPushDir < LoaderTest
module Namespace; end

def check_dirs
root_dirs = loader.root_dirs # this is private interface
roots = loader.__roots

dirs = loader.dirs
assert_equal root_dirs.keys, dirs
assert_equal roots.keys, dirs
assert dirs.frozen?

dirs = loader.dirs(namespaces: true)
assert_equal root_dirs, dirs
assert_equal roots, dirs
assert dirs.frozen?
assert !dirs.equal?(root_dirs)
assert !dirs.equal?(roots)
end

test "accepts dirs as strings and associates them to the Object namespace" do
Expand Down

0 comments on commit af1d6ac

Please sign in to comment.