activesupport 6.1.0 → 7.1.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (225) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +1075 -325
  3. data/MIT-LICENSE +1 -1
  4. data/README.rdoc +7 -7
  5. data/lib/active_support/actionable_error.rb +4 -2
  6. data/lib/active_support/array_inquirer.rb +2 -2
  7. data/lib/active_support/backtrace_cleaner.rb +32 -7
  8. data/lib/active_support/benchmarkable.rb +3 -2
  9. data/lib/active_support/broadcast_logger.rb +251 -0
  10. data/lib/active_support/builder.rb +1 -1
  11. data/lib/active_support/cache/coder.rb +153 -0
  12. data/lib/active_support/cache/entry.rb +134 -0
  13. data/lib/active_support/cache/file_store.rb +53 -20
  14. data/lib/active_support/cache/mem_cache_store.rb +201 -62
  15. data/lib/active_support/cache/memory_store.rb +86 -24
  16. data/lib/active_support/cache/null_store.rb +16 -2
  17. data/lib/active_support/cache/redis_cache_store.rb +186 -193
  18. data/lib/active_support/cache/serializer_with_fallback.rb +175 -0
  19. data/lib/active_support/cache/strategy/local_cache.rb +63 -71
  20. data/lib/active_support/cache.rb +487 -249
  21. data/lib/active_support/callbacks.rb +227 -105
  22. data/lib/active_support/code_generator.rb +70 -0
  23. data/lib/active_support/concern.rb +9 -7
  24. data/lib/active_support/concurrency/load_interlock_aware_monitor.rb +44 -7
  25. data/lib/active_support/concurrency/null_lock.rb +13 -0
  26. data/lib/active_support/concurrency/share_lock.rb +2 -2
  27. data/lib/active_support/configurable.rb +18 -5
  28. data/lib/active_support/configuration_file.rb +7 -2
  29. data/lib/active_support/core_ext/array/access.rb +1 -5
  30. data/lib/active_support/core_ext/array/conversions.rb +15 -13
  31. data/lib/active_support/core_ext/array/grouping.rb +6 -6
  32. data/lib/active_support/core_ext/array/inquiry.rb +2 -2
  33. data/lib/active_support/core_ext/big_decimal/conversions.rb +1 -1
  34. data/lib/active_support/core_ext/class/subclasses.rb +37 -26
  35. data/lib/active_support/core_ext/date/blank.rb +1 -1
  36. data/lib/active_support/core_ext/date/calculations.rb +24 -9
  37. data/lib/active_support/core_ext/date/conversions.rb +16 -15
  38. data/lib/active_support/core_ext/date_and_time/calculations.rb +14 -4
  39. data/lib/active_support/core_ext/date_and_time/compatibility.rb +1 -1
  40. data/lib/active_support/core_ext/date_time/blank.rb +1 -1
  41. data/lib/active_support/core_ext/date_time/calculations.rb +4 -0
  42. data/lib/active_support/core_ext/date_time/conversions.rb +19 -15
  43. data/lib/active_support/core_ext/digest/uuid.rb +30 -13
  44. data/lib/active_support/core_ext/enumerable.rb +85 -83
  45. data/lib/active_support/core_ext/erb/util.rb +196 -0
  46. data/lib/active_support/core_ext/file/atomic.rb +3 -1
  47. data/lib/active_support/core_ext/hash/conversions.rb +1 -2
  48. data/lib/active_support/core_ext/hash/deep_merge.rb +22 -14
  49. data/lib/active_support/core_ext/hash/deep_transform_values.rb +3 -3
  50. data/lib/active_support/core_ext/hash/indifferent_access.rb +3 -3
  51. data/lib/active_support/core_ext/hash/keys.rb +4 -4
  52. data/lib/active_support/core_ext/integer/inflections.rb +12 -12
  53. data/lib/active_support/core_ext/kernel/reporting.rb +4 -4
  54. data/lib/active_support/core_ext/kernel/singleton_class.rb +1 -1
  55. data/lib/active_support/core_ext/module/attribute_accessors.rb +8 -0
  56. data/lib/active_support/core_ext/module/attribute_accessors_per_thread.rb +49 -22
  57. data/lib/active_support/core_ext/module/concerning.rb +6 -6
  58. data/lib/active_support/core_ext/module/delegation.rb +81 -43
  59. data/lib/active_support/core_ext/module/deprecation.rb +15 -12
  60. data/lib/active_support/core_ext/module/introspection.rb +0 -1
  61. data/lib/active_support/core_ext/name_error.rb +2 -8
  62. data/lib/active_support/core_ext/numeric/bytes.rb +9 -0
  63. data/lib/active_support/core_ext/numeric/conversions.rb +82 -77
  64. data/lib/active_support/core_ext/object/acts_like.rb +29 -5
  65. data/lib/active_support/core_ext/object/blank.rb +2 -2
  66. data/lib/active_support/core_ext/object/deep_dup.rb +17 -1
  67. data/lib/active_support/core_ext/object/duplicable.rb +31 -11
  68. data/lib/active_support/core_ext/object/inclusion.rb +13 -5
  69. data/lib/active_support/core_ext/object/instance_variables.rb +22 -12
  70. data/lib/active_support/core_ext/object/json.rb +49 -27
  71. data/lib/active_support/core_ext/object/to_query.rb +2 -4
  72. data/lib/active_support/core_ext/object/try.rb +20 -20
  73. data/lib/active_support/core_ext/object/with.rb +44 -0
  74. data/lib/active_support/core_ext/object/with_options.rb +25 -6
  75. data/lib/active_support/core_ext/object.rb +1 -0
  76. data/lib/active_support/core_ext/pathname/blank.rb +16 -0
  77. data/lib/active_support/core_ext/pathname/existence.rb +23 -0
  78. data/lib/active_support/core_ext/pathname.rb +4 -0
  79. data/lib/active_support/core_ext/range/compare_range.rb +0 -25
  80. data/lib/active_support/core_ext/range/conversions.rb +34 -13
  81. data/lib/active_support/core_ext/range/each.rb +1 -1
  82. data/lib/active_support/core_ext/range/overlap.rb +40 -0
  83. data/lib/active_support/core_ext/range.rb +1 -2
  84. data/lib/active_support/core_ext/securerandom.rb +25 -13
  85. data/lib/active_support/core_ext/string/conversions.rb +2 -2
  86. data/lib/active_support/core_ext/string/filters.rb +21 -15
  87. data/lib/active_support/core_ext/string/indent.rb +1 -1
  88. data/lib/active_support/core_ext/string/inflections.rb +17 -10
  89. data/lib/active_support/core_ext/string/inquiry.rb +1 -1
  90. data/lib/active_support/core_ext/string/output_safety.rb +85 -165
  91. data/lib/active_support/core_ext/symbol/starts_ends_with.rb +0 -8
  92. data/lib/active_support/core_ext/thread/backtrace/location.rb +12 -0
  93. data/lib/active_support/core_ext/time/calculations.rb +30 -8
  94. data/lib/active_support/core_ext/time/conversions.rb +15 -13
  95. data/lib/active_support/core_ext/time/zones.rb +12 -28
  96. data/lib/active_support/core_ext.rb +2 -1
  97. data/lib/active_support/current_attributes.rb +47 -20
  98. data/lib/active_support/deep_mergeable.rb +53 -0
  99. data/lib/active_support/dependencies/autoload.rb +17 -12
  100. data/lib/active_support/dependencies/interlock.rb +10 -18
  101. data/lib/active_support/dependencies/require_dependency.rb +28 -0
  102. data/lib/active_support/dependencies.rb +58 -788
  103. data/lib/active_support/deprecation/behaviors.rb +66 -40
  104. data/lib/active_support/deprecation/constant_accessor.rb +5 -4
  105. data/lib/active_support/deprecation/deprecators.rb +104 -0
  106. data/lib/active_support/deprecation/disallowed.rb +6 -8
  107. data/lib/active_support/deprecation/instance_delegator.rb +31 -4
  108. data/lib/active_support/deprecation/method_wrappers.rb +9 -26
  109. data/lib/active_support/deprecation/proxy_wrappers.rb +38 -23
  110. data/lib/active_support/deprecation/reporting.rb +43 -26
  111. data/lib/active_support/deprecation.rb +32 -5
  112. data/lib/active_support/deprecator.rb +7 -0
  113. data/lib/active_support/descendants_tracker.rb +150 -72
  114. data/lib/active_support/digest.rb +5 -3
  115. data/lib/active_support/duration/iso8601_parser.rb +3 -3
  116. data/lib/active_support/duration/iso8601_serializer.rb +9 -3
  117. data/lib/active_support/duration.rb +83 -52
  118. data/lib/active_support/encrypted_configuration.rb +72 -9
  119. data/lib/active_support/encrypted_file.rb +29 -13
  120. data/lib/active_support/environment_inquirer.rb +23 -3
  121. data/lib/active_support/error_reporter/test_helper.rb +15 -0
  122. data/lib/active_support/error_reporter.rb +203 -0
  123. data/lib/active_support/evented_file_update_checker.rb +20 -7
  124. data/lib/active_support/execution_context/test_helper.rb +13 -0
  125. data/lib/active_support/execution_context.rb +53 -0
  126. data/lib/active_support/execution_wrapper.rb +44 -22
  127. data/lib/active_support/executor/test_helper.rb +7 -0
  128. data/lib/active_support/file_update_checker.rb +4 -2
  129. data/lib/active_support/fork_tracker.rb +28 -11
  130. data/lib/active_support/gem_version.rb +4 -4
  131. data/lib/active_support/gzip.rb +2 -0
  132. data/lib/active_support/hash_with_indifferent_access.rb +44 -19
  133. data/lib/active_support/html_safe_translation.rb +53 -0
  134. data/lib/active_support/i18n.rb +2 -1
  135. data/lib/active_support/i18n_railtie.rb +21 -14
  136. data/lib/active_support/inflector/inflections.rb +25 -7
  137. data/lib/active_support/inflector/methods.rb +50 -64
  138. data/lib/active_support/inflector/transliterate.rb +4 -2
  139. data/lib/active_support/isolated_execution_state.rb +76 -0
  140. data/lib/active_support/json/decoding.rb +2 -1
  141. data/lib/active_support/json/encoding.rb +27 -45
  142. data/lib/active_support/key_generator.rb +31 -6
  143. data/lib/active_support/lazy_load_hooks.rb +33 -7
  144. data/lib/active_support/locale/en.yml +4 -2
  145. data/lib/active_support/log_subscriber/test_helper.rb +2 -2
  146. data/lib/active_support/log_subscriber.rb +97 -35
  147. data/lib/active_support/logger.rb +9 -60
  148. data/lib/active_support/logger_thread_safe_level.rb +11 -34
  149. data/lib/active_support/message_encryptor.rb +206 -56
  150. data/lib/active_support/message_encryptors.rb +141 -0
  151. data/lib/active_support/message_pack/cache_serializer.rb +23 -0
  152. data/lib/active_support/message_pack/extensions.rb +292 -0
  153. data/lib/active_support/message_pack/serializer.rb +63 -0
  154. data/lib/active_support/message_pack.rb +50 -0
  155. data/lib/active_support/message_verifier.rb +235 -84
  156. data/lib/active_support/message_verifiers.rb +135 -0
  157. data/lib/active_support/messages/codec.rb +65 -0
  158. data/lib/active_support/messages/metadata.rb +112 -46
  159. data/lib/active_support/messages/rotation_coordinator.rb +93 -0
  160. data/lib/active_support/messages/rotator.rb +34 -32
  161. data/lib/active_support/messages/serializer_with_fallback.rb +158 -0
  162. data/lib/active_support/multibyte/chars.rb +12 -11
  163. data/lib/active_support/multibyte/unicode.rb +9 -49
  164. data/lib/active_support/multibyte.rb +1 -1
  165. data/lib/active_support/notifications/fanout.rb +304 -114
  166. data/lib/active_support/notifications/instrumenter.rb +117 -35
  167. data/lib/active_support/notifications.rb +25 -25
  168. data/lib/active_support/number_helper/number_converter.rb +14 -7
  169. data/lib/active_support/number_helper/number_to_currency_converter.rb +11 -6
  170. data/lib/active_support/number_helper/number_to_delimited_converter.rb +1 -1
  171. data/lib/active_support/number_helper/number_to_human_size_converter.rb +4 -4
  172. data/lib/active_support/number_helper/number_to_phone_converter.rb +2 -1
  173. data/lib/active_support/number_helper/number_to_rounded_converter.rb +10 -6
  174. data/lib/active_support/number_helper/rounding_helper.rb +2 -6
  175. data/lib/active_support/number_helper.rb +379 -319
  176. data/lib/active_support/option_merger.rb +10 -18
  177. data/lib/active_support/ordered_hash.rb +4 -4
  178. data/lib/active_support/ordered_options.rb +15 -1
  179. data/lib/active_support/parameter_filter.rb +105 -81
  180. data/lib/active_support/proxy_object.rb +2 -0
  181. data/lib/active_support/railtie.rb +83 -21
  182. data/lib/active_support/reloader.rb +13 -5
  183. data/lib/active_support/rescuable.rb +18 -16
  184. data/lib/active_support/ruby_features.rb +7 -0
  185. data/lib/active_support/secure_compare_rotator.rb +18 -11
  186. data/lib/active_support/security_utils.rb +1 -1
  187. data/lib/active_support/string_inquirer.rb +3 -3
  188. data/lib/active_support/subscriber.rb +11 -40
  189. data/lib/active_support/syntax_error_proxy.rb +60 -0
  190. data/lib/active_support/tagged_logging.rb +65 -25
  191. data/lib/active_support/test_case.rb +166 -27
  192. data/lib/active_support/testing/assertions.rb +61 -15
  193. data/lib/active_support/testing/autorun.rb +0 -2
  194. data/lib/active_support/testing/constant_stubbing.rb +32 -0
  195. data/lib/active_support/testing/deprecation.rb +53 -2
  196. data/lib/active_support/testing/error_reporter_assertions.rb +107 -0
  197. data/lib/active_support/testing/isolation.rb +30 -29
  198. data/lib/active_support/testing/method_call_assertions.rb +24 -11
  199. data/lib/active_support/testing/parallelization/server.rb +4 -0
  200. data/lib/active_support/testing/parallelization/worker.rb +3 -0
  201. data/lib/active_support/testing/parallelization.rb +4 -0
  202. data/lib/active_support/testing/parallelize_executor.rb +81 -0
  203. data/lib/active_support/testing/setup_and_teardown.rb +2 -0
  204. data/lib/active_support/testing/stream.rb +4 -6
  205. data/lib/active_support/testing/strict_warnings.rb +39 -0
  206. data/lib/active_support/testing/tagged_logging.rb +1 -1
  207. data/lib/active_support/testing/time_helpers.rb +49 -16
  208. data/lib/active_support/time_with_zone.rb +39 -28
  209. data/lib/active_support/values/time_zone.rb +50 -18
  210. data/lib/active_support/version.rb +1 -1
  211. data/lib/active_support/xml_mini/jdom.rb +4 -11
  212. data/lib/active_support/xml_mini/libxml.rb +5 -5
  213. data/lib/active_support/xml_mini/libxmlsax.rb +1 -1
  214. data/lib/active_support/xml_mini/nokogiri.rb +5 -5
  215. data/lib/active_support/xml_mini/nokogirisax.rb +2 -2
  216. data/lib/active_support/xml_mini/rexml.rb +2 -2
  217. data/lib/active_support/xml_mini.rb +7 -6
  218. data/lib/active_support.rb +28 -1
  219. metadata +150 -18
  220. data/lib/active_support/core_ext/marshal.rb +0 -26
  221. data/lib/active_support/core_ext/range/include_time_with_zone.rb +0 -28
  222. data/lib/active_support/core_ext/range/overlaps.rb +0 -10
  223. data/lib/active_support/core_ext/uri.rb +0 -29
  224. data/lib/active_support/dependencies/zeitwerk_integration.rb +0 -117
  225. data/lib/active_support/per_thread_registry.rb +0 -60
