activesupport 7.0.8.7 → 7.2.3

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 (203) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +229 -397
  3. data/MIT-LICENSE +1 -1
  4. data/README.rdoc +5 -5
  5. data/lib/active_support/actionable_error.rb +3 -1
  6. data/lib/active_support/array_inquirer.rb +3 -1
  7. data/lib/active_support/backtrace_cleaner.rb +39 -7
  8. data/lib/active_support/benchmarkable.rb +1 -0
  9. data/lib/active_support/broadcast_logger.rb +238 -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 +51 -19
  14. data/lib/active_support/cache/mem_cache_store.rb +98 -134
  15. data/lib/active_support/cache/memory_store.rb +85 -30
  16. data/lib/active_support/cache/null_store.rb +8 -2
  17. data/lib/active_support/cache/redis_cache_store.rb +166 -153
  18. data/lib/active_support/cache/serializer_with_fallback.rb +152 -0
  19. data/lib/active_support/cache/strategy/local_cache.rb +64 -13
  20. data/lib/active_support/cache.rb +364 -292
  21. data/lib/active_support/callbacks.rb +121 -136
  22. data/lib/active_support/code_generator.rb +15 -10
  23. data/lib/active_support/concern.rb +4 -2
  24. data/lib/active_support/concurrency/load_interlock_aware_monitor.rb +42 -3
  25. data/lib/active_support/concurrency/null_lock.rb +13 -0
  26. data/lib/active_support/configurable.rb +10 -0
  27. data/lib/active_support/core_ext/array/conversions.rb +1 -2
  28. data/lib/active_support/core_ext/array.rb +0 -1
  29. data/lib/active_support/core_ext/benchmark.rb +1 -0
  30. data/lib/active_support/core_ext/class/attribute.rb +2 -2
  31. data/lib/active_support/core_ext/class/subclasses.rb +17 -34
  32. data/lib/active_support/core_ext/date/blank.rb +4 -0
  33. data/lib/active_support/core_ext/date/conversions.rb +1 -2
  34. data/lib/active_support/core_ext/date.rb +0 -1
  35. data/lib/active_support/core_ext/date_and_time/calculations.rb +10 -0
  36. data/lib/active_support/core_ext/date_and_time/compatibility.rb +28 -1
  37. data/lib/active_support/core_ext/date_time/blank.rb +4 -0
  38. data/lib/active_support/core_ext/date_time/conversions.rb +6 -4
  39. data/lib/active_support/core_ext/date_time.rb +0 -1
  40. data/lib/active_support/core_ext/digest/uuid.rb +7 -10
  41. data/lib/active_support/core_ext/enumerable.rb +20 -80
  42. data/lib/active_support/core_ext/erb/util.rb +201 -0
  43. data/lib/active_support/core_ext/hash/conversions.rb +1 -1
  44. data/lib/active_support/core_ext/hash/deep_merge.rb +22 -14
  45. data/lib/active_support/core_ext/hash/keys.rb +4 -4
  46. data/lib/active_support/core_ext/module/attr_internal.rb +17 -6
  47. data/lib/active_support/core_ext/module/attribute_accessors.rb +6 -0
  48. data/lib/active_support/core_ext/module/attribute_accessors_per_thread.rb +34 -16
  49. data/lib/active_support/core_ext/module/concerning.rb +6 -6
  50. data/lib/active_support/core_ext/module/delegation.rb +20 -119
  51. data/lib/active_support/core_ext/module/deprecation.rb +12 -12
  52. data/lib/active_support/core_ext/module/introspection.rb +3 -1
  53. data/lib/active_support/core_ext/numeric/bytes.rb +9 -0
  54. data/lib/active_support/core_ext/numeric/conversions.rb +5 -3
  55. data/lib/active_support/core_ext/numeric.rb +0 -1
  56. data/lib/active_support/core_ext/object/blank.rb +45 -1
  57. data/lib/active_support/core_ext/object/deep_dup.rb +16 -0
  58. data/lib/active_support/core_ext/object/inclusion.rb +13 -5
  59. data/lib/active_support/core_ext/object/instance_variables.rb +4 -2
  60. data/lib/active_support/core_ext/object/json.rb +17 -7
  61. data/lib/active_support/core_ext/object/try.rb +2 -2
  62. data/lib/active_support/core_ext/object/with.rb +46 -0
  63. data/lib/active_support/core_ext/object/with_options.rb +4 -4
  64. data/lib/active_support/core_ext/object.rb +1 -0
  65. data/lib/active_support/core_ext/pathname/blank.rb +20 -0
  66. data/lib/active_support/core_ext/pathname/existence.rb +2 -0
  67. data/lib/active_support/core_ext/pathname.rb +1 -0
  68. data/lib/active_support/core_ext/range/conversions.rb +28 -7
  69. data/lib/active_support/core_ext/range/overlap.rb +40 -0
  70. data/lib/active_support/core_ext/range/sole.rb +17 -0
  71. data/lib/active_support/core_ext/range.rb +2 -2
  72. data/lib/active_support/core_ext/securerandom.rb +24 -12
  73. data/lib/active_support/core_ext/string/conversions.rb +1 -1
  74. data/lib/active_support/core_ext/string/filters.rb +24 -18
  75. data/lib/active_support/core_ext/string/indent.rb +1 -1
  76. data/lib/active_support/core_ext/string/inflections.rb +16 -5
  77. data/lib/active_support/core_ext/string/multibyte.rb +3 -3
  78. data/lib/active_support/core_ext/string/output_safety.rb +34 -177
  79. data/lib/active_support/core_ext/thread/backtrace/location.rb +12 -0
  80. data/lib/active_support/core_ext/time/calculations.rb +36 -30
  81. data/lib/active_support/core_ext/time/compatibility.rb +24 -0
  82. data/lib/active_support/core_ext/time/conversions.rb +1 -3
  83. data/lib/active_support/core_ext/time/zones.rb +4 -4
  84. data/lib/active_support/core_ext/time.rb +0 -1
  85. data/lib/active_support/core_ext.rb +0 -1
  86. data/lib/active_support/current_attributes.rb +60 -46
  87. data/lib/active_support/deep_mergeable.rb +53 -0
  88. data/lib/active_support/delegation.rb +202 -0
  89. data/lib/active_support/dependencies/autoload.rb +9 -16
  90. data/lib/active_support/deprecation/behaviors.rb +65 -42
  91. data/lib/active_support/deprecation/constant_accessor.rb +47 -25
  92. data/lib/active_support/deprecation/deprecators.rb +104 -0
  93. data/lib/active_support/deprecation/disallowed.rb +3 -5
  94. data/lib/active_support/deprecation/method_wrappers.rb +6 -23
  95. data/lib/active_support/deprecation/proxy_wrappers.rb +34 -22
  96. data/lib/active_support/deprecation/reporting.rb +49 -27
  97. data/lib/active_support/deprecation.rb +39 -9
  98. data/lib/active_support/deprecator.rb +7 -0
  99. data/lib/active_support/descendants_tracker.rb +66 -172
  100. data/lib/active_support/duration/iso8601_parser.rb +2 -2
  101. data/lib/active_support/duration/iso8601_serializer.rb +1 -4
  102. data/lib/active_support/duration.rb +13 -7
  103. data/lib/active_support/encrypted_configuration.rb +30 -9
  104. data/lib/active_support/encrypted_file.rb +9 -4
  105. data/lib/active_support/environment_inquirer.rb +22 -2
  106. data/lib/active_support/error_reporter/test_helper.rb +15 -0
  107. data/lib/active_support/error_reporter.rb +163 -36
  108. data/lib/active_support/evented_file_update_checker.rb +0 -1
  109. data/lib/active_support/execution_wrapper.rb +5 -6
  110. data/lib/active_support/file_update_checker.rb +6 -4
  111. data/lib/active_support/fork_tracker.rb +4 -32
  112. data/lib/active_support/gem_version.rb +4 -4
  113. data/lib/active_support/gzip.rb +2 -0
  114. data/lib/active_support/hash_with_indifferent_access.rb +50 -30
  115. data/lib/active_support/html_safe_translation.rb +19 -6
  116. data/lib/active_support/i18n.rb +1 -1
  117. data/lib/active_support/i18n_railtie.rb +20 -13
  118. data/lib/active_support/inflector/inflections.rb +2 -0
  119. data/lib/active_support/inflector/methods.rb +23 -11
  120. data/lib/active_support/inflector/transliterate.rb +3 -1
  121. data/lib/active_support/isolated_execution_state.rb +26 -22
  122. data/lib/active_support/json/decoding.rb +3 -2
  123. data/lib/active_support/json/encoding.rb +48 -48
  124. data/lib/active_support/key_generator.rb +9 -1
  125. data/lib/active_support/lazy_load_hooks.rb +7 -5
  126. data/lib/active_support/locale/en.yml +2 -0
  127. data/lib/active_support/log_subscriber.rb +74 -34
  128. data/lib/active_support/logger.rb +22 -60
  129. data/lib/active_support/logger_thread_safe_level.rb +10 -32
  130. data/lib/active_support/message_encryptor.rb +197 -53
  131. data/lib/active_support/message_encryptors.rb +141 -0
  132. data/lib/active_support/message_pack/cache_serializer.rb +23 -0
  133. data/lib/active_support/message_pack/extensions.rb +305 -0
  134. data/lib/active_support/message_pack/serializer.rb +63 -0
  135. data/lib/active_support/message_pack.rb +50 -0
  136. data/lib/active_support/message_verifier.rb +229 -89
  137. data/lib/active_support/message_verifiers.rb +137 -0
  138. data/lib/active_support/messages/codec.rb +65 -0
  139. data/lib/active_support/messages/metadata.rb +111 -45
  140. data/lib/active_support/messages/rotation_coordinator.rb +93 -0
  141. data/lib/active_support/messages/rotator.rb +38 -31
  142. data/lib/active_support/messages/serializer_with_fallback.rb +158 -0
  143. data/lib/active_support/multibyte/chars.rb +8 -3
  144. data/lib/active_support/multibyte/unicode.rb +9 -37
  145. data/lib/active_support/notifications/fanout.rb +248 -87
  146. data/lib/active_support/notifications/instrumenter.rb +93 -25
  147. data/lib/active_support/notifications.rb +29 -28
  148. data/lib/active_support/number_helper/number_converter.rb +16 -7
  149. data/lib/active_support/number_helper/number_to_currency_converter.rb +6 -6
  150. data/lib/active_support/number_helper/number_to_human_size_converter.rb +3 -3
  151. data/lib/active_support/number_helper/number_to_phone_converter.rb +1 -0
  152. data/lib/active_support/number_helper.rb +379 -318
  153. data/lib/active_support/option_merger.rb +2 -2
  154. data/lib/active_support/ordered_hash.rb +3 -3
  155. data/lib/active_support/ordered_options.rb +67 -15
  156. data/lib/active_support/parameter_filter.rb +84 -69
  157. data/lib/active_support/proxy_object.rb +8 -3
  158. data/lib/active_support/railtie.rb +25 -20
  159. data/lib/active_support/reloader.rb +12 -4
  160. data/lib/active_support/rescuable.rb +2 -0
  161. data/lib/active_support/secure_compare_rotator.rb +16 -9
  162. data/lib/active_support/string_inquirer.rb +4 -2
  163. data/lib/active_support/subscriber.rb +10 -27
  164. data/lib/active_support/syntax_error_proxy.rb +60 -0
  165. data/lib/active_support/tagged_logging.rb +64 -25
  166. data/lib/active_support/test_case.rb +156 -7
  167. data/lib/active_support/testing/assertions.rb +28 -12
  168. data/lib/active_support/testing/autorun.rb +0 -2
  169. data/lib/active_support/testing/constant_stubbing.rb +54 -0
  170. data/lib/active_support/testing/deprecation.rb +20 -27
  171. data/lib/active_support/testing/error_reporter_assertions.rb +107 -0
  172. data/lib/active_support/testing/isolation.rb +21 -9
  173. data/lib/active_support/testing/method_call_assertions.rb +7 -8
  174. data/lib/active_support/testing/parallelization/server.rb +18 -2
  175. data/lib/active_support/testing/parallelization/worker.rb +2 -2
  176. data/lib/active_support/testing/parallelization.rb +12 -1
  177. data/lib/active_support/testing/parallelize_executor.rb +8 -3
  178. data/lib/active_support/testing/setup_and_teardown.rb +2 -0
  179. data/lib/active_support/testing/stream.rb +1 -1
  180. data/lib/active_support/testing/tests_without_assertions.rb +19 -0
  181. data/lib/active_support/testing/time_helpers.rb +38 -16
  182. data/lib/active_support/time_with_zone.rb +12 -18
  183. data/lib/active_support/values/time_zone.rb +25 -14
  184. data/lib/active_support/version.rb +1 -1
  185. data/lib/active_support/xml_mini/jdom.rb +3 -10
  186. data/lib/active_support/xml_mini/nokogiri.rb +1 -1
  187. data/lib/active_support/xml_mini/nokogirisax.rb +1 -1
  188. data/lib/active_support/xml_mini/rexml.rb +1 -1
  189. data/lib/active_support/xml_mini.rb +14 -3
  190. data/lib/active_support.rb +15 -3
  191. metadata +142 -24
  192. data/lib/active_support/core_ext/array/deprecated_conversions.rb +0 -25
  193. data/lib/active_support/core_ext/date/deprecated_conversions.rb +0 -40
  194. data/lib/active_support/core_ext/date_time/deprecated_conversions.rb +0 -36
  195. data/lib/active_support/core_ext/numeric/deprecated_conversions.rb +0 -60
  196. data/lib/active_support/core_ext/range/deprecated_conversions.rb +0 -36
  197. data/lib/active_support/core_ext/range/include_time_with_zone.rb +0 -5
  198. data/lib/active_support/core_ext/range/overlaps.rb +0 -10
  199. data/lib/active_support/core_ext/time/deprecated_conversions.rb +0 -73
  200. data/lib/active_support/core_ext/uri.rb +0 -5
  201. data/lib/active_support/deprecation/instance_delegator.rb +0 -38
  202. data/lib/active_support/per_thread_registry.rb +0 -65
  203. data/lib/active_support/ruby_features.rb +0 -7
