aboutsummaryrefslogtreecommitdiffstats
path: root/railties
diff options
context:
space:
mode:
authorXavier Noria <fxn@hashref.com>2019-02-14 15:12:57 -0800
committerXavier Noria <fxn@hashref.com>2019-02-14 22:34:16 -0800
commit870377915af301c98a54f7f588e077610b2190aa (patch)
treebb370790a0da8075fef47e2574581c6ba595deb8 /railties
parent16e235126ef0187a93b81ee959a8127097e2f394 (diff)
downloadrails-870377915af301c98a54f7f588e077610b2190aa.tar.gz
rails-870377915af301c98a54f7f588e077610b2190aa.tar.bz2
rails-870377915af301c98a54f7f588e077610b2190aa.zip
Replace autoloader accessors with Rails.autoloaders.{main,once}
Rails.autoloader and Rails.once_autoloader was just tentative API good enough for a first patch. Rails.autoloader is singular and does not convey in its name that there is another autoloader. That might be confusing, for example if you set a logger and miss traces. On the other hand, the name `once_autoloader` is very close to being horrible. Rails.autoloaders.main and Rails.autoloaders.once read better for my taste, and have a nice symmetry. Also, both "main" and "once" are four letters long, short and same length. They are tagged as "rails.main" and "rails.once", respectively. References #35235.
Diffstat (limited to 'railties')
-rw-r--r--railties/lib/rails.rb19
-rw-r--r--railties/lib/rails/autoloaders.rb30
-rw-r--r--railties/lib/rails/engine.rb2
-rw-r--r--railties/test/application/configuration_test.rb27
-rw-r--r--railties/test/application/zeitwerk_integration_test.rb28
5 files changed, 65 insertions, 41 deletions
diff --git a/railties/lib/rails.rb b/railties/lib/rails.rb
index bca2cf34e1..1f533a8c04 100644
--- a/railties/lib/rails.rb
+++ b/railties/lib/rails.rb
@@ -13,6 +13,7 @@ require "active_support/core_ext/object/blank"
require "rails/application"
require "rails/version"
+require "rails/autoloaders"
require "active_support/railtie"
require "action_dispatch/railtie"
@@ -111,24 +112,8 @@ module Rails
application && Pathname.new(application.paths["public"].first)
end
- def autoloader
- if configuration.autoloader == :zeitwerk
- @autoloader ||= Zeitwerk::Loader.new
- end
- end
-
- def once_autoloader
- if configuration.autoloader == :zeitwerk
- @once_autoloader ||= Zeitwerk::Loader.new
- end
- end
-
def autoloaders
- if configuration.autoloader == :zeitwerk
- [autoloader, once_autoloader]
- else
- []
- end
+ Autoloaders
end
end
end
diff --git a/railties/lib/rails/autoloaders.rb b/railties/lib/rails/autoloaders.rb
new file mode 100644
index 0000000000..4b71f90c6b
--- /dev/null
+++ b/railties/lib/rails/autoloaders.rb
@@ -0,0 +1,30 @@
+module Rails
+ module Autoloaders # :nodoc:
+ class << self
+ include Enumerable
+
+ def main
+ if zeitwerk_enabled?
+ @main ||= Zeitwerk::Loader.new.tap { |loader| loader.tag = "rails.main" }
+ end
+ end
+
+ def once
+ if zeitwerk_enabled?
+ @once ||= Zeitwerk::Loader.new.tap { |loader| loader.tag = "rails.once" }
+ end
+ end
+
+ def each
+ if zeitwerk_enabled?
+ yield main
+ yield once
+ end
+ end
+
+ def zeitwerk_enabled?
+ Rails.configuration.autoloader == :zeitwerk
+ end
+ end
+ end
+end
diff --git a/railties/lib/rails/engine.rb b/railties/lib/rails/engine.rb
index 2485158a7b..76be6a8ac7 100644
--- a/railties/lib/rails/engine.rb
+++ b/railties/lib/rails/engine.rb
@@ -472,7 +472,7 @@ module Rails
# Eager load the application by loading all ruby
# files inside eager_load paths.
def eager_load!
- if Rails.autoloader
+ if Rails.autoloaders.zeitwerk_enabled?
eager_load_with_zeitwerk!
else
eager_load_with_dependencies!
diff --git a/railties/test/application/configuration_test.rb b/railties/test/application/configuration_test.rb
index f3161ad2a8..73773602a3 100644
--- a/railties/test/application/configuration_test.rb
+++ b/railties/test/application/configuration_test.rb
@@ -1157,23 +1157,30 @@ module ApplicationTests
end
end
- test "autoloader & autoloader=" do
+ test "autoloaders" do
app "development"
config = Rails.application.config
- assert_instance_of Zeitwerk::Loader, Rails.autoloader
- assert_instance_of Zeitwerk::Loader, Rails.once_autoloader
- assert_equal [Rails.autoloader, Rails.once_autoloader], Rails.autoloaders
+ assert Rails.autoloaders.zeitwerk_enabled?
+ assert_instance_of Zeitwerk::Loader, Rails.autoloaders.main
+ assert_equal "rails.main", Rails.autoloaders.main.tag
+ assert_instance_of Zeitwerk::Loader, Rails.autoloaders.once
+ assert_equal "rails.once", Rails.autoloaders.once.tag
+ assert_equal [Rails.autoloaders.main, Rails.autoloaders.once], Rails.autoloaders.to_a
config.autoloader = :classic
- assert_nil Rails.autoloader
- assert_nil Rails.once_autoloader
- assert_empty Rails.autoloaders
+ assert_not Rails.autoloaders.zeitwerk_enabled?
+ assert_nil Rails.autoloaders.main
+ assert_nil Rails.autoloaders.once
+ assert_equal 0, Rails.autoloaders.count
config.autoloader = :zeitwerk
- assert_instance_of Zeitwerk::Loader, Rails.autoloader
- assert_instance_of Zeitwerk::Loader, Rails.once_autoloader
- assert_equal [Rails.autoloader, Rails.once_autoloader], Rails.autoloaders
+ assert Rails.autoloaders.zeitwerk_enabled?
+ assert_instance_of Zeitwerk::Loader, Rails.autoloaders.main
+ assert_equal "rails.main", Rails.autoloaders.main.tag
+ assert_instance_of Zeitwerk::Loader, Rails.autoloaders.once
+ assert_equal "rails.once", Rails.autoloaders.once.tag
+ assert_equal [Rails.autoloaders.main, Rails.autoloaders.once], Rails.autoloaders.to_a
assert_raises(ArgumentError) { config.autoloader = :unknown }
end
diff --git a/railties/test/application/zeitwerk_integration_test.rb b/railties/test/application/zeitwerk_integration_test.rb
index 628a85acd8..16ffbe56bb 100644
--- a/railties/test/application/zeitwerk_integration_test.rb
+++ b/railties/test/application/zeitwerk_integration_test.rb
@@ -30,9 +30,10 @@ class ZeitwerkIntegrationTest < ActiveSupport::TestCase
boot
assert decorated?
- assert_instance_of Zeitwerk::Loader, Rails.autoloader
- assert_instance_of Zeitwerk::Loader, Rails.once_autoloader
- assert_equal [Rails.autoloader, Rails.once_autoloader], Rails.autoloaders
+ assert Rails.autoloaders.zeitwerk_enabled?
+ assert_instance_of Zeitwerk::Loader, Rails.autoloaders.main
+ assert_instance_of Zeitwerk::Loader, Rails.autoloaders.once
+ assert_equal [Rails.autoloaders.main, Rails.autoloaders.once], Rails.autoloaders.to_a
end
test "ActiveSupport::Dependencies is not decorated in classic mode" do
@@ -40,9 +41,10 @@ class ZeitwerkIntegrationTest < ActiveSupport::TestCase
boot
assert_not decorated?
- assert_nil Rails.autoloader
- assert_nil Rails.once_autoloader
- assert_empty Rails.autoloaders
+ assert_not Rails.autoloaders.zeitwerk_enabled?
+ assert_nil Rails.autoloaders.main
+ assert_nil Rails.autoloaders.once
+ assert_equal 0, Rails.autoloaders.count
end
test "constantize returns the value stored in the constant" do
@@ -136,8 +138,8 @@ class ZeitwerkIntegrationTest < ActiveSupport::TestCase
)
boot
- assert_not_includes Rails.autoloader.dirs, "#{app_path}/extras"
- assert_includes Rails.once_autoloader.dirs, "#{app_path}/extras"
+ assert_not_includes Rails.autoloaders.main.dirs, "#{app_path}/extras"
+ assert_includes Rails.autoloaders.once.dirs, "#{app_path}/extras"
end
test "clear reloads the main autoloader, and does not reload the once one" do
@@ -145,13 +147,13 @@ class ZeitwerkIntegrationTest < ActiveSupport::TestCase
$zeitwerk_integration_reload_test = []
- autoloader = Rails.autoloader
- def autoloader.reload
- $zeitwerk_integration_reload_test << :autoloader
+ main_autoloader = Rails.autoloaders.main
+ def main_autoloader.reload
+ $zeitwerk_integration_reload_test << :main_autoloader
super
end
- once_autoloader = Rails.once_autoloader
+ once_autoloader = Rails.autoloaders.once
def once_autoloader.reload
$zeitwerk_integration_reload_test << :once_autoloader
super
@@ -159,6 +161,6 @@ class ZeitwerkIntegrationTest < ActiveSupport::TestCase
ActiveSupport::Dependencies.clear
- assert_equal %i(autoloader), $zeitwerk_integration_reload_test
+ assert_equal %i(main_autoloader), $zeitwerk_integration_reload_test
end
end