activesupport 5.2.4.3 → 7.0.3

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of activesupport might be problematic. Click here for more details.

Files changed (228) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +244 -459
  3. data/MIT-LICENSE +1 -1
  4. data/README.rdoc +4 -3
  5. data/lib/active_support/actionable_error.rb +48 -0
  6. data/lib/active_support/array_inquirer.rb +2 -2
  7. data/lib/active_support/backtrace_cleaner.rb +31 -5
  8. data/lib/active_support/benchmarkable.rb +3 -3
  9. data/lib/active_support/cache/file_store.rb +47 -41
  10. data/lib/active_support/cache/mem_cache_store.rb +151 -40
  11. data/lib/active_support/cache/memory_store.rb +68 -34
  12. data/lib/active_support/cache/null_store.rb +16 -3
  13. data/lib/active_support/cache/redis_cache_store.rb +103 -101
  14. data/lib/active_support/cache/strategy/local_cache.rb +56 -64
  15. data/lib/active_support/cache.rb +333 -116
  16. data/lib/active_support/callbacks.rb +244 -128
  17. data/lib/active_support/code_generator.rb +65 -0
  18. data/lib/active_support/concern.rb +72 -5
  19. data/lib/active_support/concurrency/load_interlock_aware_monitor.rb +16 -0
  20. data/lib/active_support/concurrency/share_lock.rb +2 -3
  21. data/lib/active_support/configurable.rb +15 -16
  22. data/lib/active_support/configuration_file.rb +51 -0
  23. data/lib/active_support/core_ext/array/access.rb +15 -7
  24. data/lib/active_support/core_ext/array/conversions.rb +18 -17
  25. data/lib/active_support/core_ext/array/deprecated_conversions.rb +25 -0
  26. data/lib/active_support/core_ext/array/extract.rb +21 -0
  27. data/lib/active_support/core_ext/array/grouping.rb +6 -6
  28. data/lib/active_support/core_ext/array/inquiry.rb +2 -2
  29. data/lib/active_support/core_ext/array.rb +2 -1
  30. data/lib/active_support/core_ext/benchmark.rb +2 -2
  31. data/lib/active_support/core_ext/big_decimal/conversions.rb +1 -1
  32. data/lib/active_support/core_ext/class/attribute.rb +32 -47
  33. data/lib/active_support/core_ext/class/subclasses.rb +9 -22
  34. data/lib/active_support/core_ext/date/blank.rb +1 -1
  35. data/lib/active_support/core_ext/date/calculations.rb +15 -14
  36. data/lib/active_support/core_ext/date/conversions.rb +16 -15
  37. data/lib/active_support/core_ext/date/deprecated_conversions.rb +26 -0
  38. data/lib/active_support/core_ext/date.rb +1 -0
  39. data/lib/active_support/core_ext/date_and_time/calculations.rb +41 -51
  40. data/lib/active_support/core_ext/date_and_time/compatibility.rb +15 -0
  41. data/lib/active_support/core_ext/date_and_time/zones.rb +0 -1
  42. data/lib/active_support/core_ext/date_time/blank.rb +1 -1
  43. data/lib/active_support/core_ext/date_time/calculations.rb +1 -1
  44. data/lib/active_support/core_ext/date_time/conversions.rb +13 -14
  45. data/lib/active_support/core_ext/date_time/deprecated_conversions.rb +22 -0
  46. data/lib/active_support/core_ext/date_time.rb +1 -0
  47. data/lib/active_support/core_ext/digest/uuid.rb +39 -13
  48. data/lib/active_support/core_ext/enumerable.rb +241 -76
  49. data/lib/active_support/core_ext/file/atomic.rb +3 -1
  50. data/lib/active_support/core_ext/hash/conversions.rb +3 -4
  51. data/lib/active_support/core_ext/hash/deep_transform_values.rb +46 -0
  52. data/lib/active_support/core_ext/hash/except.rb +2 -2
  53. data/lib/active_support/core_ext/hash/indifferent_access.rb +3 -3
  54. data/lib/active_support/core_ext/hash/keys.rb +2 -31
  55. data/lib/active_support/core_ext/hash/slice.rb +6 -27
  56. data/lib/active_support/core_ext/hash.rb +1 -2
  57. data/lib/active_support/core_ext/integer/multiple.rb +1 -1
  58. data/lib/active_support/core_ext/kernel/reporting.rb +4 -4
  59. data/lib/active_support/core_ext/kernel/singleton_class.rb +1 -1
  60. data/lib/active_support/core_ext/kernel.rb +0 -1
  61. data/lib/active_support/core_ext/load_error.rb +1 -1
  62. data/lib/active_support/core_ext/module/attr_internal.rb +2 -2
  63. data/lib/active_support/core_ext/module/attribute_accessors.rb +32 -39
  64. data/lib/active_support/core_ext/module/attribute_accessors_per_thread.rb +35 -28
  65. data/lib/active_support/core_ext/module/concerning.rb +8 -2
  66. data/lib/active_support/core_ext/module/delegation.rb +70 -33
  67. data/lib/active_support/core_ext/module/introspection.rb +16 -15
  68. data/lib/active_support/core_ext/module/redefine_method.rb +8 -17
  69. data/lib/active_support/core_ext/module.rb +0 -1
  70. data/lib/active_support/core_ext/name_error.rb +23 -2
  71. data/lib/active_support/core_ext/numeric/conversions.rb +132 -129
  72. data/lib/active_support/core_ext/numeric/deprecated_conversions.rb +60 -0
  73. data/lib/active_support/core_ext/numeric.rb +1 -1
  74. data/lib/active_support/core_ext/object/acts_like.rb +29 -5
  75. data/lib/active_support/core_ext/object/blank.rb +3 -4
  76. data/lib/active_support/core_ext/object/deep_dup.rb +1 -1
  77. data/lib/active_support/core_ext/object/duplicable.rb +14 -110
  78. data/lib/active_support/core_ext/object/json.rb +44 -27
  79. data/lib/active_support/core_ext/object/to_query.rb +2 -2
  80. data/lib/active_support/core_ext/object/try.rb +24 -14
  81. data/lib/active_support/core_ext/object/with_options.rb +21 -2
  82. data/lib/active_support/core_ext/pathname/existence.rb +21 -0
  83. data/lib/active_support/core_ext/pathname.rb +3 -0
  84. data/lib/active_support/core_ext/range/compare_range.rb +23 -27
  85. data/lib/active_support/core_ext/range/conversions.rb +32 -30
  86. data/lib/active_support/core_ext/range/deprecated_conversions.rb +26 -0
  87. data/lib/active_support/core_ext/range/each.rb +1 -2
  88. data/lib/active_support/core_ext/range/include_time_with_zone.rb +4 -20
  89. data/lib/active_support/core_ext/range/overlaps.rb +1 -1
  90. data/lib/active_support/core_ext/range.rb +1 -1
  91. data/lib/active_support/core_ext/regexp.rb +8 -5
  92. data/lib/active_support/core_ext/securerandom.rb +23 -3
  93. data/lib/active_support/core_ext/string/access.rb +5 -16
  94. data/lib/active_support/core_ext/string/conversions.rb +3 -2
  95. data/lib/active_support/core_ext/string/filters.rb +42 -1
  96. data/lib/active_support/core_ext/string/inflections.rb +46 -7
  97. data/lib/active_support/core_ext/string/inquiry.rb +2 -1
  98. data/lib/active_support/core_ext/string/multibyte.rb +6 -5
  99. data/lib/active_support/core_ext/string/output_safety.rb +129 -20
  100. data/lib/active_support/core_ext/string/starts_ends_with.rb +2 -2
  101. data/lib/active_support/core_ext/string/strip.rb +3 -1
  102. data/lib/active_support/core_ext/symbol/starts_ends_with.rb +6 -0
  103. data/lib/active_support/core_ext/symbol.rb +3 -0
  104. data/lib/active_support/core_ext/time/calculations.rb +59 -10
  105. data/lib/active_support/core_ext/time/conversions.rb +15 -12
  106. data/lib/active_support/core_ext/time/deprecated_conversions.rb +22 -0
  107. data/lib/active_support/core_ext/time/zones.rb +7 -22
  108. data/lib/active_support/core_ext/time.rb +1 -0
  109. data/lib/active_support/core_ext/uri.rb +3 -22
  110. data/lib/active_support/core_ext.rb +2 -1
  111. data/lib/active_support/current_attributes/test_helper.rb +13 -0
  112. data/lib/active_support/current_attributes.rb +47 -16
  113. data/lib/active_support/dependencies/interlock.rb +10 -18
  114. data/lib/active_support/dependencies/require_dependency.rb +28 -0
  115. data/lib/active_support/dependencies.rb +60 -715
  116. data/lib/active_support/deprecation/behaviors.rb +21 -5
  117. data/lib/active_support/deprecation/disallowed.rb +56 -0
  118. data/lib/active_support/deprecation/instance_delegator.rb +0 -1
  119. data/lib/active_support/deprecation/method_wrappers.rb +18 -23
  120. data/lib/active_support/deprecation/proxy_wrappers.rb +31 -8
  121. data/lib/active_support/deprecation/reporting.rb +50 -7
  122. data/lib/active_support/deprecation.rb +7 -2
  123. data/lib/active_support/descendants_tracker.rb +190 -34
  124. data/lib/active_support/digest.rb +5 -3
  125. data/lib/active_support/duration/iso8601_parser.rb +5 -7
  126. data/lib/active_support/duration/iso8601_serializer.rb +27 -15
  127. data/lib/active_support/duration.rb +149 -67
  128. data/lib/active_support/encrypted_configuration.rb +12 -5
  129. data/lib/active_support/encrypted_file.rb +23 -5
  130. data/lib/active_support/environment_inquirer.rb +20 -0
  131. data/lib/active_support/error_reporter.rb +117 -0
  132. data/lib/active_support/evented_file_update_checker.rb +85 -122
  133. data/lib/active_support/execution_context/test_helper.rb +13 -0
  134. data/lib/active_support/execution_context.rb +53 -0
  135. data/lib/active_support/execution_wrapper.rb +44 -21
  136. data/lib/active_support/executor/test_helper.rb +7 -0
  137. data/lib/active_support/file_update_checker.rb +0 -1
  138. data/lib/active_support/fork_tracker.rb +71 -0
  139. data/lib/active_support/gem_version.rb +5 -5
  140. data/lib/active_support/hash_with_indifferent_access.rb +73 -43
  141. data/lib/active_support/html_safe_translation.rb +43 -0
  142. data/lib/active_support/i18n.rb +2 -0
  143. data/lib/active_support/i18n_railtie.rb +15 -8
  144. data/lib/active_support/inflector/inflections.rb +25 -14
  145. data/lib/active_support/inflector/methods.rb +38 -71
  146. data/lib/active_support/inflector/transliterate.rb +47 -18
  147. data/lib/active_support/isolated_execution_state.rb +72 -0
  148. data/lib/active_support/json/decoding.rb +25 -26
  149. data/lib/active_support/json/encoding.rb +14 -6
  150. data/lib/active_support/key_generator.rb +23 -38
  151. data/lib/active_support/lazy_load_hooks.rb +19 -5
  152. data/lib/active_support/locale/en.rb +33 -0
  153. data/lib/active_support/locale/en.yml +8 -4
  154. data/lib/active_support/log_subscriber/test_helper.rb +2 -2
  155. data/lib/active_support/log_subscriber.rb +51 -11
  156. data/lib/active_support/logger.rb +6 -22
  157. data/lib/active_support/logger_silence.rb +11 -19
  158. data/lib/active_support/logger_thread_safe_level.rb +45 -10
  159. data/lib/active_support/message_encryptor.rb +20 -19
  160. data/lib/active_support/message_verifier.rb +53 -21
  161. data/lib/active_support/messages/metadata.rb +13 -4
  162. data/lib/active_support/messages/rotation_configuration.rb +2 -1
  163. data/lib/active_support/messages/rotator.rb +10 -9
  164. data/lib/active_support/multibyte/chars.rb +17 -76
  165. data/lib/active_support/multibyte/unicode.rb +7 -331
  166. data/lib/active_support/multibyte.rb +1 -1
  167. data/lib/active_support/notifications/fanout.rb +163 -37
  168. data/lib/active_support/notifications/instrumenter.rb +90 -11
  169. data/lib/active_support/notifications.rb +88 -30
  170. data/lib/active_support/number_helper/number_converter.rb +6 -9
  171. data/lib/active_support/number_helper/number_to_currency_converter.rb +12 -12
  172. data/lib/active_support/number_helper/number_to_delimited_converter.rb +4 -3
  173. data/lib/active_support/number_helper/number_to_human_converter.rb +4 -3
  174. data/lib/active_support/number_helper/number_to_human_size_converter.rb +5 -4
  175. data/lib/active_support/number_helper/number_to_percentage_converter.rb +3 -1
  176. data/lib/active_support/number_helper/number_to_phone_converter.rb +3 -2
  177. data/lib/active_support/number_helper/number_to_rounded_converter.rb +12 -7
  178. data/lib/active_support/number_helper/rounding_helper.rb +12 -32
  179. data/lib/active_support/number_helper.rb +36 -12
  180. data/lib/active_support/option_merger.rb +15 -4
  181. data/lib/active_support/ordered_hash.rb +2 -2
  182. data/lib/active_support/ordered_options.rb +14 -4
  183. data/lib/active_support/parameter_filter.rb +138 -0
  184. data/lib/active_support/per_thread_registry.rb +6 -1
  185. data/lib/active_support/rails.rb +1 -10
  186. data/lib/active_support/railtie.rb +77 -5
  187. data/lib/active_support/reloader.rb +5 -6
  188. data/lib/active_support/rescuable.rb +8 -8
  189. data/lib/active_support/ruby_features.rb +7 -0
  190. data/lib/active_support/secure_compare_rotator.rb +51 -0
  191. data/lib/active_support/security_utils.rb +19 -12
  192. data/lib/active_support/string_inquirer.rb +2 -3
  193. data/lib/active_support/subscriber.rb +79 -46
  194. data/lib/active_support/tagged_logging.rb +58 -9
  195. data/lib/active_support/test_case.rb +79 -0
  196. data/lib/active_support/testing/assertions.rb +62 -11
  197. data/lib/active_support/testing/deprecation.rb +52 -2
  198. data/lib/active_support/testing/file_fixtures.rb +2 -0
  199. data/lib/active_support/testing/isolation.rb +4 -4
  200. data/lib/active_support/testing/method_call_assertions.rb +32 -5
  201. data/lib/active_support/testing/parallelization/server.rb +82 -0
  202. data/lib/active_support/testing/parallelization/worker.rb +103 -0
  203. data/lib/active_support/testing/parallelization.rb +55 -0
  204. data/lib/active_support/testing/parallelize_executor.rb +76 -0
  205. data/lib/active_support/testing/stream.rb +4 -7
  206. data/lib/active_support/testing/tagged_logging.rb +1 -1
  207. data/lib/active_support/testing/time_helpers.rb +60 -14
  208. data/lib/active_support/time_with_zone.rb +139 -64
  209. data/lib/active_support/values/time_zone.rb +66 -30
  210. data/lib/active_support/version.rb +1 -1
  211. data/lib/active_support/xml_mini/jdom.rb +3 -4
  212. data/lib/active_support/xml_mini/libxml.rb +7 -7
  213. data/lib/active_support/xml_mini/libxmlsax.rb +5 -5
  214. data/lib/active_support/xml_mini/nokogiri.rb +6 -6
  215. data/lib/active_support/xml_mini/nokogirisax.rb +4 -4
  216. data/lib/active_support/xml_mini/rexml.rb +11 -4
  217. data/lib/active_support/xml_mini.rb +7 -14
  218. data/lib/active_support.rb +30 -1
  219. metadata +64 -35
  220. data/lib/active_support/core_ext/array/prepend_and_append.rb +0 -9
  221. data/lib/active_support/core_ext/hash/compact.rb +0 -29
  222. data/lib/active_support/core_ext/hash/transform_values.rb +0 -32
  223. data/lib/active_support/core_ext/kernel/agnostics.rb +0 -13
  224. data/lib/active_support/core_ext/marshal.rb +0 -24
  225. data/lib/active_support/core_ext/module/reachable.rb +0 -11
  226. data/lib/active_support/core_ext/numeric/inquiry.rb +0 -28
  227. data/lib/active_support/core_ext/range/include_range.rb +0 -3
  228. data/lib/active_support/values/unicode_tables.dat +0 -0