@@ -11,34 +11,50 @@ module ActiveSupport
11
11
  attr_accessor :gem_name
12
12
 
13
13
  # Outputs a deprecation warning to the output configured by
14
- # <tt>ActiveSupport::Deprecation.behavior</tt>.
14
+ # ActiveSupport::Deprecation#behavior.
15
15
  #
16
- # ActiveSupport::Deprecation.warn('something broke!')
16
+ # ActiveSupport::Deprecation.new.warn('something broke!')
17
17
  # # => "DEPRECATION WARNING: something broke! (called from your_code.rb:1)"
18
18
  def warn(message = nil, callstack = nil)
19
19
  return if silenced
20
20
 
21
21
  callstack ||= caller_locations(2)
22
- deprecation_message(callstack, message).tap do |m|
22
+ deprecation_message(callstack, message).tap do |full_message|
23
23
  if deprecation_disallowed?(message)
24
- disallowed_behavior.each { |b| b.call(m, callstack, deprecation_horizon, gem_name) }
24
+ disallowed_behavior.each { |b| b.call(full_message, callstack, self) }
25
25
  else
26
- behavior.each { |b| b.call(m, callstack, deprecation_horizon, gem_name) }
26
+ behavior.each { |b| b.call(full_message, callstack, self) }
27
27
  end