@@ -1,9 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActiveSupport
4
+ # = Active Support \Error Reporter
5
+ #
4
6
  # +ActiveSupport::ErrorReporter+ is a common interface for error reporting services.
5
7
  #
6
- # To rescue and report any unhandled error, you can use the +handle+ method:
8
+ # To rescue and report any unhandled error, you can use the #handle method:
7
9
  #
8
10
  # Rails.error.handle do
9
11
  # do_something!
@@ -11,68 +13,151 @@ module ActiveSupport
11
13
  #
12
14
  # If an error is raised, it will be reported and swallowed.
13
15
  #
14
- # Alternatively if you want to report the error but not swallow it, you can use +record+
16
+ # Alternatively, if you want to report the error but not swallow it, you can use #record:
15
17
  #
16
18
  # Rails.error.record do
17
19
  # do_something!
18
20
  # end
19
21
  #
20
- # Both methods can be restricted to only handle a specific exception class
22
+ # Both methods can be restricted to handle only a specific error class:
21
23
  #
22
24
  # maybe_tags = Rails.error.handle(Redis::BaseError) { redis.get("tags") }
23
25
  #
24
- # You can also pass some extra context information that may be used by the error subscribers:
25
- #
26
- # Rails.error.handle(context: { section: "admin" }) do
27
- # # ...
28
- # end
29
- #
30
- # Additionally a +severity+ can be passed along to communicate how important the error report is.
31
- # +severity+ can be one of +:error+, +:warning+, or +:info+. Handled errors default to the +:warning+
32
- # severity, and unhandled ones to +:error+.
33
- #
34
- # Both +handle+ and +record+ pass through the return value from the block. In the case of +handle+
35
- # rescuing an error, a fallback can be provided. The fallback must be a callable whose result will
36
- # be returned when the block raises and is handled:
37
- #
38
- # user = Rails.error.handle(fallback: -> { User.anonymous }) do
39
- # User.find_by(params)
40
- # end
41
26
  class ErrorReporter