@@ -1,753 +1,98 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "set"
4
- require "thread"
5
- require "concurrent/map"
6
- require "pathname"
7
- require "active_support/core_ext/module/aliasing"
8
- require "active_support/core_ext/module/attribute_accessors"
9
- require "active_support/core_ext/module/introspection"
10
- require "active_support/core_ext/module/anonymous"
11
- require "active_support/core_ext/object/blank"
12
- require "active_support/core_ext/kernel/reporting"
13
- require "active_support/core_ext/load_error"
14
- require "active_support/core_ext/name_error"
15
- require "active_support/core_ext/string/starts_ends_with"
16
4
  require "active_support/dependencies/interlock"
17
- require "active_support/inflector"
18
5
 
19
- module ActiveSupport #:nodoc:
20
- module Dependencies #:nodoc:
21
- extend self
6
+ module ActiveSupport # :nodoc:
7
+ module Dependencies # :nodoc:
8
+ require_relative "dependencies/require_dependency"
22
9
 
23
- mattr_accessor :interlock, default: Interlock.new
10
+ singleton_class.attr_accessor :interlock
11
+ @interlock = Interlock.new
24
12
 
25
13
  # :doc:
26
14
 
27
15
  # Execute the supplied block without interference from any
28
16
  # concurrent loads.