28
28
  end
29
29
  end
30
30
 
31
31
  # Silence deprecation warnings within the block.
32
32
  #
33
- # ActiveSupport::Deprecation.warn('something broke!')
33
+ # deprecator = ActiveSupport::Deprecation.new
34
+ # deprecator.warn('something broke!')
34
35
  # # => "DEPRECATION WARNING: something broke! (called from your_code.rb:1)"
35
36
  #
36
- # ActiveSupport::Deprecation.silence do
37
- # ActiveSupport::Deprecation.warn('something broke!')
37
+ # deprecator.silence do
38
+ # deprecator.warn('something broke!')
38
39
  # end
39
40
  # # => nil
40
41
  def silence(&block)
41
- @silenced_thread.bind(true, &block)
42
+ begin_silence
43
+ block.call
44
+ ensure
45
+ end_silence
46
+ end
47
+
48
+ def begin_silence # :nodoc:
49
+ @silence_counter.value += 1
50
+ end
51
+
52
+ def end_silence # :nodoc:
53
+ @silence_counter.value -= 1
54
+ end
55
+
56
+ def silenced
57
+ @silenced || @silence_counter.value.nonzero?
42
58
  end
43
59
 
44
60
  # Allow previously disallowed deprecation warnings within the block.
@@ -46,27 +62,28 @@ module ActiveSupport
46
62
  # expressions. (Symbols are treated as strings). These are compared against
