Skip to content

Commit db0c464

Browse files
authored
Merge pull request rails#44575 from Shopify/eager-auto-load
Modernize ActiveSupport::Autoload
2 parents e3353e5 + 9f7222b commit db0c464

File tree

1 file changed

+13
-11
lines changed
  • activesupport/lib/active_support/dependencies

1 file changed

+13
-11
lines changed

activesupport/lib/active_support/dependencies/autoload.rb

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,13 @@ module ActiveSupport
2626
# MyLib.eager_load!
2727
module Autoload
2828
def self.extended(base) # :nodoc:
29-
base.class_eval do
30-
@_autoloads = {}
31-
@_under_path = nil
32-
@_at_path = nil
33-
@_eager_autoload = false
29+
if RUBY_VERSION < "3"
30+
base.class_eval do
31+
@_autoloads = nil
32+
@_under_path = nil
33+
@_at_path = nil
34+
@_eager_autoload = false
35+
end
3436
end
3537
end
3638

@@ -41,7 +43,8 @@ def autoload(const_name, path = @_at_path)
4143
end
4244

4345
if @_eager_autoload
44-
@_autoloads[const_name] = path
46+
@_eagerloaded_constants ||= []
47+
@_eagerloaded_constants << const_name
4548
end
4649

4750
super const_name, path
@@ -69,11 +72,10 @@ def eager_autoload
6972
end
7073

7174
def eager_load!
72-
@_autoloads.each_value { |file| require file }
73-
end
74-
75-
def autoloads
76-
@_autoloads
75+
if @_eagerloaded_constants
76+
@_eagerloaded_constants.each { |const_name| const_get(const_name) }
77+
@_eagerloaded_constants = nil
78+
end
7779
end
7880
end
7981
end

0 commit comments

Comments
 (0)