29
- def self.run_interlock
30
- Dependencies.interlock.running { yield }
17
+ def self.run_interlock(&block)
18
+ interlock.running(&block)
31
19
  end
32
20
 
33
21
  # Execute the supplied block while holding an exclusive lock,
34
22
  # preventing any other thread from being inside a #run_interlock
35
23
  # block at the same time.
36
- def self.load_interlock
37
- Dependencies.interlock.loading { yield }
24
+ def self.load_interlock(&block)
25
+ interlock.loading(&block)
38
26
  end
39
27
 
40
28
  # Execute the supplied block while holding an exclusive lock,
41
29
  # preventing any other thread from being inside a #run_interlock
42
30
  # block at the same time.
43
- def self.unload_interlock
44
- Dependencies.interlock.unloading { yield }
31
+ def self.unload_interlock(&block)
32
+ interlock.unloading(&block)
45
33
  end
46
34
 
47
35
  # :nodoc:
48
36
 
49
- # Should we turn on Ruby warnings on the first load of dependent files?
50
- mattr_accessor :warnings_on_first_load, default: false
51
-
52
- # All files ever loaded.
53
- mattr_accessor :history, default: Set.new
54
-
55
- # All files currently loaded.
56
- mattr_accessor :loaded, default: Set.new
57
-
58
- # Stack of files being loaded.
59
- mattr_accessor :loading, default: []
60
-
61
- # Should we load files or require them?
62
- mattr_accessor :mechanism, default: ENV["NO_RELOAD"] ? :require : :load
63
-
64
- # The set of directories from which we may automatically load files. Files
65
- # under these directories will be reloaded on each request in development mode,
66
- # unless the directory also appears in autoload_once_paths.
67
- mattr_accessor :autoload_paths, default: []
68
-
69
- # The set of directories from which automatically loaded constants are loaded
70
- # only once. All directories in this set must also be present in +autoload_paths+.
71
- mattr_accessor :autoload_once_paths, default: []
72
-
73
- # An array of qualified constant names that have been loaded. Adding a name
74
- # to this array will cause it to be unloaded the next time Dependencies are
75
- # cleared.
76
- mattr_accessor :autoloaded_constants, default: []
77
-
78
- # An array of constant names that need to be unloaded on every request. Used
79
- # to allow arbitrary constants to be marked for unloading.
80
- mattr_accessor :explicitly_unloadable_constants, default: []
81
-
82
- # The WatchStack keeps a stack of the modules being watched as files are
83
- # loaded. If a file in the process of being loaded (parent.rb) triggers the
84
- # load of another file (child.rb) the stack will ensure that child.rb
85
- # handles the new constants.
86
- #
87
- # If child.rb is being autoloaded, its constants will be added to
88
- # autoloaded_constants. If it was being required, they will be discarded.
89
- #
90
- # This is handled by walking back up the watch stack and adding the constants
91
- # found by child.rb to the list of original constants in parent.rb.
92
- class WatchStack
93
- include Enumerable
94
-
95
- # @watching is a stack of lists of constants being watched. For instance,
96
- # if parent.rb is autoloaded, the stack will look like [[Object]]. If
97
- # parent.rb then requires namespace/child.rb, the stack will look like
98
- # [[Object], [Namespace]].
99
-
100
- attr_reader :watching
101
-
102
- def initialize
103
- @watching = []
104
- @stack = Hash.new { |h, k| h[k] = [] }
105
- end
106
-
107
- def each(&block)
108
- @stack.each(&block)
109
- end
110
-
111
- def watching?
112
- !@watching.empty?
113
- end
114
-
115
- # Returns a list of new constants found since the last call to
116
- # <tt>watch_namespaces</tt>.
117
- def new_constants
118
- constants = []
119
-
120
- # Grab the list of namespaces that we're looking for new constants under
121
- @watching.last.each do |namespace|
122
- # Retrieve the constants that were present under the namespace when watch_namespaces
123
- # was originally called
124
- original_constants = @stack[namespace].last
125
-
126
- mod = Inflector.constantize(namespace) if Dependencies.qualified_const_defined?(namespace)
127
- next unless mod.is_a?(Module)
128
-
129
- # Get a list of the constants that were added
130
- new_constants = mod.constants(false) - original_constants
131
-
132
- # @stack[namespace] returns an Array of the constants that are being evaluated
133
- # for that namespace. For instance, if parent.rb requires child.rb, the first
134
- # element of @stack[Object] will be an Array of the constants that were present
135
- # before parent.rb was required. The second element will be an Array of the
136
- # constants that were present before child.rb was required.
137
- @stack[namespace].each do |namespace_constants|
138
- namespace_constants.concat(new_constants)
139
- end
140
-
141
- # Normalize the list of new constants, and add them to the list we will return
142
- new_constants.each do |suffix|
143
- constants << ([namespace, suffix] - ["Object"]).join("::".freeze)
144
- end
145
- end
146
- constants
147
- ensure
148
- # A call to new_constants is always called after a call to watch_namespaces
149
- pop_modules(@watching.pop)
150
- end
151
-
152
- # Add a set of modules to the watch stack, remembering the initial
153
- # constants.
154
- def watch_namespaces(namespaces)
155
- @watching << namespaces.map do |namespace|
156
- module_name = Dependencies.to_constant_name(namespace)
157
- original_constants = Dependencies.qualified_const_defined?(module_name) ?
158
- Inflector.constantize(module_name).constants(false) : []
159
-
160
- @stack[module_name] << original_constants
161
- module_name
162
- end
163
- end
164
-
165
- private
166
- def pop_modules(modules)
167
- modules.each { |mod| @stack[mod].pop }
168
- end
169
- end
170
-
171
- # An internal stack used to record which constants are loaded by any block.
172
- mattr_accessor :constant_watch_stack, default: WatchStack.new
173
-
174
- # Module includes this module.
175
- module ModuleConstMissing #:nodoc:
176
- def self.append_features(base)
177
- base.class_eval do
178
- # Emulate #exclude via an ivar
179
- return if defined?(@_const_missing) && @_const_missing
180
- @_const_missing = instance_method(:const_missing)
181
- remove_method(:const_missing)
182
- end
183
- super
184
- end
185
-
186
- def self.exclude_from(base)
187
- base.class_eval do
188
- define_method :const_missing, @_const_missing
189
- @_const_missing = nil
190
- end
191
- end
192
-
193
- def const_missing(const_name)
194
- from_mod = anonymous? ? guess_for_anonymous(const_name) : self
195
- Dependencies.load_missing_constant(from_mod, const_name)
196
- end
197
-
198
- # We assume that the name of the module reflects the nesting
199
- # (unless it can be proven that is not the case) and the path to the file
200
- # that defines the constant. Anonymous modules cannot follow these
201
- # conventions and therefore we assume that the user wants to refer to a
202
- # top-level constant.
203
- def guess_for_anonymous(const_name)
204
- if Object.const_defined?(const_name)
205
- raise NameError.new "#{const_name} cannot be autoloaded from an anonymous class or module", const_name
206
- else
207
- Object
208
- end
209
- end
210
-
211
- def unloadable(const_desc = self)
212
- super(const_desc)
213
- end
214
- end
215
-
216
- # Object includes this module.
217
- module Loadable #:nodoc:
218
- def self.exclude_from(base)
219
- base.class_eval do
220
- define_method(:load, Kernel.instance_method(:load))
221
- private :load
222
- end
223
- end
224
-
225
- def require_or_load(file_name)
226
- Dependencies.require_or_load(file_name)
227
- end
228
-
229
- # :doc:
230
-
231
- # Interprets a file using <tt>mechanism</tt> and marks its defined
232
- # constants as autoloaded. <tt>file_name</tt> can be either a string or
233
- # respond to <tt>to_path</tt>.
234
- #
235
- # Use this method in code that absolutely needs a certain constant to be
236
- # defined at that point. A typical use case is to make constant name
237
- # resolution deterministic for constants with the same relative name in
238
- # different namespaces whose evaluation would depend on load order
239
- # otherwise.
240
- def require_dependency(file_name, message = "No such file to load -- %s.rb")
241
- file_name = file_name.to_path if file_name.respond_to?(:to_path)
242
- unless file_name.is_a?(String)
243
- raise ArgumentError, "the file name must either be a String or implement #to_path -- you passed #{file_name.inspect}"
244
- end
245
-
246
- Dependencies.depend_on(file_name, message)
247
- end
248
-
249
- # :nodoc:
250
-
251
- def load_dependency(file)
252
- if Dependencies.load? && Dependencies.constant_watch_stack.watching?
253
- descs = Dependencies.constant_watch_stack.watching.flatten.uniq
254
-
255
- Dependencies.new_constants_in(*descs) { yield }
256
- else
257
- yield
258
- end
259
- rescue Exception => exception # errors from loading file
260
- exception.blame_file! file if exception.respond_to? :blame_file!
261
- raise
262
- end
263
-
264
- # Mark the given constant as unloadable. Unloadable constants are removed
265
- # each time dependencies are cleared.
266
- #
267
- # Note that marking a constant for unloading need only be done once. Setup
268
- # or init scripts may list each unloadable constant that may need unloading;
269
- # each constant will be removed for every subsequent clear, as opposed to
270
- # for the first clear.
271
- #
272
- # The provided constant descriptor may be a (non-anonymous) module or class,
273
- # or a qualified constant name as a string or symbol.
274
- #
275
- # Returns +true+ if the constant was not previously marked for unloading,
276
- # +false+ otherwise.
277
- def unloadable(const_desc)
278
- Dependencies.mark_for_unload const_desc
279
- end
280
-
281
- private
282
-
283
- def load(file, wrap = false)
284
- result = false
285
- load_dependency(file) { result = super }
286
- result
287
- end
288
-
289
- def require(file)
290
- result = false
291
- load_dependency(file) { result = super }
292
- result
293
- end
294
- end
295
-
296
- # Exception file-blaming.
297
- module Blamable #:nodoc:
298
- def blame_file!(file)
299
- (@blamed_files ||= []).unshift file
300
- end
301
-
302
- def blamed_files
303
- @blamed_files ||= []
304
- end
305
-
306
- def describe_blame
307
- return nil if blamed_files.empty?
308
- "This error occurred while loading the following files:\n #{blamed_files.join "\n "}"
309
- end
310
-
311
- def copy_blame!(exc)
312
- @blamed_files = exc.blamed_files.clone
313
- self
314
- end
315
- end
316
-
317
- def hook!
318
- Object.class_eval { include Loadable }
319
- Module.class_eval { include ModuleConstMissing }
320
- Exception.class_eval { include Blamable }
321
- end
322
-
323
- def unhook!
324
- ModuleConstMissing.exclude_from(Module)
325
- Loadable.exclude_from(Object)
326
- end
327
-
328
- def load?
329
- mechanism == :load
330
- end
331
-
332
- def depend_on(file_name, message = "No such file to load -- %s.rb")
333
- path = search_for_file(file_name)
334
- require_or_load(path || file_name)
335
- rescue LoadError => load_error
336
- if file_name = load_error.message[/ -- (.*?)(\.rb)?$/, 1]
337
- load_error.message.replace(message % file_name)
338
- load_error.copy_blame!(load_error)
339
- end
340
- raise
341
- end
342
-
343
- def clear
344
- Dependencies.unload_interlock do
345
- loaded.clear
346
- loading.clear
347
- remove_unloadable_constants!
348
- end
349
- end
350
-
351
- def require_or_load(file_name, const_path = nil)
352
- file_name = $` if file_name =~ /\.rb\z/
353
- expanded = File.expand_path(file_name)
354
- return if loaded.include?(expanded)
355
-
356
- Dependencies.load_interlock do
357
- # Maybe it got loaded while we were waiting for our lock:
358
- return if loaded.include?(expanded)
359
-
360
- # Record that we've seen this file *before* loading it to avoid an
361
- # infinite loop with mutual dependencies.
362
- loaded << expanded
363
- loading << expanded
364
-
365
- begin
366
- if load?
367
- # Enable warnings if this file has not been loaded before and
368
- # warnings_on_first_load is set.
369
- load_args = ["#{file_name}.rb"]
370
- load_args << const_path unless const_path.nil?
371
-
372
- if !warnings_on_first_load || history.include?(expanded)
373
- result = load_file(*load_args)
374
- else
375
- enable_warnings { result = load_file(*load_args) }
376
- end
377
- else
378
- result = require file_name
379
- end
380
- rescue Exception
381
- loaded.delete expanded
382
- raise
383
- ensure
384
- loading.pop
385
- end
386
-
387
- # Record history *after* loading so first load gets warnings.
388
- history << expanded
389
- result
390
- end
391
- end
392
-
393
- # Is the provided constant path defined?
394
- def qualified_const_defined?(path)
395
- Object.const_defined?(path, false)
396
- end
397
-
398
- # Given +path+, a filesystem path to a ruby file, return an array of
399
- # constant paths which would cause Dependencies to attempt to load this
400
- # file.
401
- def loadable_constants_for_path(path, bases = autoload_paths)
402
- path = $` if path =~ /\.rb\z/
403
- expanded_path = File.expand_path(path)
404
- paths = []
405
-
406
- bases.each do |root|
407
- expanded_root = File.expand_path(root)
408
- next unless expanded_path.start_with?(expanded_root)
409
-
410
- root_size = expanded_root.size
411
- next if expanded_path[root_size] != ?/.freeze
412
-
413
- nesting = expanded_path[(root_size + 1)..-1]
414
- paths << nesting.camelize unless nesting.blank?
415
- end
416
-
417
- paths.uniq!
418
- paths
419
- end
420
-
421
- # Search for a file in autoload_paths matching the provided suffix.
422
- def search_for_file(path_suffix)
423
- path_suffix = path_suffix.sub(/(\.rb)?$/, ".rb".freeze)
424
-
425
- autoload_paths.each do |root|
426
- path = File.join(root, path_suffix)
427
- return path if File.file? path
428
- end
429
- nil # Gee, I sure wish we had first_match ;-)
430
- end
431
-
432
- # Does the provided path_suffix correspond to an autoloadable module?
433
- # Instead of returning a boolean, the autoload base for this module is
434
- # returned.
435
- def autoloadable_module?(path_suffix)
436
- autoload_paths.each do |load_path|
437
- return load_path if File.directory? File.join(load_path, path_suffix)
438
- end
439
- nil
440
- end
441
-
442
- def load_once_path?(path)
443
- # to_s works around a ruby issue where String#starts_with?(Pathname)
444
- # will raise a TypeError: no implicit conversion of Pathname into String
445
- autoload_once_paths.any? { |base| path.starts_with? base.to_s }
446
- end
447
-
448
- # Attempt to autoload the provided module name by searching for a directory
449
- # matching the expected path suffix. If found, the module is created and
450
- # assigned to +into+'s constants with the name +const_name+. Provided that
451
- # the directory was loaded from a reloadable base path, it is added to the
452
- # set of constants that are to be unloaded.
453
- def autoload_module!(into, const_name, qualified_name, path_suffix)
454
- return nil unless base_path = autoloadable_module?(path_suffix)
455
- mod = Module.new
456
- into.const_set const_name, mod
457
- autoloaded_constants << qualified_name unless autoload_once_paths.include?(base_path)
458
- autoloaded_constants.uniq!
459
- mod
460
- end
461
-
462
- # Load the file at the provided path. +const_paths+ is a set of qualified
463
- # constant names. When loading the file, Dependencies will watch for the
464
- # addition of these constants. Each that is defined will be marked as
465
- # autoloaded, and will be removed when Dependencies.clear is next called.
37
+ # The array of directories from which we autoload and reload, if reloading
38
+ # is enabled. The public interface to push directories to this collection
39
+ # from applications or engines is config.autoload_paths.
466
40
  #