47
63
  # the text of deprecation warning messages generated within the block.
48
64
  # Matching warnings will be exempt from the rules set by
49
- # +ActiveSupport::Deprecation.disallowed_warnings+
65
+ # ActiveSupport::Deprecation#disallowed_warnings.
50
66
  #
51
67
  # The optional <tt>if:</tt> argument accepts a truthy/falsy value or an object that
52
68
  # responds to <tt>.call</tt>. If truthy, then matching warnings will be allowed.
53
69
  # If falsey then the method yields to the block without allowing the warning.
54
70
  #
55
- # ActiveSupport::Deprecation.disallowed_behavior = :raise
56
- # ActiveSupport::Deprecation.disallowed_warnings = [
71
+ # deprecator = ActiveSupport::Deprecation.new
72
+ # deprecator.disallowed_behavior = :raise
73
+ # deprecator.disallowed_warnings = [
57
74
  # "something broke"
58
75
  # ]
59
76
  #
60
- # ActiveSupport::Deprecation.warn('something broke!')
77
+ # deprecator.warn('something broke!')
61
78
  # # => ActiveSupport::DeprecationException
62
79
  #
63
- # ActiveSupport::Deprecation.allow ['something broke'] do
64
- # ActiveSupport::Deprecation.warn('something broke!')
80
+ # deprecator.allow ['something broke'] do
81
+ # deprecator.warn('something broke!')
65
82
  # end
