aboutsummaryrefslogtreecommitdiffstats
path: root/railties/lib/rails/autoloaders.rb
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/lib/rails/autoloaders.rb
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/lib/rails/autoloaders.rb')
-rw-r--r--railties/lib/rails/autoloaders.rb30
1 files changed, 30 insertions, 0 deletions
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