467
- # If the second parameter is left off, then Dependencies will construct a
468
- # set of names that the file at +path+ may define. See
469
- # +loadable_constants_for_path+ for more details.
470
- def load_file(path, const_paths = loadable_constants_for_path(path))
471
- const_paths = [const_paths].compact unless const_paths.is_a? Array
472
- parent_paths = const_paths.collect { |const_path| const_path[/.*(?=::)/] || ::Object }
473
-
474
- result = nil
475
- newly_defined_paths = new_constants_in(*parent_paths) do
476
- result = Kernel.load path
477
- end
478
-
479
- autoloaded_constants.concat newly_defined_paths unless load_once_path?(path)
480
- autoloaded_constants.uniq!
481
- result
482
- end
483
-
484
- # Returns the constant path for the provided parent and constant name.
485
- def qualified_name_for(mod, name)
486
- mod_name = to_constant_name mod
487
- mod_name == "Object" ? name.to_s : "#{mod_name}::#{name}"
488
- end
489
-
490
- # Load the constant named +const_name+ which is missing from +from_mod+. If
491
- # it is not possible to load the constant into from_mod, try its parent
492
- # module using +const_missing+.
493
- def load_missing_constant(from_mod, const_name)
494
- unless qualified_const_defined?(from_mod.name) && Inflector.constantize(from_mod.name).equal?(from_mod)
495
- raise ArgumentError, "A copy of #{from_mod} has been removed from the module tree but is still active!"
496
- end
497
-
498
- qualified_name = qualified_name_for from_mod, const_name
499
- path_suffix = qualified_name.underscore
500
-
501
- file_path = search_for_file(path_suffix)
502
-
503
- if file_path
504
- expanded = File.expand_path(file_path)
505
- expanded.sub!(/\.rb\z/, "".freeze)
506
-
507
- if loading.include?(expanded)
508
- raise "Circular dependency detected while autoloading constant #{qualified_name}"
509
- else
510
- require_or_load(expanded, qualified_name)
511
- raise LoadError, "Unable to autoload constant #{qualified_name}, expected #{file_path} to define it" unless from_mod.const_defined?(const_name, false)
512
- return from_mod.const_get(const_name)
513
- end
514
- elsif mod = autoload_module!(from_mod, const_name, qualified_name, path_suffix)
515
- return mod
516
- elsif (parent = from_mod.parent) && parent != from_mod &&
517
- ! from_mod.parents.any? { |p| p.const_defined?(const_name, false) }
518
- # If our parents do not have a constant named +const_name+ then we are free
519
- # to attempt to load upwards. If they do have such a constant, then this
520
- # const_missing must be due to from_mod::const_name, which should not
521
- # return constants from from_mod's parents.
522
- begin
523
- # Since Ruby does not pass the nesting at the point the unknown
524
- # constant triggered the callback we cannot fully emulate constant
525
- # name lookup and need to make a trade-off: we are going to assume
526
- # that the nesting in the body of Foo::Bar is [Foo::Bar, Foo] even
527
- # though it might not be. Counterexamples are
528
- #
529
- # class Foo::Bar
530
- # Module.nesting # => [Foo::Bar]
531
- # end
532
- #
533
- # or
534
- #
535
- # module M::N
536
- # module S::T
537
- # Module.nesting # => [S::T, M::N]
538
- # end
539
- # end
540
- #
541
- # for example.
542
- return parent.const_missing(const_name)
543
- rescue NameError => e
544
- raise unless e.missing_name? qualified_name_for(parent, const_name)
545
- end
546
- end
547
-
548
- name_error = NameError.new("uninitialized constant #{qualified_name}", const_name)
549
- name_error.set_backtrace(caller.reject { |l| l.starts_with? __FILE__ })
550
- raise name_error
551
- end
552
-
553
- # Remove the constants that have been autoloaded, and those that have been
554
- # marked for unloading. Before each constant is removed a callback is sent
555
- # to its class/module if it implements +before_remove_const+.
41
+ # This collection is allowed to have intersection with autoload_once_paths.
42
+ # Common directories are not reloaded.
43
+ singleton_class.attr_accessor :autoload_paths
44
+ self.autoload_paths = []
45
+
46
+ # The array of directories from which we autoload and never reload, even if
47
+ # reloading is enabled. The public interface to push directories to this
48
+ # collection from applications or engines is config.autoload_once_paths.
49
+ singleton_class.attr_accessor :autoload_once_paths
50
+ self.autoload_once_paths = []
51
+
52
+ # This is a private set that collects all eager load paths during bootstrap.
53
+ # Useful for Zeitwerk integration. The public interface to push custom
54
+ # directories to this collection from applications or engines is
55
+ # config.eager_load_paths.
56
+ singleton_class.attr_accessor :_eager_load_paths
57
+ self._eager_load_paths = Set.new
58
+
59
+ # If reloading is enabled, this private set holds autoloaded classes tracked
60
+ # by the descendants tracker. It is populated by an on_load callback in the
61
+ # main autoloader. Used to clear state.
62
+ singleton_class.attr_accessor :_autoloaded_tracked_classes
63
+ self._autoloaded_tracked_classes = Set.new
64
+
65
+ # If reloading is enabled, this private attribute stores the main autoloader
66
+ # of a Rails application. It is `nil` otherwise.
556
67
  #