66
83
  # # => nil
67
84
  #
68
- # ActiveSupport::Deprecation.allow ['something broke'], if: Rails.env.production? do
69
- # ActiveSupport::Deprecation.warn('something broke!')
85
+ # deprecator.allow ['something broke'], if: Rails.env.production? do
86
+ # deprecator.warn('something broke!')
70
87
  # end
71
88
  # # => ActiveSupport::DeprecationException for dev/test, nil for production
72
89
  def allow(allowed_warnings = :all, if: true, &block)
@@ -79,10 +96,6 @@ module ActiveSupport
79
96
  end
80
97
  end
81
98
 
82
- def silenced
83
- @silenced || @silenced_thread.value
84
- end
85
-
86
99
  def deprecation_warning(deprecated_method_name, message = nil, caller_backtrace = nil)
87
100
  caller_backtrace ||= caller_locations(2)
88
101
  deprecated_method_warning(deprecated_method_name, message).tap do |msg|
@@ -125,10 +138,13 @@ module ActiveSupport
125
138
  end
126
139
 
127
140
  def extract_callstack(callstack)
141
+ return [] if callstack.empty?
128
142
  return _extract_callstack(callstack) if callstack.first.is_a? String
129
143
 
130
144
  offending_line = callstack.find { |frame|
131
- frame.absolute_path && !ignored_callstack(frame.absolute_path)
145
+ # Code generated with `eval` doesn't have an `absolute_path`, e.g. templates.
146
+ path = frame.absolute_path || frame.path
147
+ path && !ignored_callstack?(path)
132
148
  } || callstack.first
133
149
 
134
150
  [offending_line.path, offending_line.lineno, offending_line.label]
@@ -136,7 +152,7 @@ module ActiveSupport
136
152
 
137
153
  def _extract_callstack(callstack)
138
154
  warn "Please pass `caller_locations` to the deprecation API" if $VERBOSE
139
- offending_line = callstack.find { |line| !ignored_callstack(line) } || callstack.first
155
+ offending_line = callstack.find { |line| !ignored_callstack?(line) } || callstack.first
140
156
 
141
157
  if offending_line
