-
-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
164 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# frozen_string_literal: true | ||
|
||
class Zeitwerk::Loader::Autoloads | ||
# @sig () -> void | ||
def initialize | ||
# Maps crefs for which an autoload has been set, to their respective | ||
# autoload paths. Entries look like this: | ||
# | ||
# #<Zeitwerk::Cref:... @mod=Object, @cname=:User, ...> => ".../app/models/user.rb" | ||
# #<Zeitwerk::Cref:... @mod=Hotel, @cname=:Pricing, ...> => ".../app/models/hotel/pricing.rb" | ||
# | ||
# I would expect this collection to not be necessary, since it is basically | ||
# doing what Module#autoload? already does. However, there is an edge case | ||
# in which Module#autoload? returns nil for an autoload just set. See | ||
# | ||
# https://bugs.ruby-lang.org/issues/21035 | ||
# | ||
# @sig Hash[Zeitwerk::Cref, String] | ||
@c2a = {} | ||
|
||
# This is the inverse of `c2a`. | ||
# | ||
# @sig Hash[String, Zeitwerk::Cref] | ||
@a2c = {} | ||
end | ||
|
||
# @sig (Zeitwerk::Cref, String) -> void | ||
def define(cref, autoload_path) | ||
cref.autoload(autoload_path) | ||
@c2a[cref] = autoload_path | ||
@a2c[autoload_path] = cref | ||
end | ||
|
||
# @sig () { () -> (Zeitwerk::Cref, String) } -> void | ||
def each(&block) | ||
@c2a.each(&block) | ||
end | ||
|
||
# @sig (Zeitwerk::Cref) -> String? | ||
def autoload_path_for(cref) | ||
@c2a[cref] | ||
end | ||
|
||
# @sig (String) -> Zeitwerk::Cref? | ||
def cref_for(autoload_path) | ||
@a2c[autoload_path] | ||
end | ||
|
||
# @sig (String) -> Zeitwerk::Cref? | ||
def delete(abspath) | ||
cref = @a2c.delete(abspath) | ||
@c2a.delete(cref) | ||
cref | ||
end | ||
|
||
# @sig () -> void | ||
def clear | ||
@c2a.clear | ||
@a2c.clear | ||
end | ||
|
||
# @sig () -> bool | ||
def empty? | ||
@c2a.empty? && @a2c.empty? | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# frozen_string_literal: true | ||
|
||
require "test_helper" | ||
|
||
class TestAutoloads < Minitest::Test | ||
def setup | ||
@autoloads = Zeitwerk::Loader::Autoloads.new | ||
@autoload_path = "/m.rb" | ||
@autoload_path2 = "/n.rb" | ||
@cref = Zeitwerk::Cref.new(Object, :M) | ||
@cref2 = Zeitwerk::Cref.new(Object, :N) | ||
end | ||
|
||
test "define defines an autoload" do | ||
on_teardown { remove_const :M } | ||
|
||
@autoloads.define(@cref, @autoload_path) | ||
assert_equal @autoload_path, @cref.autoload? | ||
end | ||
|
||
test "autoload_path_for returns the configured autoload path" do | ||
on_teardown { remove_const :M } | ||
|
||
@autoloads.define(@cref, @autoload_path) | ||
assert_equal @autoload_path, @autoloads.autoload_path_for(@cref) | ||
assert_nil @autoloads.autoload_path_for(@cref2) | ||
end | ||
|
||
test "cref_for returns the configured cref" do | ||
on_teardown { remove_const :M } | ||
|
||
@autoloads.define(@cref, @autoload_path) | ||
assert_equal @cref, @autoloads.cref_for(@autoload_path) | ||
assert_nil @autoloads.cref_for(@autoload_path2) | ||
end | ||
|
||
test "each iterates over the autoloads" do | ||
on_teardown { remove_const :M } | ||
|
||
@autoloads.define(@cref, @autoload_path) | ||
@autoloads.each do |cref, autoload_path| | ||
assert_equal @autoload_path, autoload_path | ||
assert_equal Object, cref.mod | ||
assert_equal :M, cref.cname | ||
end | ||
end | ||
|
||
test "delete maintains the two internal collections" do | ||
on_teardown { remove_const :M } | ||
|
||
@autoloads.define(@cref, @autoload_path) | ||
@autoloads.delete(@autoload_path) | ||
assert @autoloads.empty? | ||
end | ||
|
||
test "a new instance is empty" do | ||
assert @autoloads.empty? | ||
end | ||
|
||
test "an instance with definitions is not empty" do | ||
on_teardown { remove_const :M } | ||
|
||
@autoloads.define(@cref, @autoload_path) | ||
assert !@autoloads.empty? | ||
end | ||
|
||
test "a cleared instance is empty" do | ||
on_teardown { remove_const :M } | ||
|
||
@autoloads.define(@cref, @autoload_path) | ||
@autoloads.clear | ||
assert @autoloads.empty? | ||
end | ||
end |
Oops, something went wrong.