aboutsummaryrefslogtreecommitdiffstats
path: root/railties/lib/rails/autoloaders.rb
blob: b03499cf810c9333d2dae1b964ddee4a9bc5ace3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# frozen_string_literal: true

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