142
158
  if md = offending_line.match(/^(.+?):(\d+)(?::in `(.*?)')?/)
@@ -147,10 +163,11 @@ module ActiveSupport
147
163
  end
148
164
  end
149
165
 
150
- RAILS_GEM_ROOT = File.expand_path("../../../..", __dir__) + "/"
166
+ RAILS_GEM_ROOT = File.expand_path("../../../..", __dir__) + "/" # :nodoc:
167
+ LIB_DIR = RbConfig::CONFIG["libdir"] # :nodoc:
151
168
 
152
- def ignored_callstack(path)
153
- path.start_with?(RAILS_GEM_ROOT) || path.start_with?(RbConfig::CONFIG["rubylibdir"])
169
+ def ignored_callstack?(path)
170
+ path.start_with?(RAILS_GEM_ROOT, LIB_DIR)
154
171
  end
155
172
  end
156
173
  end
@@ -3,8 +3,35 @@
3
3
  require "singleton"
4
4
 
5
5
  module ActiveSupport
6
- # \Deprecation specifies the API used by Rails to deprecate methods, instance
7
- # variables, objects and constants.
6
+ # = Active Support \Deprecation
7
+ #
8
+ # \Deprecation specifies the API used by \Rails to deprecate methods, instance variables, objects, and constants. It's
9
+ # also available for gems or applications.
10
+ #
11
+ # For a gem, use Deprecation.new to create a Deprecation object and store it in your module or class (in order for
12
+ # users to be able to configure it).
13
+ #
14
+ # module MyLibrary
15
+ # def self.deprecator
16
+ # @deprecator ||= ActiveSupport::Deprecation.new("2.0", "MyLibrary")
17
+ # end
18
+ # end
19
+ #
20
+ # For a Railtie or Engine, you may also want to add it to the application's deprecators, so that the application's
21
+ # configuration can be applied to it.
22
+ #
23
+ # module MyLibrary
24
+ # class Railtie < Rails::Railtie
25
+ # initializer "my_library.deprecator" do |app|
26
+ # app.deprecators[:my_library] = MyLibrary.deprecator
27
+ # end
28
+ # end
29
+ # end
30
+ #
31
+ # With the above initializer, configuration settings like the following will affect +MyLibrary.deprecator+:
32
+ #
33
+ # # in config/environments/test.rb
34
+ # config.active_support.deprecation = :raise
8
35
  class Deprecation
9
36
  # active_support.rb sets an autoload for ActiveSupport::Deprecation.
10
37
  #
@@ -21,10 +48,10 @@ module ActiveSupport
21
48
  require "active_support/deprecation/constant_accessor"
22
49
  require "active_support/deprecation/method_wrappers"
23
50
  require "active_support/deprecation/proxy_wrappers"
51
+ require "active_support/deprecation/deprecators"
24
52
  require "active_support/core_ext/module/deprecation"
25
53
  require "concurrent/atomic/thread_local_var"
26
54
 
27
- include Singleton
28
55
  include InstanceDelegator
29
56
  include Behavior
30
57
  include Reporting
@@ -38,13 +65,13 @@ module ActiveSupport
38
65
  # and the second is a library name.
39
66
  #
40
67
  # ActiveSupport::Deprecation.new('2.0', 'MyLibrary')
41
- def initialize(deprecation_horizon = "6.2", gem_name = "Rails")
68
+ def initialize(deprecation_horizon = "7.2", gem_name = "Rails")
42
69
  self.gem_name = gem_name
43
70
  self.deprecation_horizon = deprecation_horizon
44
71
  # By default, warnings are not silenced and debugging is off.
45
72
  self.silenced = false
46
73
  self.debug = false
47
- @silenced_thread = Concurrent::ThreadLocalVar.new(false)
74
+ @silence_counter = Concurrent::ThreadLocalVar.new(0)
48
75
  @explicitly_allowed_warnings = Concurrent::ThreadLocalVar.new(nil)
49
76
  end
50
77
  end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveSupport
4
+ def self.deprecator # :nodoc:
5
+ ActiveSupport::Deprecation._instance
6
+ end
7
+ end
@@ -1,112 +1,190 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "weakref"
4
+ require "active_support/ruby_features"
4
5
 
5
6
  module ActiveSupport
7
+ # = Active Support Descendants Tracker
8
+ #
6
9
  # This module provides an internal implementation to track descendants
7
- # which is faster than iterating through ObjectSpace.
10
+ # which is faster than iterating through +ObjectSpace+.
11
+ #
12
+ # However Ruby 3.1 provide a fast native +Class#subclasses+ method,
13
+ # so if you know your code won't be executed on older rubies, including
14
+ # +ActiveSupport::DescendantsTracker+ does not provide any benefit.
8
15
  module DescendantsTracker
9
- @@direct_descendants = {}
16
+ @clear_disabled = false
10
17
 
11
- class << self
12
- def direct_descendants(klass)
13
- descendants = @@direct_descendants[klass]
14
- descendants ? descendants.to_a : []
15
- end
16
- alias_method :subclasses, :direct_descendants
18
+ if RUBY_ENGINE == "ruby"
19
+ # On MRI `ObjectSpace::WeakMap` keys are weak references.
20
+ # So we can simply use WeakMap as a `Set`.
21
+ class WeakSet < ObjectSpace::WeakMap # :nodoc:
22
+ alias_method :to_a, :keys
17
23
 
18
- def descendants(klass)
19
- arr = []
20
- accumulate_descendants(klass, arr)
21
- arr
24
+ def <<(object)
25
+ self[object] = true
26
+ end
22
27
  end
28
+ else
29
+ # On TruffleRuby `ObjectSpace::WeakMap` keys are strong references.
30
+ # So we use `object_id` as a key and the actual object as a value.
31
+ #
32
+ # JRuby for now doesn't have Class#descendant, but when it will, it will likely
33
+ # have the same WeakMap semantic than Truffle so we future proof this as much as possible.
34
+ class WeakSet # :nodoc:
35
+ def initialize
36
+ @map = ObjectSpace::WeakMap.new
37
+ end
23
38
 
24
- def clear
25
- if defined? ActiveSupport::Dependencies
26
- @@direct_descendants.each do |klass, descendants|
27
- if Dependencies.autoloaded?(klass)
28
- @@direct_descendants.delete(klass)
29
- else
30
- descendants.reject! { |v| Dependencies.autoloaded?(v) }
31
- end
32
- end
33
- else
34
- @@direct_descendants.clear
39
+ def [](object)
40
+ @map.key?(object.object_id)
35
41
  end
36
- end
42
+ alias_method :include?, :[]
37
43
 
38
- # This is the only method that is not thread safe, but is only ever called
39
- # during the eager loading phase.
40
- def store_inherited(klass, descendant)
41
- (@@direct_descendants[klass] ||= DescendantsArray.new) << descendant
42
- end
44
+ def []=(object, _present)
45
+ @map[object.object_id] = object
46
+ end
43
47
 
44
- private
45
- def accumulate_descendants(klass, acc)
46
- if direct_descendants = @@direct_descendants[klass]
47
- direct_descendants.each do |direct_descendant|
48
- acc << direct_descendant
49
- accumulate_descendants(direct_descendant, acc)
50
- end
51
- end
48
+ def to_a
49
+ @map.values
52
50
  end
53
- end
54
51
 
55
- def inherited(base)
56
- DescendantsTracker.store_inherited(self, base)
57
- super
52
+ def <<(object)
53
+ self[object] = true
54
+ end
55
+ end
58
56
  end
57
+ @excluded_descendants = WeakSet.new
58
+
59
+ module ReloadedClassesFiltering # :nodoc:
60
+ def subclasses
61
+ DescendantsTracker.reject!(super)
62
+ end
59
63
 
60
- def direct_descendants
61
- DescendantsTracker.direct_descendants(self)
64
+ def descendants
65
+ DescendantsTracker.reject!(super)
66
+ end
62
67
  end
63
- alias_method :subclasses, :direct_descendants
64
68
 
65
- def descendants
66
- DescendantsTracker.descendants(self)
69
+ class << self
70
+ def disable_clear! # :nodoc:
71
+ unless @clear_disabled
72
+ @clear_disabled = true
73
+ ReloadedClassesFiltering.remove_method(:subclasses)
74
+ ReloadedClassesFiltering.remove_method(:descendants)
75
+ @excluded_descendants = nil
76
+ end
77
+ end
78
+
79
+ def clear(classes) # :nodoc:
80
+ raise "DescendantsTracker.clear was disabled because config.enable_reloading is false" if @clear_disabled
81
+
82
+ classes.each do |klass|
83
+ @excluded_descendants << klass
84
+ klass.descendants.each do |descendant|
85
+ @excluded_descendants << descendant
86
+ end
87
+ end
88
+ end
89
+
90
+ def reject!(classes) # :nodoc:
91
+ if @excluded_descendants
92
+ classes.reject! { |d| @excluded_descendants.include?(d) }
93
+ end
94
+ classes
95
+ end
67
96
  end
68
97
 
69
- # DescendantsArray is an array that contains weak references to classes.
70
- class DescendantsArray # :nodoc:
71
- include Enumerable
98
+ if RubyFeatures::CLASS_SUBCLASSES
99
+ class << self
100
+ def subclasses(klass)
101
+ klass.subclasses
102
+ end
72
103
 
73
- def initialize
74
- @refs = []
104
+ def descendants(klass)
105
+ klass.descendants
106
+ end
75
107
  end
76
108
 
77
- def initialize_copy(orig)
78
- @refs = @refs.dup
109
+ def descendants
110
+ subclasses = DescendantsTracker.reject!(self.subclasses)
111
+ subclasses.concat(subclasses.flat_map(&:descendants))
79
112
  end
113
+ else
114
+ # DescendantsArray is an array that contains weak references to classes.
115
+ # Note: DescendantsArray is redundant with WeakSet, however WeakSet when used
116
+ # on Ruby 2.7 or 3.0 can trigger a Ruby crash: https://bugs.ruby-lang.org/issues/18928
117
+ class DescendantsArray # :nodoc:
118
+ include Enumerable
119
+
120
+ def initialize
121
+ @refs = []
122
+ end
123
+
124
+ def <<(klass)
125
+ @refs << WeakRef.new(klass)
126
+ end
127
+
128
+ def each
129
+ @refs.reject! do |ref|
130
+ yield ref.__getobj__
131
+ false
132
+ rescue WeakRef::RefError
133
+ true
134
+ end
135
+ self
136
+ end
137
+
138
+ def refs_size
139
+ @refs.size
140
+ end
141
+
142
+ def cleanup!
143
+ @refs.delete_if { |ref| !ref.weakref_alive? }
144
+ end
80
145
 
81
- def <<(klass)
82
- @refs << WeakRef.new(klass)
146
+ def reject!
147
+ @refs.reject! do |ref|
148
+ yield ref.__getobj__
149
+ rescue WeakRef::RefError
150
+ true
151
+ end
152
+ end
83
153
  end
84
154
 
85
- def each
86
- @refs.reject! do |ref|
87
- yield ref.__getobj__
88
- false
89
- rescue WeakRef::RefError
90
- true
155
+ @direct_descendants = {}
156
+
157
+ class << self
158
+ def subclasses(klass)
159
+ descendants = @direct_descendants[klass]
160
+ descendants ? DescendantsTracker.reject!(descendants.to_a) : []
161
+ end
162
+
163
+ def descendants(klass)
164
+ subclasses = self.subclasses(klass)
165
+ subclasses.concat(subclasses.flat_map { |k| descendants(k) })
166
+ end
167
+
168
+ # This is the only method that is not thread safe, but is only ever called
169
+ # during the eager loading phase.
170
+ def store_inherited(klass, descendant) # :nodoc:
171
+ (@direct_descendants[klass] ||= DescendantsArray.new) << descendant
91
172
  end
92
- self
93
173
  end
94
174
 
95
- def refs_size
96
- @refs.size
175
+ def subclasses
176
+ DescendantsTracker.subclasses(self)
97
177
  end
98
178
 
99
- def cleanup!
100
- @refs.delete_if { |ref| !ref.weakref_alive? }
179
+ def descendants
180
+ DescendantsTracker.descendants(self)
101
181
  end
102
182
 
103
- def reject!
104
- @refs.reject! do |ref|
105
- yield ref.__getobj__
106
- rescue WeakRef::RefError
107
- true
183
+ private
184
+ def inherited(base) # :nodoc:
185
+ DescendantsTracker.store_inherited(self, base)
186
+ super
108
187
  end
109
- end
110
188
  end
111
189
  end
112
190
  end
@@ -1,10 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "openssl"
4
+
3
5
  module ActiveSupport
4
- class Digest #:nodoc:
5
- class <<self
6
+ class Digest # :nodoc:
7
+ class << self
6
8
  def hash_digest_class
7
- @hash_digest_class ||= ::Digest::MD5
9
+ @hash_digest_class ||= OpenSSL::Digest::MD5
8
10
  end
9
11
 
10
12
  def hash_digest_class=(klass)
@@ -16,11 +16,11 @@ module ActiveSupport
16
16
  PERIOD = "."
17
17
  COMMA = ","
18
18
 
19
- SIGN_MARKER = /\A\-|\+|/
19
+ SIGN_MARKER = /\A-|\+|/
20
20
  DATE_MARKER = /P/
21
21
  TIME_MARKER = /T/
22
- DATE_COMPONENT = /(\-?\d+(?:[.,]\d+)?)(Y|M|D|W)/
23
- TIME_COMPONENT = /(\-?\d+(?:[.,]\d+)?)(H|M|S)/
22
+ DATE_COMPONENT = /(-?\d+(?:[.,]\d+)?)(Y|M|D|W)/
23
+ TIME_COMPONENT = /(-?\d+(?:[.,]\d+)?)(H|M|S)/
24
24
 
25
25
  DATE_TO_PART = { "Y" => :years, "M" => :months, "W" => :weeks, "D" => :days }
26
26
  TIME_TO_PART = { "H" => :hours, "M" => :minutes, "S" => :seconds }
@@ -1,7 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "active_support/core_ext/object/blank"
4
-
5
3
  module ActiveSupport
6
4
  class Duration
7
5
  # Serializes duration to string according to ISO 8601 Duration format.
@@ -27,7 +25,7 @@ module ActiveSupport
27
25
  time << "#{parts[:hours]}H" if parts.key?(:hours)
28
26
  time << "#{parts[:minutes]}M" if parts.key?(:minutes)
29
27
  if parts.key?(:seconds)
30
- time << "#{sprintf(@precision ? "%0.0#{@precision}f" : '%g', parts[:seconds])}S"
28
+ time << "#{format_seconds(parts[:seconds])}S"
31
29
  end
32
30
  output << "T#{time}" unless time.empty?
33
31
  output
@@ -54,6 +52,14 @@ module ActiveSupport
54
52
  def week_mixed_with_date?(parts)
55
53
  parts.key?(:weeks) && (parts.keys & DATE_COMPONENTS).any?
56
54
  end
55
+
56
+ def format_seconds(seconds)
57
+ if @precision
58
+ sprintf("%0.0#{@precision}f", seconds)
59
+ else
60
+ seconds.to_s
61
+ end
62
+ end
57
63
  end
58
64
  end
59
65
  end