42
27
  SEVERITIES = %i(error warning info)
28
+ DEFAULT_SOURCE = "application"
29
+ DEFAULT_RESCUE = [StandardError].freeze
43
30
 
44
- attr_accessor :logger
31
+ attr_accessor :logger, :debug_mode
32
+
33
+ UnexpectedError = Class.new(Exception)
45
34
 
46
35
  def initialize(*subscribers, logger: nil)
47
36
  @subscribers = subscribers.flatten
48
37
  @logger = logger
38
+ @debug_mode = false
49
39
  end
50
40
 
51
- # Report any unhandled exception, and swallow it.
41
+ # Evaluates the given block, reporting and swallowing any unhandled error.
42
+ # If no error is raised, returns the return value of the block. Otherwise,
43
+ # returns the result of +fallback.call+, or +nil+ if +fallback+ is not
44
+ # specified.
52
45
  #
46
+ # # Will report a TypeError to all subscribers and return nil.
53
47
  # Rails.error.handle do
54
48
  # 1 + '1'
55
49
  # end
56
50
  #
57
- def handle(error_class = StandardError, severity: :warning, context: {}, fallback: nil)
51
+ # Can be restricted to handle only specific error classes:
52
+ #
53
+ # maybe_tags = Rails.error.handle(Redis::BaseError) { redis.get("tags") }
54
+ #
55
+ # ==== Options
56
+ #
57
+ # * +:severity+ - This value is passed along to subscribers to indicate how
58
+ # important the error report is. Can be +:error+, +:warning+, or +:info+.
59
+ # Defaults to +:warning+.
60
+ #
61
+ # * +:context+ - Extra information that is passed along to subscribers. For
62
+ # example:
63
+ #
64
+ # Rails.error.handle(context: { section: "admin" }) do
65
+ # # ...
66
+ # end
67
+ #
68
+ # * +:fallback+ - A callable that provides +handle+'s return value when an
69
+ # unhandled error is raised. For example:
70
+ #
71
+ # user = Rails.error.handle(fallback: -> { User.anonymous }) do
72
+ # User.find_by(params)
73
+ # end
74
+ #
75
+ # * +:source+ - This value is passed along to subscribers to indicate the
76
+ # source of the error. Subscribers can use this value to ignore certain
77
+ # errors. Defaults to <tt>"application"</tt>.
78
+ def handle(*error_classes, severity: :warning, context: {}, fallback: nil, source: DEFAULT_SOURCE)
79
+ error_classes = DEFAULT_RESCUE if error_classes.empty?
58
80
  yield