557
- # The callback implementation should be restricted to cleaning up caches, etc.
558
- # as the environment will be in an inconsistent state, e.g. other constants
559
- # may have already been unloaded and not accessible.
560
- def remove_unloadable_constants!
561
- autoloaded_constants.each { |const| remove_constant const }
562
- autoloaded_constants.clear
563
- Reference.clear!
564
- explicitly_unloadable_constants.each { |const| remove_constant const }
565
- end
68
+ # The public interface for this autoloader is `Rails.autoloaders.main`.
69
+ singleton_class.attr_accessor :autoloader
566
70
 
567
- class ClassCache
568
- def initialize
569
- @store = Concurrent::Map.new
570
- end
571
-
572
- def empty?
573
- @store.empty?
574
- end
575
-
576
- def key?(key)
577
- @store.key?(key)
578
- end
579
-
580
- def get(key)
581
- key = key.name if key.respond_to?(:name)
582
- @store[key] ||= Inflector.constantize(key)
583
- end
584
- alias :[] :get
585
-
586
- def safe_get(key)
587
- key = key.name if key.respond_to?(:name)
588
- @store[key] ||= Inflector.safe_constantize(key)
589
- end
590
-
591
- def store(klass)
592
- return self unless klass.respond_to?(:name)
593
- raise(ArgumentError, "anonymous classes cannot be cached") if klass.name.empty?
594
- @store[klass.name] = klass
595
- self
596
- end
597
-
598
- def clear!
599
- @store.clear
600
- end
601
- end
602
-
603
- Reference = ClassCache.new
604
-
605
- # Store a reference to a class +klass+.
606
- def reference(klass)
607
- Reference.store klass
608
- end
609
-
610
- # Get the reference for class named +name+.
611
- # Raises an exception if referenced class does not exist.
612
- def constantize(name)
613
- Reference.get(name)
614
- end
615
-
616
- # Get the reference for class named +name+ if one exists.
617
- # Otherwise returns +nil+.
618
- def safe_constantize(name)
619
- Reference.safe_get(name)
620
- end
621
-
622
- # Determine if the given constant has been automatically loaded.
623
- def autoloaded?(desc)
624
- return false if desc.is_a?(Module) && desc.anonymous?
625
- name = to_constant_name desc
626
- return false unless qualified_const_defined?(name)
627
- autoloaded_constants.include?(name)
628
- end
629
-
630
- # Will the provided constant descriptor be unloaded?
631
- def will_unload?(const_desc)
632
- autoloaded?(const_desc) ||
633
- explicitly_unloadable_constants.include?(to_constant_name(const_desc))
634
- end
635
-
636
- # Mark the provided constant name for unloading. This constant will be
637
- # unloaded on each request, not just the next one.
638
- def mark_for_unload(const_desc)
639
- name = to_constant_name const_desc
640
- if explicitly_unloadable_constants.include? name
641
- false
642
- else
643
- explicitly_unloadable_constants << name
644
- true
645
- end
646
- end
647
-
648
- # Run the provided block and detect the new constants that were loaded during
649
- # its execution. Constants may only be regarded as 'new' once -- so if the
650
- # block calls +new_constants_in+ again, then the constants defined within the
651
- # inner call will not be reported in this one.
71
+ # Private method that reloads constants autoloaded by the main autoloader.
652
72
  #
