From 870377915af301c98a54f7f588e077610b2190aa Mon Sep 17 00:00:00 2001 From: Xavier Noria <fxn@hashref.com> Date: Thu, 14 Feb 2019 15:12:57 -0800 Subject: 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. --- railties/lib/rails/autoloaders.rb | 30 ++++++++++++++++++++++++++++++ railties/lib/rails/engine.rb | 2 +- 2 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 railties/lib/rails/autoloaders.rb (limited to 'railties/lib/rails') 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! -- cgit v1.2.3