59
- rescue error_class => error
60
- report(error, handled: true, severity: severity, context: context)
81
+ rescue *error_classes => error
82
+ report(error, handled: true, severity: severity, context: context, source: source)
61
83
  fallback.call if fallback
62
84
  end
63
85
 
64
- def record(error_class = StandardError, severity: :error, context: {})
86
+ # Evaluates the given block, reporting and re-raising any unhandled error.
87
+ # If no error is raised, returns the return value of the block.
88
+ #
89
+ # # Will report a TypeError to all subscribers and re-raise it.
90
+ # Rails.error.record do
91
+ # 1 + '1'
92
+ # end
93
+ #
94
+ # Can be restricted to handle only specific error classes:
95
+ #
96
+ # tags = Rails.error.record(Redis::BaseError) { redis.get("tags") }
97
+ #
98
+ # ==== Options
99
+ #
100
+ # * +:severity+ - This value is passed along to subscribers to indicate how
101
+ # important the error report is. Can be +:error+, +:warning+, or +:info+.
102
+ # Defaults to +:error+.
103
+ #
104
+ # * +:context+ - Extra information that is passed along to subscribers. For
105
+ # example:
106
+ #
107
+ # Rails.error.record(context: { section: "admin" }) do
108
+ # # ...
109
+ # end
110
+ #
111
+ # * +:source+ - This value is passed along to subscribers to indicate the
112
+ # source of the error. Subscribers can use this value to ignore certain
113
+ # errors. Defaults to <tt>"application"</tt>.
114
+ def record(*error_classes, severity: :error, context: {}, source: DEFAULT_SOURCE)
115
+ error_classes = DEFAULT_RESCUE if error_classes.empty?
65
116
  yield
66
- rescue error_class => error
67
- report(error, handled: false, severity: severity, context: context)
117
+ rescue *error_classes => error
118
+ report(error, handled: false, severity: severity, context: context, source: source)
68
119
  raise
69
120
  end
70
121
 