653
- # If the provided block does not run to completion, and instead raises an
654
- # exception, any new constants are regarded as being only partially defined
655
- # and will be removed immediately.
656
- def new_constants_in(*descs)
657
- constant_watch_stack.watch_namespaces(descs)
658
- success = false
659
-
660
- begin
661
- yield # Now yield to the code that is to define new constants.
662
- success = true
663
- ensure
664
- new_constants = constant_watch_stack.new_constants
665
-
666
- return new_constants if success
667
-
668
- # Remove partially loaded constants.
669
- new_constants.each { |c| remove_constant(c) }
73
+ # Rails.application.reloader.reload! is the public interface for application
74
+ # reload. That involves more things, like deleting unloaded classes from the
75
+ # internal state of the descendants tracker, or reloading routes.
76
+ def self.clear
77
+ unload_interlock do
78
+ _autoloaded_tracked_classes.clear
79
+ autoloader.reload
670
80
  end
671
81
  end
672
82
 
673
- # Convert the provided const desc to a qualified constant name (as a string).
674
- # A module, class, symbol, or string may be provided.
675
- def to_constant_name(desc) #:nodoc:
676
- case desc
677
- when String then desc.sub(/^::/, "")
678
- when Symbol then desc.to_s
679
- when Module
680
- desc.name ||
681
- raise(ArgumentError, "Anonymous modules have no name to be referenced by")
682
- else raise TypeError, "Not a valid constant descriptor: #{desc.inspect}"
83
+ # Private method used by require_dependency.
84
+ def self.search_for_file(relpath)
85
+ relpath += ".rb" unless relpath.end_with?(".rb")
86
+ autoload_paths.each do |autoload_path|
87
+ abspath = File.join(autoload_path, relpath)
88
+ return abspath if File.file?(abspath)
683
89
  end