122
+ # Either report the given error when in production, or raise it when in development or test.
123
+ #
124
+ # When called in production, after the error is reported, this method will return
125
+ # nil and execution will continue.
126
+ #
127
+ # When called in development, the original error is wrapped in a different error class to ensure
128
+ # it's not being rescued higher in the stack and will be surfaced to the developer.
129
+ #
130
+ # This method is intended for reporting violated assertions about preconditions, or similar
131
+ # cases that can and should be gracefully handled in production, but that aren't supposed to happen.
132
+ #
133
+ # The error can be either an exception instance or a String.
134
+ #
135
+ # example:
136
+ #
137
+ # def edit
138
+ # if published?
139
+ # Rails.error.unexpected("[BUG] Attempting to edit a published article, that shouldn't be possible")
140
+ # return false
141
+ # end
142
+ # # ...
143
+ # end
144
+ #
145
+ def unexpected(error, severity: :warning, context: {}, source: DEFAULT_SOURCE)
146
+ error = RuntimeError.new(error) if error.is_a?(String)
147
+ error.set_backtrace(caller(1)) if error.backtrace.nil?
148
+
149
+ if @debug_mode
150
+ raise UnexpectedError, "#{error.class.name}: #{error.message}", error.backtrace, cause: error
151
+ else
152
+ report(error, handled: true, severity: severity, context: context, source: source)
153
+ end
154
+ end
155
+
71
156
  # Register a new error subscriber. The subscriber must respond to
72
157
  #
73
- # report(Exception, handled: Boolean, context: Hash)
158
+ # report(Exception, handled: Boolean, severity: (:error OR :warning OR :info), context: Hash, source: String)
74
159
  #
75
- # The +report+ method +should+ never raise an error.
160
+ # The +report+ method <b>should never</b> raise an error.
76
161
  def subscribe(subscriber)
77
162
  unless subscriber.respond_to?(:report)
78
163
  raise ArgumentError, "Error subscribers must respond to #report"
@@ -80,26 +165,61 @@ module ActiveSupport
80
165
  @subscribers << subscriber
81
166
  end
82
167
 
83
- # Update the execution context that is accessible to error subscribers
168
+ # Unregister an error subscriber. Accepts either a subscriber or a class.
169
+ #
170
+ # subscriber = MyErrorSubscriber.new
171
+ # Rails.error.subscribe(subscriber)
172
+ #
173
+ # Rails.error.unsubscribe(subscriber)
174
+ # # or
175
+ # Rails.error.unsubscribe(MyErrorSubscriber)
176
+ def unsubscribe(subscriber)
177
+ @subscribers.delete_if { |s| subscriber === s }
178
+ end
179
+
180
+ # Prevent a subscriber from being notified of errors for the
181
+ # duration of the block. You may pass in the subscriber itself, or its class.
182
+ #
183
+ # This can be helpful for error reporting service integrations, when they wish
184
+ # to handle any errors higher in the stack.
185
+ def disable(subscriber)
186
+ disabled_subscribers = (ActiveSupport::IsolatedExecutionState[self] ||= [])
187
+ disabled_subscribers << subscriber
188
+ begin
189
+ yield
190
+ ensure
191
+ disabled_subscribers.delete(subscriber)
192
+ end
193
+ end
194
+
195
+ # Update the execution context that is accessible to error subscribers. Any
196
+ # context passed to #handle, #record, or #report will be merged with the
197
+ # context set here.
84
198
  #
85
199
  # Rails.error.set_context(section: "checkout", user_id: @user.id)
86
200
  #
87
- # See +ActiveSupport::ExecutionContext.set+
88
201
  def set_context(...)
89
202
  ActiveSupport::ExecutionContext.set(...)
90
203
  end
91
204
 
92
- # When the block based +handle+ and +record+ methods are not suitable, you can directly use +report+
205
+ # Report an error directly to subscribers. You can use this method when the
206
+ # block-based #handle and #record methods are not suitable.
93
207
  #
94
- # Rails.error.report(error, handled: true)
95
- def report(error, handled:, severity: handled ? :warning : :error, context: {})
208
+ # Rails.error.report(error)
209
+ #
210
+ def report(error, handled: true, severity: handled ? :warning : :error, context: {}, source: DEFAULT_SOURCE)
211
+ return if error.instance_variable_defined?(:@__rails_error_reported)
212
+
96
213
  unless SEVERITIES.include?(severity)
97
214
  raise ArgumentError, "severity must be one of #{SEVERITIES.map(&:inspect).join(", ")}, got: #{severity.inspect}"
98
215
  end
99
216
 
100
217
  full_context = ActiveSupport::ExecutionContext.to_h.merge(context)
218
+ disabled_subscribers = ActiveSupport::IsolatedExecutionState[self]
101
219
  @subscribers.each do |subscriber|
102
- subscriber.report(error, handled: handled, severity: severity, context: full_context)
220
+ unless disabled_subscribers&.any? { |s| s === subscriber }
221
+ subscriber.report(error, handled: handled, severity: severity, context: full_context, source: source)
222
+ end
103
223
  rescue => subscriber_error
104
224
  if logger