90
+ nil
684
91
  end
685
92
 
686
- def remove_constant(const) #:nodoc:
687
- # Normalize ::Foo, ::Object::Foo, Object::Foo, Object::Object::Foo, etc. as Foo.
688
- normalized = const.to_s.sub(/\A::/, "")
689
- normalized.sub!(/\A(Object::)+/, "")
690
-
691
- constants = normalized.split("::")
692
- to_remove = constants.pop
693
-
694
- # Remove the file path from the loaded list.
695
- file_path = search_for_file(const.underscore)
696
- if file_path
697
- expanded = File.expand_path(file_path)
698
- expanded.sub!(/\.rb\z/, "")
699
- loaded.delete(expanded)
700
- end
701
-
702
- if constants.empty?
703
- parent = Object
704
- else
705
- # This method is robust to non-reachable constants.
706
- #
707
- # Non-reachable constants may be passed if some of the parents were
708
- # autoloaded and already removed. It is easier to do a sanity check
709
- # here than require the caller to be clever. We check the parent
710
- # rather than the very const argument because we do not want to
711
- # trigger Kernel#autoloads, see the comment below.
712
- parent_name = constants.join("::")
713
- return unless qualified_const_defined?(parent_name)
714
- parent = constantize(parent_name)
715
- end
716
-
717
- # In an autoloaded user.rb like this
718
- #
719
- # autoload :Foo, 'foo'
720
- #
721
- # class User < ActiveRecord::Base
722
- # end
723
- #
724
- # we correctly register "Foo" as being autoloaded. But if the app does
725
- # not use the "Foo" constant we need to be careful not to trigger
726
- # loading "foo.rb" ourselves. While #const_defined? and #const_get? do
727
- # require the file, #autoload? and #remove_const don't.
728
- #
729
- # We are going to remove the constant nonetheless ---which exists as
730
- # far as Ruby is concerned--- because if the user removes the macro
731
- # call from a class or module that were not autoloaded, as in the
732
- # example above with Object, accessing to that constant must err.
733
- unless parent.autoload?(to_remove)
734
- begin
735
- constantized = parent.const_get(to_remove, false)
736
- rescue NameError
737
- # The constant is no longer reachable, just skip it.
738
- return
739
- else
740
- constantized.before_remove_const if constantized.respond_to?(:before_remove_const)
741
- end
742
- end
743
-
744
- begin
745
- parent.instance_eval { remove_const to_remove }
746
- rescue NameError
747
- # The constant is no longer reachable, just skip it.
748
- end
93
+ # Private method that helps configuring the autoloaders.
94
+ def self.eager_load?(path)
95
+ _eager_load_paths.member?(path)
749
96
  end
750
97
  end
751
98
  end
752
-
753
- ActiveSupport::Dependencies.hook!