105
225
  logger.fatal(
@@ -111,6 +231,13 @@ module ActiveSupport
111
231
  end
112
232
  end
113
233
 
234
+ while error
235
+ unless error.frozen?
236
+ error.instance_variable_set(:@__rails_error_reported, true)
237
+ end
238
+ error = error.cause
239
+ end
240
+
114
241
  nil
115
242
  end
116
243
  end
@@ -6,7 +6,6 @@ require "listen"
6
6
  require "set"
7
7
  require "pathname"
8
8
  require "concurrent/atomic/atomic_boolean"
9
- require "active_support/fork_tracker"
10
9
 
11
10
  module ActiveSupport
12
11
  # Allows you to "listen" to changes in a file system.
@@ -2,7 +2,6 @@
2
2
 
3
3
  require "active_support/error_reporter"
4
4
  require "active_support/callbacks"
5
- require "concurrent/hash"
6
5
 
7
6
  module ActiveSupport
8
7
  class ExecutionWrapper
@@ -84,14 +83,14 @@ module ActiveSupport
84
83
  end
85
84
 
86
85
  # Perform the work in the supplied block as an execution.
87
- def self.wrap
86
+ def self.wrap(source: "application.active_support")
88
87
  return yield if active?
89
88
 
90
89
  instance = run!
91
90
  begin
92
91
  yield
93
- rescue => error
94
- error_reporter.report(error, handled: false)
92
+ rescue Exception => error
93
+ error_reporter&.report(error, handled: false, source: source)
95
94
  raise
96
95
  ensure
97
96
  instance.complete!
@@ -108,8 +107,8 @@ module ActiveSupport
108
107
  end
109
108
  end
110
109
 
111
- def self.error_reporter
112
- @error_reporter ||= ActiveSupport::ErrorReporter.new
110
+ def self.error_reporter # :nodoc:
111
+ ActiveSupport.error_reporter
113
112
  end
114
113
 
115
114
  def self.active_key # :nodoc:
@@ -3,7 +3,9 @@
3
3
  require "active_support/core_ext/time/calculations"
4
4
 
5
5
  module ActiveSupport
6
- # FileUpdateChecker specifies the API used by Rails to watch files
6
+ # = \File Update Checker
7
+ #
8
+ # FileUpdateChecker specifies the API used by \Rails to watch files
7
9
  # and control reloading. The API depends on four methods:
8
10
  #
9
11
  # * +initialize+ which expects two parameters and one block as
@@ -20,7 +22,7 @@ module ActiveSupport
20
22
  # After initialization, a call to +execute_if_updated+ must execute
21
23
  # the block only if there was really a change in the filesystem.
22
24
  #
23
- # This class is used by Rails to reload the I18n framework whenever
25
+ # This class is used by \Rails to reload the I18n framework whenever
24
26
  # they are changed upon a new request.
25
27
  #
26
28
  # i18n_reloader = ActiveSupport::FileUpdateChecker.new(paths) do
@@ -102,7 +104,7 @@ module ActiveSupport
102
104
  @watched || begin
103
105
  all = @files.select { |f| File.exist?(f) }
104
106
  all.concat(Dir[@glob]) if @glob
105
- all
107
+ all.tap(&:uniq!)
106
108
  end
107
109
  end
108
110
 
@@ -118,7 +120,7 @@ module ActiveSupport
118
120
  # healthy to consider this edge case because with mtimes in the future
119
121
  # reloading is not triggered.
120
122
  def max_mtime(paths)
121
- time_now = Time.now
123
+ time_now = Time.at(0, Process.clock_gettime(Process::CLOCK_REALTIME, :nanosecond), :nanosecond)
122
124
  max_mtime = nil
123
125
 
124
126
  # Time comparisons are performed with #compare_without_coercion because
@@ -2,42 +2,21 @@
2
2
 
3
3
  module ActiveSupport
4
4
  module ForkTracker # :nodoc:
5
- module ModernCoreExt
5
+ module CoreExt
6
6
  def _fork
7
7
  pid = super
8
8
  if pid == 0
9
- ForkTracker.check!
9
+ ForkTracker.after_fork_callback
10
10
  end
11
11
  pid
12
12
  end
13
13
  end
14
14
 
15
- module CoreExt
16
- def fork(...)
17
- if block_given?
18
- super do
19
- ForkTracker.check!
20
- yield
21
- end
22
- else
23
- unless pid = super
24
- ForkTracker.check!
25
- end
26
- pid
27
- end
28
- end
29
- end
30
-
31
- module CoreExtPrivate
32
- include CoreExt
33
- private :fork
34
- end
35
-
36
15
  @pid = Process.pid
37
16
  @callbacks = []
38
17
 
39
18
  class << self
40
- def check!
19
+ def after_fork_callback
41
20
  new_pid = Process.pid
42
21
  if @pid != new_pid
43
22
  @callbacks.each(&:call)
@@ -46,14 +25,7 @@ module ActiveSupport
46
25
  end
47
26
 
48
27
  def hook!
49
- if Process.respond_to?(:_fork) # Ruby 3.1+
50
- ::Process.singleton_class.prepend(ModernCoreExt)
51
- elsif Process.respond_to?(:fork)
52
- ::Object.prepend(CoreExtPrivate) if RUBY_VERSION < "3.0"
53
- ::Kernel.prepend(CoreExtPrivate)
54
- ::Kernel.singleton_class.prepend(CoreExt)
55
- ::Process.singleton_class.prepend(CoreExt)
56
- end
28
+ ::Process.singleton_class.prepend(CoreExt)
57
29
  end
58
30
 
59
31
  def after_fork(&block)
@@ -1,16 +1,16 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActiveSupport
4
- # Returns the currently loaded version of Active Support as a <tt>Gem::Version</tt>.
4
+ # Returns the currently loaded version of Active Support as a +Gem::Version+.
5
5
  def self.gem_version
6
6
  Gem::Version.new VERSION::STRING
7
7
  end
8
8
 
9
9
  module VERSION
10
10
  MAJOR = 7
11
- MINOR = 0
12
- TINY = 8
13
- PRE = "7"
11
+ MINOR = 2
12
+ TINY = 3
13
+ PRE = nil
14
14
 
15
15
  STRING = [MAJOR, MINOR, TINY, PRE].compact.join(".")
16
16
  end
@@ -4,6 +4,8 @@ require "zlib"
4
4
  require "stringio"
5
5
 
6
6
  module ActiveSupport
7
+ # = Active Support \Gzip
8
+ #
7
9
  # A convenient wrapper for the zlib standard library that allows
8
10
  # compression/decompression of strings with gzip.
9
11
  #
@@ -6,6 +6,8 @@ require "active_support/core_ext/hash/except"
6
6
  require "active_support/core_ext/hash/slice"
7
7
 
8
8
  module ActiveSupport
9
+ # = \Hash With Indifferent Access
10
+ #
9
11
  # Implements a hash where keys <tt>:foo</tt> and <tt>"foo"</tt> are considered
10
12
  # to be the same.
11
13
  #
@@ -37,7 +39,7 @@ module ActiveSupport
37
39
  #
38
40
  # but this class is intended for use cases where strings or symbols are the
39
41
  # expected keys and it is convenient to understand both as the same. For
40
- # example the +params+ hash in Ruby on Rails.
42
+ # example the +params+ hash in Ruby on \Rails.
41
43
  #
42
44
  # Note that core extensions define <tt>Hash#with_indifferent_access</tt>:
43
45
  #
@@ -45,7 +47,7 @@ module ActiveSupport
45
47
  #
46
48
  # which may be handy.
47
49
  #
48
- # To access this class outside of Rails, require the core extension with:
50
+ # To access this class outside of \Rails, require the core extension with:
49
51
  #
50
52
  # require "active_support/core_ext/hash/indifferent_access"
51
53
  #
@@ -113,7 +115,7 @@ module ActiveSupport
113
115
  # hash.update({ "a" => 1 }, { "b" => 2 }) # => { "a" => 1, "b" => 2 }
114
116
  #
115
117
  # The arguments can be either an
116
- # <tt>ActiveSupport::HashWithIndifferentAccess</tt> or a regular +Hash+.
118
+ # +ActiveSupport::HashWithIndifferentAccess+ or a regular +Hash+.
117
119
  # In either case the merge respects the semantics of indifferent access.
118
120
  #
119
121
  # If the argument is a regular hash with keys +:key+ and <tt>"key"</tt> only one
@@ -218,8 +220,12 @@ module ActiveSupport
218
220
  # hash.default # => nil
219
221
  # hash.default('foo') # => 'foo'
220
222
  # hash.default(:foo) # => 'foo'
221
- def default(*args)
222
- super(*args.map { |arg| convert_key(arg) })
223
+ def default(key = (no_key = true))
224
+ if no_key
225
+ super()
226
+ else
227
+ super(convert_key(key))
228
+ end
223
229
  end
224
230
 
225
231
  # Returns an array of the values at the specified indices:
@@ -229,7 +235,8 @@ module ActiveSupport
229
235
  # hash[:b] = 'y'
230
236
  # hash.values_at('a', 'b') # => ["x", "y"]
231
237
  def values_at(*keys)
232
- super(*keys.map { |key| convert_key(key) })
238
+ keys.map! { |key| convert_key(key) }
239
+ super
233
240
  end
234
241
 
235
242
  # Returns an array of the values at the specified indices, but also
@@ -242,7 +249,8 @@ module ActiveSupport
242
249
  # hash.fetch_values('a', 'c') { |key| 'z' } # => ["x", "z"]
243
250
  # hash.fetch_values('a', 'c') # => KeyError: key not found: "c"
244
251
  def fetch_values(*indices, &block)
245
- super(*indices.map { |key| convert_key(key) }, &block)
252
+ indices.map! { |key| convert_key(key) }
253
+ super
246
254
  end
247
255
 
248
256
  # Returns a shallow copy of the hash.
@@ -254,9 +262,7 @@ module ActiveSupport
254
262
  # hash[:a][:c] # => "c"
255
263
  # dup[:a][:c] # => "c"
256
264
  def dup
257
- self.class.new(self).tap do |new_hash|
258
- set_defaults(new_hash)
259
- end
265
+ copy_defaults(self.class.new(self))
260
266
  end
261
267
 
262
268
  # This method has the same semantics of +update+, except it does not
@@ -301,7 +307,7 @@ module ActiveSupport
301
307
  # hash.except(:a, "b") # => {c: 10}.with_indifferent_access
302
308
  # hash # => { a: "x", b: "y", c: 10 }.with_indifferent_access
303
309
  def except(*keys)
304
- slice(*self.keys - keys.map { |key| convert_key(key) })
310
+ dup.except!(*keys)
305
311
  end
306
312
  alias_method :without, :except
307
313
 
@@ -326,21 +332,36 @@ module ActiveSupport
326
332
  dup.tap { |hash| hash.reject!(*args, &block) }
327
333
  end
328
334
 
329
- def transform_values(*args, &block)
335
+ def transform_values(&block)
330
336
  return to_enum(:transform_values) unless block_given?
331
- dup.tap { |hash| hash.transform_values!(*args, &block) }
337
+ dup.tap { |hash| hash.transform_values!(&block) }
332
338
  end
333
339
 
334
- def transform_keys(*args, &block)
335
- return to_enum(:transform_keys) unless block_given?
336
- dup.tap { |hash| hash.transform_keys!(*args, &block) }
340
+ NOT_GIVEN = Object.new # :nodoc:
341
+
342
+ def transform_keys(hash = NOT_GIVEN, &block)
343
+ if NOT_GIVEN.equal?(hash)
344
+ if block_given?
345
+ self.class.new(super(&block))
346
+ else
347
+ to_enum(:transform_keys)
348
+ end
349
+ else
350
+ self.class.new(super)
351
+ end
337
352
  end
338
353
 
339
- def transform_keys!
340
- return enum_for(:transform_keys!) { size } unless block_given?
341
- keys.each do |key|
342
- self[yield(key)] = delete(key)
354
+ def transform_keys!(hash = NOT_GIVEN, &block)
355
+ if NOT_GIVEN.equal?(hash)
356
+ if block_given?
357
+ replace(copy_defaults(transform_keys(&block)))
358
+ else
359
+ return to_enum(:transform_keys!)
360
+ end
361
+ else
362
+ replace(copy_defaults(transform_keys(hash, &block)))
343
363
  end
364
+
344
365
  self
345
366
  end
346
367
 
@@ -361,7 +382,7 @@ module ActiveSupport
361
382
  # Convert to a regular hash with string keys.
362
383
  def to_hash
363
384
  _new_hash = Hash.new
364
- set_defaults(_new_hash)
385
+ copy_defaults(_new_hash)
365
386
 
366
387
  each do |key, value|
367
388
  _new_hash[key] = convert_value(value, conversion: :to_hash)
@@ -369,15 +390,13 @@ module ActiveSupport
369
390
  _new_hash
370
391
  end
371
392
 
393
+ def to_proc
394
+ proc { |key| self[key] }
395
+ end
396
+
372
397
  private
373
- if Symbol.method_defined?(:name)
374
- def convert_key(key)
375
- key.kind_of?(Symbol) ? key.name : key
376
- end
377
- else
378
- def convert_key(key)
379
- key.kind_of?(Symbol) ? key.to_s : key
380
- end
398
+ def convert_key(key)
399
+ Symbol === key ? key.name : key
381
400
  end
382
401
 
383
402
  def convert_value(value, conversion: nil)
@@ -397,12 +416,13 @@ module ActiveSupport
397
416
  end
398
417
  end
399
418
 
400
- def set_defaults(target)
419
+ def copy_defaults(target)
401
420
  if default_proc
402
421
  target.default_proc = default_proc.dup
403
422
  else
404
423
  target.default = default
405
424
  end
425
+ target
406
426
  end
407
427
 
408
428
  def update_with_single_argument(other_hash, block)
@@ -7,18 +7,31 @@ module ActiveSupport
7
7
  def translate(key, **options)
8
8
  if html_safe_translation_key?(key)
9
9
  html_safe_options = html_escape_translation_options(options)
10
- translation = I18n.translate(key, **html_safe_options)
11
- html_safe_translation(translation)
10
+
11
+ exception = false
12
+
13
+ exception_handler = ->(*args) do
14
+ exception = true
15
+ I18n.exception_handler.call(*args)
16
+ end
17
+
18
+ translation = I18n.translate(key, **html_safe_options, exception_handler: exception_handler)
19
+
20
+ if exception
21
+ translation
22
+ else
23
+ html_safe_translation(translation)
24
+ end
12
25
  else
13
26
  I18n.translate(key, **options)
14
27
  end
15
28
  end
16
29
 
17
- private
18
- def html_safe_translation_key?(key)
19
- /(?:_|\b)html\z/.match?(key)
20
- end
30
+ def html_safe_translation_key?(key)
31
+ /(?:_|\b)html\z/.match?(key)
32
+ end
21
33
 
34
+ private
22
35
  def html_escape_translation_options(options)
23
36
  options.each do |name, value|
24
37
  unless i18n_option?(name) || (name == :count && value.is_a?(Numeric))
@@ -7,7 +7,7 @@ begin
7
7
  require "i18n"
8
8
  require "i18n/backend/fallbacks"
9
9
  rescue LoadError => e
10
- $stderr.puts "The i18n gem is not available. Please add it to your Gemfile and run bundle install"
10
+ warn "The i18n gem is not available. Please add it to your Gemfile and run bundle install"
11
11
  raise e
12
12
  end
13
13
  require "active_support/lazy_load_hooks"