activesupport 4.0.12 → 7.0.2.4

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 (295) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +249 -501
  3. data/MIT-LICENSE +2 -2
  4. data/README.rdoc +10 -5
  5. data/lib/active_support/actionable_error.rb +48 -0
  6. data/lib/active_support/all.rb +5 -3
  7. data/lib/active_support/array_inquirer.rb +48 -0
  8. data/lib/active_support/backtrace_cleaner.rb +41 -13
  9. data/lib/active_support/benchmarkable.rb +7 -15
  10. data/lib/active_support/builder.rb +3 -1
  11. data/lib/active_support/cache/file_store.rb +96 -74
  12. data/lib/active_support/cache/mem_cache_store.rb +211 -103
  13. data/lib/active_support/cache/memory_store.rb +90 -58
  14. data/lib/active_support/cache/null_store.rb +19 -7
  15. data/lib/active_support/cache/redis_cache_store.rb +468 -0
  16. data/lib/active_support/cache/strategy/local_cache.rb +86 -83
  17. data/lib/active_support/cache/strategy/local_cache_middleware.rb +45 -0
  18. data/lib/active_support/cache.rb +580 -241
  19. data/lib/active_support/callbacks.rb +812 -425
  20. data/lib/active_support/code_generator.rb +65 -0
  21. data/lib/active_support/concern.rb +103 -14
  22. data/lib/active_support/concurrency/load_interlock_aware_monitor.rb +33 -0
  23. data/lib/active_support/concurrency/share_lock.rb +226 -0
  24. data/lib/active_support/configurable.rb +21 -19
  25. data/lib/active_support/configuration_file.rb +51 -0
  26. data/lib/active_support/core_ext/array/access.rb +47 -1
  27. data/lib/active_support/core_ext/array/conversions.rb +35 -44
  28. data/lib/active_support/core_ext/array/deprecated_conversions.rb +25 -0
  29. data/lib/active_support/core_ext/array/extract.rb +21 -0
  30. data/lib/active_support/core_ext/array/extract_options.rb +2 -0
  31. data/lib/active_support/core_ext/array/grouping.rb +26 -16
  32. data/lib/active_support/core_ext/array/inquiry.rb +19 -0
  33. data/lib/active_support/core_ext/array/wrap.rb +7 -4
  34. data/lib/active_support/core_ext/array.rb +10 -7
  35. data/lib/active_support/core_ext/benchmark.rb +5 -3
  36. data/lib/active_support/core_ext/big_decimal/conversions.rb +9 -26
  37. data/lib/active_support/core_ext/big_decimal.rb +3 -1
  38. data/lib/active_support/core_ext/class/attribute.rb +52 -49
  39. data/lib/active_support/core_ext/class/attribute_accessors.rb +5 -169
  40. data/lib/active_support/core_ext/class/subclasses.rb +25 -26
  41. data/lib/active_support/core_ext/class.rb +4 -4
  42. data/lib/active_support/core_ext/date/acts_like.rb +3 -1
  43. data/lib/active_support/core_ext/date/blank.rb +14 -0
  44. data/lib/active_support/core_ext/date/calculations.rb +31 -18
  45. data/lib/active_support/core_ext/date/conversions.rb +43 -32
  46. data/lib/active_support/core_ext/date/deprecated_conversions.rb +26 -0
  47. data/lib/active_support/core_ext/date/zones.rb +5 -34
  48. data/lib/active_support/core_ext/date.rb +7 -4
  49. data/lib/active_support/core_ext/date_and_time/calculations.rb +198 -66
  50. data/lib/active_support/core_ext/date_and_time/compatibility.rb +31 -0
  51. data/lib/active_support/core_ext/date_and_time/zones.rb +40 -0
  52. data/lib/active_support/core_ext/date_time/acts_like.rb +4 -2
  53. data/lib/active_support/core_ext/date_time/blank.rb +14 -0
  54. data/lib/active_support/core_ext/date_time/calculations.rb +79 -38
  55. data/lib/active_support/core_ext/date_time/compatibility.rb +18 -0
  56. data/lib/active_support/core_ext/date_time/conversions.rb +31 -26
  57. data/lib/active_support/core_ext/date_time/deprecated_conversions.rb +22 -0
  58. data/lib/active_support/core_ext/date_time.rb +8 -4
  59. data/lib/active_support/core_ext/digest/uuid.rb +79 -0
  60. data/lib/active_support/core_ext/digest.rb +3 -0
  61. data/lib/active_support/core_ext/enumerable.rb +249 -17
  62. data/lib/active_support/core_ext/file/atomic.rb +41 -32
  63. data/lib/active_support/core_ext/file.rb +3 -1
  64. data/lib/active_support/core_ext/hash/conversions.rb +71 -49
  65. data/lib/active_support/core_ext/hash/deep_merge.rb +9 -13
  66. data/lib/active_support/core_ext/hash/deep_transform_values.rb +46 -0
  67. data/lib/active_support/core_ext/hash/except.rb +14 -5
  68. data/lib/active_support/core_ext/hash/indifferent_access.rb +5 -3
  69. data/lib/active_support/core_ext/hash/keys.rb +39 -56
  70. data/lib/active_support/core_ext/hash/reverse_merge.rb +5 -2
  71. data/lib/active_support/core_ext/hash/slice.rb +8 -23
  72. data/lib/active_support/core_ext/hash.rb +10 -8
  73. data/lib/active_support/core_ext/integer/inflections.rb +3 -1
  74. data/lib/active_support/core_ext/integer/multiple.rb +3 -1
  75. data/lib/active_support/core_ext/integer/time.rb +11 -33
  76. data/lib/active_support/core_ext/integer.rb +5 -3
  77. data/lib/active_support/core_ext/kernel/concern.rb +14 -0
  78. data/lib/active_support/core_ext/kernel/reporting.rb +9 -78
  79. data/lib/active_support/core_ext/kernel/singleton_class.rb +2 -0
  80. data/lib/active_support/core_ext/kernel.rb +5 -4
  81. data/lib/active_support/core_ext/load_error.rb +5 -21
  82. data/lib/active_support/core_ext/module/aliasing.rb +6 -44
  83. data/lib/active_support/core_ext/module/anonymous.rb +12 -1
  84. data/lib/active_support/core_ext/module/attr_internal.rb +8 -8
  85. data/lib/active_support/core_ext/module/attribute_accessors.rb +186 -44
  86. data/lib/active_support/core_ext/module/attribute_accessors_per_thread.rb +157 -0
  87. data/lib/active_support/core_ext/module/concerning.rb +140 -0
  88. data/lib/active_support/core_ext/module/delegation.rb +172 -45
  89. data/lib/active_support/core_ext/module/deprecation.rb +3 -3
  90. data/lib/active_support/core_ext/module/introspection.rb +23 -38
  91. data/lib/active_support/core_ext/module/redefine_method.rb +40 -0
  92. data/lib/active_support/core_ext/module/remove_method.rb +8 -3
  93. data/lib/active_support/core_ext/module.rb +13 -10
  94. data/lib/active_support/core_ext/name_error.rb +45 -4
  95. data/lib/active_support/core_ext/numeric/bytes.rb +22 -0
  96. data/lib/active_support/core_ext/numeric/conversions.rb +135 -127
  97. data/lib/active_support/core_ext/numeric/deprecated_conversions.rb +60 -0
  98. data/lib/active_support/core_ext/numeric/time.rb +37 -50
  99. data/lib/active_support/core_ext/numeric.rb +6 -3
  100. data/lib/active_support/core_ext/object/acts_like.rb +41 -6
  101. data/lib/active_support/core_ext/object/blank.rb +70 -20
  102. data/lib/active_support/core_ext/object/conversions.rb +6 -4
  103. data/lib/active_support/core_ext/object/deep_dup.rb +19 -10
  104. data/lib/active_support/core_ext/object/duplicable.rb +17 -47
  105. data/lib/active_support/core_ext/object/inclusion.rb +18 -15
  106. data/lib/active_support/core_ext/object/instance_variables.rb +3 -1
  107. data/lib/active_support/core_ext/object/json.rb +244 -0
  108. data/lib/active_support/core_ext/object/to_param.rb +3 -1
  109. data/lib/active_support/core_ext/object/to_query.rb +21 -8
  110. data/lib/active_support/core_ext/object/try.rb +106 -26
  111. data/lib/active_support/core_ext/object/with_options.rb +64 -5
  112. data/lib/active_support/core_ext/object.rb +14 -12
  113. data/lib/active_support/core_ext/pathname/existence.rb +21 -0
  114. data/lib/active_support/core_ext/pathname.rb +3 -0
  115. data/lib/active_support/core_ext/range/compare_range.rb +57 -0
  116. data/lib/active_support/core_ext/range/conversions.rb +37 -15
  117. data/lib/active_support/core_ext/range/deprecated_conversions.rb +26 -0
  118. data/lib/active_support/core_ext/range/each.rb +18 -17
  119. data/lib/active_support/core_ext/range/include_time_with_zone.rb +7 -0
  120. data/lib/active_support/core_ext/range/overlaps.rb +2 -0
  121. data/lib/active_support/core_ext/range.rb +7 -4
  122. data/lib/active_support/core_ext/regexp.rb +10 -1
  123. data/lib/active_support/core_ext/securerandom.rb +45 -0
  124. data/lib/active_support/core_ext/string/access.rb +42 -51
  125. data/lib/active_support/core_ext/string/behavior.rb +3 -1
  126. data/lib/active_support/core_ext/string/conversions.rb +18 -13
  127. data/lib/active_support/core_ext/string/exclude.rb +5 -3
  128. data/lib/active_support/core_ext/string/filters.rb +97 -7
  129. data/lib/active_support/core_ext/string/indent.rb +6 -4
  130. data/lib/active_support/core_ext/string/inflections.rb +106 -25
  131. data/lib/active_support/core_ext/string/inquiry.rb +4 -1
  132. data/lib/active_support/core_ext/string/multibyte.rb +18 -9
  133. data/lib/active_support/core_ext/string/output_safety.rb +227 -54
  134. data/lib/active_support/core_ext/string/starts_ends_with.rb +4 -2
  135. data/lib/active_support/core_ext/string/strip.rb +6 -5
  136. data/lib/active_support/core_ext/string/zones.rb +4 -1
  137. data/lib/active_support/core_ext/string.rb +15 -13
  138. data/lib/active_support/core_ext/symbol/starts_ends_with.rb +6 -0
  139. data/lib/active_support/core_ext/symbol.rb +3 -0
  140. data/lib/active_support/core_ext/time/acts_like.rb +3 -1
  141. data/lib/active_support/core_ext/time/calculations.rb +178 -116
  142. data/lib/active_support/core_ext/time/compatibility.rb +16 -0
  143. data/lib/active_support/core_ext/time/conversions.rb +37 -25
  144. data/lib/active_support/core_ext/time/deprecated_conversions.rb +22 -0
  145. data/lib/active_support/core_ext/time/zones.rb +44 -42
  146. data/lib/active_support/core_ext/time.rb +8 -5
  147. data/lib/active_support/core_ext/uri.rb +4 -25
  148. data/lib/active_support/core_ext.rb +4 -2
  149. data/lib/active_support/current_attributes/test_helper.rb +13 -0
  150. data/lib/active_support/current_attributes.rb +226 -0
  151. data/lib/active_support/dependencies/autoload.rb +3 -1
  152. data/lib/active_support/dependencies/interlock.rb +49 -0
  153. data/lib/active_support/dependencies/require_dependency.rb +28 -0
  154. data/lib/active_support/dependencies.rb +71 -696
  155. data/lib/active_support/deprecation/behaviors.rb +65 -16
  156. data/lib/active_support/deprecation/constant_accessor.rb +52 -0
  157. data/lib/active_support/deprecation/disallowed.rb +56 -0
  158. data/lib/active_support/deprecation/instance_delegator.rb +16 -2
  159. data/lib/active_support/deprecation/method_wrappers.rb +62 -21
  160. data/lib/active_support/deprecation/proxy_wrappers.rb +82 -31
  161. data/lib/active_support/deprecation/reporting.rb +81 -18
  162. data/lib/active_support/deprecation.rb +19 -11
  163. data/lib/active_support/descendants_tracker.rb +192 -34
  164. data/lib/active_support/digest.rb +22 -0
  165. data/lib/active_support/duration/iso8601_parser.rb +123 -0
  166. data/lib/active_support/duration/iso8601_serializer.rb +67 -0
  167. data/lib/active_support/duration.rb +437 -39
  168. data/lib/active_support/encrypted_configuration.rb +56 -0
  169. data/lib/active_support/encrypted_file.rb +117 -0
  170. data/lib/active_support/environment_inquirer.rb +20 -0
  171. data/lib/active_support/error_reporter.rb +117 -0
  172. data/lib/active_support/evented_file_update_checker.rb +170 -0
  173. data/lib/active_support/execution_context/test_helper.rb +13 -0
  174. data/lib/active_support/execution_context.rb +53 -0
  175. data/lib/active_support/execution_wrapper.rb +151 -0
  176. data/lib/active_support/executor/test_helper.rb +7 -0
  177. data/lib/active_support/executor.rb +8 -0
  178. data/lib/active_support/file_update_checker.rb +62 -37
  179. data/lib/active_support/fork_tracker.rb +71 -0
  180. data/lib/active_support/gem_version.rb +17 -0
  181. data/lib/active_support/gzip.rb +7 -5
  182. data/lib/active_support/hash_with_indifferent_access.rb +207 -54
  183. data/lib/active_support/html_safe_translation.rb +43 -0
  184. data/lib/active_support/i18n.rb +10 -6
  185. data/lib/active_support/i18n_railtie.rb +48 -19
  186. data/lib/active_support/inflections.rb +19 -12
  187. data/lib/active_support/inflector/inflections.rb +97 -37
  188. data/lib/active_support/inflector/methods.rb +192 -157
  189. data/lib/active_support/inflector/transliterate.rb +83 -33
  190. data/lib/active_support/inflector.rb +7 -5
  191. data/lib/active_support/isolated_execution_state.rb +64 -0
  192. data/lib/active_support/json/decoding.rb +37 -42
  193. data/lib/active_support/json/encoding.rb +93 -293
  194. data/lib/active_support/json.rb +4 -2
  195. data/lib/active_support/key_generator.rb +30 -47
  196. data/lib/active_support/lazy_load_hooks.rb +54 -21
  197. data/lib/active_support/locale/en.rb +33 -0
  198. data/lib/active_support/locale/en.yml +10 -4
  199. data/lib/active_support/log_subscriber/test_helper.rb +14 -12
  200. data/lib/active_support/log_subscriber.rb +61 -18
  201. data/lib/active_support/logger.rb +40 -4
  202. data/lib/active_support/logger_silence.rb +17 -20
  203. data/lib/active_support/logger_thread_safe_level.rb +69 -0
  204. data/lib/active_support/message_encryptor.rb +178 -55
  205. data/lib/active_support/message_verifier.rb +195 -26
  206. data/lib/active_support/messages/metadata.rb +80 -0
  207. data/lib/active_support/messages/rotation_configuration.rb +23 -0
  208. data/lib/active_support/messages/rotator.rb +57 -0
  209. data/lib/active_support/multibyte/chars.rb +45 -92
  210. data/lib/active_support/multibyte/unicode.rb +44 -377
  211. data/lib/active_support/multibyte.rb +5 -3
  212. data/lib/active_support/notifications/fanout.rb +177 -44
  213. data/lib/active_support/notifications/instrumenter.rb +117 -17
  214. data/lib/active_support/notifications.rb +106 -39
  215. data/lib/active_support/number_helper/number_converter.rb +181 -0
  216. data/lib/active_support/number_helper/number_to_currency_converter.rb +46 -0
  217. data/lib/active_support/number_helper/number_to_delimited_converter.rb +30 -0
  218. data/lib/active_support/number_helper/number_to_human_converter.rb +69 -0
  219. data/lib/active_support/number_helper/number_to_human_size_converter.rb +60 -0
  220. data/lib/active_support/number_helper/number_to_percentage_converter.rb +16 -0
  221. data/lib/active_support/number_helper/number_to_phone_converter.rb +59 -0
  222. data/lib/active_support/number_helper/number_to_rounded_converter.rb +59 -0
  223. data/lib/active_support/number_helper/rounding_helper.rb +46 -0
  224. data/lib/active_support/number_helper.rb +152 -394
  225. data/lib/active_support/option_merger.rb +18 -5
  226. data/lib/active_support/ordered_hash.rb +8 -6
  227. data/lib/active_support/ordered_options.rb +43 -7
  228. data/lib/active_support/parameter_filter.rb +138 -0
  229. data/lib/active_support/per_thread_registry.rb +24 -11
  230. data/lib/active_support/proxy_object.rb +2 -0
  231. data/lib/active_support/rails.rb +10 -11
  232. data/lib/active_support/railtie.rb +118 -12
  233. data/lib/active_support/reloader.rb +130 -0
  234. data/lib/active_support/rescuable.rb +112 -57
  235. data/lib/active_support/ruby_features.rb +7 -0
  236. data/lib/active_support/secure_compare_rotator.rb +51 -0
  237. data/lib/active_support/security_utils.rb +38 -0
  238. data/lib/active_support/string_inquirer.rb +11 -4
  239. data/lib/active_support/subscriber.rb +109 -39
  240. data/lib/active_support/tagged_logging.rb +54 -17
  241. data/lib/active_support/test_case.rb +121 -37
  242. data/lib/active_support/testing/assertions.rb +177 -39
  243. data/lib/active_support/testing/autorun.rb +5 -3
  244. data/lib/active_support/testing/constant_lookup.rb +3 -6
  245. data/lib/active_support/testing/declarative.rb +10 -22
  246. data/lib/active_support/testing/deprecation.rb +65 -11
  247. data/lib/active_support/testing/file_fixtures.rb +38 -0
  248. data/lib/active_support/testing/isolation.rb +56 -87
  249. data/lib/active_support/testing/method_call_assertions.rb +70 -0
  250. data/lib/active_support/testing/parallelization/server.rb +82 -0
  251. data/lib/active_support/testing/parallelization/worker.rb +103 -0
  252. data/lib/active_support/testing/parallelization.rb +55 -0
  253. data/lib/active_support/testing/parallelize_executor.rb +76 -0
  254. data/lib/active_support/testing/setup_and_teardown.rb +30 -10
  255. data/lib/active_support/testing/stream.rb +41 -0
  256. data/lib/active_support/testing/tagged_logging.rb +6 -4
  257. data/lib/active_support/testing/time_helpers.rb +246 -0
  258. data/lib/active_support/time.rb +13 -13
  259. data/lib/active_support/time_with_zone.rb +315 -90
  260. data/lib/active_support/values/time_zone.rb +306 -135
  261. data/lib/active_support/version.rb +6 -7
  262. data/lib/active_support/xml_mini/jdom.rb +117 -115
  263. data/lib/active_support/xml_mini/libxml.rb +22 -21
  264. data/lib/active_support/xml_mini/libxmlsax.rb +17 -19
  265. data/lib/active_support/xml_mini/nokogiri.rb +19 -19
  266. data/lib/active_support/xml_mini/nokogirisax.rb +16 -17
  267. data/lib/active_support/xml_mini/rexml.rb +25 -17
  268. data/lib/active_support/xml_mini.rb +67 -56
  269. data/lib/active_support.rb +58 -3
  270. metadata +125 -66
  271. data/lib/active_support/basic_object.rb +0 -11
  272. data/lib/active_support/buffered_logger.rb +0 -21
  273. data/lib/active_support/concurrency/latch.rb +0 -27
  274. data/lib/active_support/core_ext/array/prepend_and_append.rb +0 -7
  275. data/lib/active_support/core_ext/array/uniq_by.rb +0 -19
  276. data/lib/active_support/core_ext/class/delegating_attributes.rb +0 -40
  277. data/lib/active_support/core_ext/date_time/zones.rb +0 -24
  278. data/lib/active_support/core_ext/hash/diff.rb +0 -14
  279. data/lib/active_support/core_ext/kernel/agnostics.rb +0 -11
  280. data/lib/active_support/core_ext/kernel/debugger.rb +0 -10
  281. data/lib/active_support/core_ext/logger.rb +0 -67
  282. data/lib/active_support/core_ext/marshal.rb +0 -21
  283. data/lib/active_support/core_ext/module/qualified_const.rb +0 -52
  284. data/lib/active_support/core_ext/module/reachable.rb +0 -8
  285. data/lib/active_support/core_ext/object/to_json.rb +0 -27
  286. data/lib/active_support/core_ext/proc.rb +0 -17
  287. data/lib/active_support/core_ext/range/include_range.rb +0 -23
  288. data/lib/active_support/core_ext/string/encoding.rb +0 -8
  289. data/lib/active_support/core_ext/struct.rb +0 -6
  290. data/lib/active_support/core_ext/thread.rb +0 -79
  291. data/lib/active_support/core_ext/time/marshal.rb +0 -30
  292. data/lib/active_support/file_watcher.rb +0 -36
  293. data/lib/active_support/json/variable.rb +0 -18
  294. data/lib/active_support/testing/pending.rb +0 -14
  295. data/lib/active_support/values/unicode_tables.dat +0 -0
@@ -1,43 +1,63 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "active_support/notifications"
2
4
 
3
5
  module ActiveSupport
6
+ # Raised when <tt>ActiveSupport::Deprecation::Behavior#behavior</tt> is set with <tt>:raise</tt>.
7
+ # You would set <tt>:raise</tt>, as a behavior to raise errors and proactively report exceptions from deprecations.
4
8
  class DeprecationException < StandardError
5
9
  end
6
10
 
7
11
  class Deprecation
8
12
  # Default warning behaviors per Rails.env.
9
13
  DEFAULT_BEHAVIORS = {
10
- raise: ->(message, callstack) {
14
+ raise: ->(message, callstack, deprecation_horizon, gem_name) {
11
15
  e = DeprecationException.new(message)
12
- e.set_backtrace(callstack)
16
+ e.set_backtrace(callstack.map(&:to_s))
13
17
  raise e
14
18
  },
15
19
 
16
- stderr: ->(message, callstack) {
20
+ stderr: ->(message, callstack, deprecation_horizon, gem_name) {
17
21
  $stderr.puts(message)
18
22
  $stderr.puts callstack.join("\n ") if debug
19
23
  },
20
-
21
- log: ->(message, callstack) {
24
+
25
+ log: ->(message, callstack, deprecation_horizon, gem_name) {
22
26
  logger =
23
- if defined?(Rails) && Rails.logger
27
+ if defined?(Rails.logger) && Rails.logger
24
28
  Rails.logger
25
29
  else
26
- require 'active_support/logger'
30
+ require "active_support/logger"
27
31
  ActiveSupport::Logger.new($stderr)
28
32
  end
29
33
  logger.warn message
30
34
  logger.debug callstack.join("\n ") if debug
31
35
  },
32
-
33
- notify: ->(message, callstack) {
34
- ActiveSupport::Notifications.instrument("deprecation.rails",
35
- :message => message, :callstack => callstack)
36
+
37
+ notify: ->(message, callstack, deprecation_horizon, gem_name) {
38
+ notification_name = "deprecation.#{gem_name.underscore.tr('/', '_')}"
39
+ ActiveSupport::Notifications.instrument(notification_name,
40
+ message: message,
41
+ callstack: callstack,
42
+ gem_name: gem_name,
43
+ deprecation_horizon: deprecation_horizon)
36
44
  },
37
-
38
- silence: ->(message, callstack) {},
45
+
46
+ silence: ->(message, callstack, deprecation_horizon, gem_name) { },
39
47
  }
40
48
 
49
+ # Behavior module allows to determine how to display deprecation messages.
50
+ # You can create a custom behavior or set any from the +DEFAULT_BEHAVIORS+
51
+ # constant. Available behaviors are:
52
+ #
53
+ # [+raise+] Raise <tt>ActiveSupport::DeprecationException</tt>.
54
+ # [+stderr+] Log all deprecation warnings to <tt>$stderr</tt>.
55
+ # [+log+] Log all deprecation warnings to +Rails.logger+.
56
+ # [+notify+] Use +ActiveSupport::Notifications+ to notify +deprecation.rails+.
57
+ # [+silence+] Do nothing. On Rails, set <tt>config.active_support.report_deprecations = false</tt> to disable all behaviors.
58
+ #
59
+ # Setting behaviors only affects deprecations that happen after boot time.
60
+ # For more information you can read the documentation of the +behavior=+ method.
41
61
  module Behavior
42
62
  # Whether to print a backtrace along with the warning.
43
63
  attr_accessor :debug
@@ -47,13 +67,18 @@ module ActiveSupport
47
67
  @behavior ||= [DEFAULT_BEHAVIORS[:stderr]]
48
68
  end
49
69
 
70
+ # Returns the current behavior for disallowed deprecations or if one isn't set, defaults to +:raise+.
71
+ def disallowed_behavior
72
+ @disallowed_behavior ||= [DEFAULT_BEHAVIORS[:raise]]
73
+ end
74
+
50
75
  # Sets the behavior to the specified value. Can be a single value, array,
51
76
  # or an object that responds to +call+.
52
77
  #
53
78
  # Available behaviors:
54
79
  #
55
80
  # [+raise+] Raise <tt>ActiveSupport::DeprecationException</tt>.
56
- # [+stderr+] Log all deprecation warnings to +$stderr+.
81
+ # [+stderr+] Log all deprecation warnings to <tt>$stderr</tt>.
57
82
  # [+log+] Log all deprecation warnings to +Rails.logger+.
58
83
  # [+notify+] Use +ActiveSupport::Notifications+ to notify +deprecation.rails+.
59
84
  # [+silence+] Do nothing.
@@ -65,12 +90,36 @@ module ActiveSupport
65
90
  # ActiveSupport::Deprecation.behavior = :stderr
66
91
  # ActiveSupport::Deprecation.behavior = [:stderr, :log]
67
92
  # ActiveSupport::Deprecation.behavior = MyCustomHandler
68
- # ActiveSupport::Deprecation.behavior = ->(message, callstack) {
93
+ # ActiveSupport::Deprecation.behavior = ->(message, callstack, deprecation_horizon, gem_name) {
69
94
  # # custom stuff
70
95
  # }
96
+ #
97
+ # If you are using Rails, you can set <tt>config.active_support.report_deprecations = false</tt> to disable
98
+ # all deprecation behaviors. This is similar to the +silence+ option but more performant.
71
99
  def behavior=(behavior)
72
- @behavior = Array(behavior).map { |b| DEFAULT_BEHAVIORS[b] || b }
100
+ @behavior = Array(behavior).map { |b| DEFAULT_BEHAVIORS[b] || arity_coerce(b) }
101
+ end
102
+
103
+ # Sets the behavior for disallowed deprecations (those configured by
104
+ # ActiveSupport::Deprecation.disallowed_warnings=) to the specified
105
+ # value. As with +behavior=+, this can be a single value, array, or an
106
+ # object that responds to +call+.
107
+ def disallowed_behavior=(behavior)
108
+ @disallowed_behavior = Array(behavior).map { |b| DEFAULT_BEHAVIORS[b] || arity_coerce(b) }
73
109
  end
110
+
111
+ private
112
+ def arity_coerce(behavior)
113
+ unless behavior.respond_to?(:call)
114
+ raise ArgumentError, "#{behavior.inspect} is not a valid deprecation behavior."
115
+ end
116
+
117
+ if behavior.arity == 4 || behavior.arity == -1
118
+ behavior
119
+ else
120
+ -> message, callstack, _, _ { behavior.call(message, callstack) }
121
+ end
122
+ end
74
123
  end
75
124
  end
76
125
  end
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveSupport
4
+ class Deprecation
5
+ # DeprecatedConstantAccessor transforms a constant into a deprecated one by
6
+ # hooking +const_missing+.
7
+ #
8
+ # It takes the names of an old (deprecated) constant and of a new constant
9
+ # (both in string form) and optionally a deprecator. The deprecator defaults
10
+ # to +ActiveSupport::Deprecator+ if none is specified.
11
+ #
12
+ # The deprecated constant now returns the same object as the new one rather
13
+ # than a proxy object, so it can be used transparently in +rescue+ blocks
14
+ # etc.
15
+ #
16
+ # PLANETS = %w(mercury venus earth mars jupiter saturn uranus neptune pluto)
17
+ #
18
+ # # (In a later update, the original implementation of `PLANETS` has been removed.)
19
+ #
20
+ # PLANETS_POST_2006 = %w(mercury venus earth mars jupiter saturn uranus neptune)
21
+ # include ActiveSupport::Deprecation::DeprecatedConstantAccessor
22
+ # deprecate_constant 'PLANETS', 'PLANETS_POST_2006'
23
+ #
24
+ # PLANETS.map { |planet| planet.capitalize }
25
+ # # => DEPRECATION WARNING: PLANETS is deprecated! Use PLANETS_POST_2006 instead.
26
+ # (Backtrace information…)
27
+ # ["Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune"]
28
+ module DeprecatedConstantAccessor
29
+ def self.included(base)
30
+ require "active_support/inflector/methods"
31
+
32
+ extension = Module.new do
33
+ def const_missing(missing_const_name)
34
+ if class_variable_defined?(:@@_deprecated_constants)
35
+ if (replacement = class_variable_get(:@@_deprecated_constants)[missing_const_name.to_s])
36
+ replacement[:deprecator].warn(replacement[:message] || "#{name}::#{missing_const_name} is deprecated! Use #{replacement[:new]} instead.", caller_locations)
37
+ return ActiveSupport::Inflector.constantize(replacement[:new].to_s)
38
+ end
39
+ end
40
+ super
41
+ end
42
+
43
+ def deprecate_constant(const_name, new_constant, message: nil, deprecator: ActiveSupport::Deprecation.instance)
44
+ class_variable_set(:@@_deprecated_constants, {}) unless class_variable_defined?(:@@_deprecated_constants)
45
+ class_variable_get(:@@_deprecated_constants)[const_name.to_s] = { new: new_constant, message: message, deprecator: deprecator }
46
+ end
47
+ end
48
+ base.singleton_class.prepend extension
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveSupport
4
+ class Deprecation
5
+ module Disallowed
6
+ # Sets the criteria used to identify deprecation messages which should be
7
+ # disallowed. Can be an array containing strings, symbols, or regular
8
+ # expressions. (Symbols are treated as strings). These are compared against
9
+ # the text of the generated deprecation warning.
10
+ #
11
+ # Additionally the scalar symbol +:all+ may be used to treat all
12
+ # deprecations as disallowed.
13
+ #
14
+ # Deprecations matching a substring or regular expression will be handled
15
+ # using the configured +ActiveSupport::Deprecation.disallowed_behavior+
16
+ # rather than +ActiveSupport::Deprecation.behavior+
17
+ attr_writer :disallowed_warnings
18
+
19
+ # Returns the configured criteria used to identify deprecation messages
20
+ # which should be treated as disallowed.
21
+ def disallowed_warnings
22
+ @disallowed_warnings ||= []
23
+ end
24
+
25
+ private
26
+ def deprecation_disallowed?(message)
27
+ disallowed = ActiveSupport::Deprecation.disallowed_warnings
28
+ return false if explicitly_allowed?(message)
29
+ return true if disallowed == :all
30
+ disallowed.any? do |rule|
31
+ case rule
32
+ when String, Symbol
33
+ message.include?(rule.to_s)
34
+ when Regexp
35
+ rule.match?(message)
36
+ end
37
+ end
38
+ end
39
+
40
+ def explicitly_allowed?(message)
41
+ allowances = @explicitly_allowed_warnings.value
42
+ return false unless allowances
43
+ return true if allowances == :all
44
+ allowances = [allowances] unless allowances.kind_of?(Array)
45
+ allowances.any? do |rule|
46
+ case rule
47
+ when String, Symbol
48
+ message.include?(rule.to_s)
49
+ when Regexp
50
+ rule.match?(message)
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
@@ -1,11 +1,13 @@
1
- require 'active_support/core_ext/kernel/singleton_class'
2
- require 'active_support/core_ext/module/delegation'
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/core_ext/module/delegation"
3
4
 
4
5
  module ActiveSupport
5
6
  class Deprecation
6
7
  module InstanceDelegator # :nodoc:
7
8
  def self.included(base)
8
9
  base.extend(ClassMethods)
10
+ base.singleton_class.prepend(OverrideDelegators)
9
11
  base.public_class_method :new
10
12
  end
11
13
 
@@ -19,6 +21,18 @@ module ActiveSupport
19
21
  singleton_class.delegate(method_name, to: :instance)
20
22
  end
21
23
  end
24
+
25
+ module OverrideDelegators # :nodoc:
26
+ def warn(message = nil, callstack = nil)
27
+ callstack ||= caller_locations(2)
28
+ super
29
+ end
30
+
31
+ def deprecation_warning(deprecated_method_name, message = nil, caller_backtrace = nil)
32
+ caller_backtrace ||= caller_locations(2)
33
+ super
34
+ end
35
+ end
22
36
  end
23
37
  end
24
38
  end
@@ -1,43 +1,84 @@
1
- require 'active_support/core_ext/module/aliasing'
2
- require 'active_support/core_ext/array/extract_options'
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/core_ext/array/extract_options"
4
+ require "active_support/core_ext/module/redefine_method"
3
5
 
4
6
  module ActiveSupport
5
7
  class Deprecation
6
8
  module MethodWrapper
7
9
  # Declare that a method has been deprecated.
8
10
  #
9
- # module Fred
10
- # extend self
11
- #
12
- # def foo; end
13
- # def bar; end
14
- # def baz; end
11
+ # class Fred
12
+ # def aaa; end
13
+ # def bbb; end
14
+ # def ccc; end
15
+ # def ddd; end
16
+ # def eee; end
15
17
  # end
16
18
  #
17
- # ActiveSupport::Deprecation.deprecate_methods(Fred, :foo, bar: :qux, baz: 'use Bar#baz instead')
18
- # # => [:foo, :bar, :baz]
19
+ # Using the default deprecator:
20
+ # ActiveSupport::Deprecation.deprecate_methods(Fred, :aaa, bbb: :zzz, ccc: 'use Bar#ccc instead')
21
+ # # => Fred
22
+ #
23
+ # Fred.new.aaa
24
+ # # DEPRECATION WARNING: aaa is deprecated and will be removed from Rails 5.1. (called from irb_binding at (irb):10)
25
+ # # => nil
26
+ #
27
+ # Fred.new.bbb
28
+ # # DEPRECATION WARNING: bbb is deprecated and will be removed from Rails 5.1 (use zzz instead). (called from irb_binding at (irb):11)
29
+ # # => nil
19
30
  #
20
- # Fred.foo
21
- # # => "DEPRECATION WARNING: foo is deprecated and will be removed from Rails 4.1."
31
+ # Fred.new.ccc
32
+ # # DEPRECATION WARNING: ccc is deprecated and will be removed from Rails 5.1 (use Bar#ccc instead). (called from irb_binding at (irb):12)
33
+ # # => nil
22
34
  #
23
- # Fred.bar
24
- # # => "DEPRECATION WARNING: bar is deprecated and will be removed from Rails 4.1 (use qux instead)."
35
+ # Passing in a custom deprecator:
36
+ # custom_deprecator = ActiveSupport::Deprecation.new('next-release', 'MyGem')
37
+ # ActiveSupport::Deprecation.deprecate_methods(Fred, ddd: :zzz, deprecator: custom_deprecator)
38
+ # # => [:ddd]
25
39
  #
26
- # Fred.baz
27
- # # => "DEPRECATION WARNING: baz is deprecated and will be removed from Rails 4.1 (use Bar#baz instead)."
40
+ # Fred.new.ddd
41
+ # DEPRECATION WARNING: ddd is deprecated and will be removed from MyGem next-release (use zzz instead). (called from irb_binding at (irb):15)
42
+ # # => nil
43
+ #
44
+ # Using a custom deprecator directly:
45
+ # custom_deprecator = ActiveSupport::Deprecation.new('next-release', 'MyGem')
46
+ # custom_deprecator.deprecate_methods(Fred, eee: :zzz)
47
+ # # => [:eee]
48
+ #
49
+ # Fred.new.eee
50
+ # DEPRECATION WARNING: eee is deprecated and will be removed from MyGem next-release (use zzz instead). (called from irb_binding at (irb):18)
51
+ # # => nil
28
52
  def deprecate_methods(target_module, *method_names)
29
53
  options = method_names.extract_options!
30
- deprecator = options.delete(:deprecator) || ActiveSupport::Deprecation.instance
54
+ deprecator = options.delete(:deprecator) || self
31
55
  method_names += options.keys
56
+ mod = nil
32
57
 
33
58
  method_names.each do |method_name|
34
- target_module.alias_method_chain(method_name, :deprecation) do |target, punctuation|
35
- target_module.send(:define_method, "#{target}_with_deprecation#{punctuation}") do |*args, &block|
36
- deprecator.deprecation_warning(method_name, options[method_name])
37
- send(:"#{target}_without_deprecation#{punctuation}", *args, &block)
59
+ message = options[method_name]
60
+ if target_module.method_defined?(method_name) || target_module.private_method_defined?(method_name)
61
+ method = target_module.instance_method(method_name)
62
+ target_module.module_eval do
63
+ redefine_method(method_name) do |*args, &block|
64
+ deprecator.deprecation_warning(method_name, message)
65
+ method.bind_call(self, *args, &block)
66
+ end
67
+ ruby2_keywords(method_name)
68
+ end
69
+ else
70
+ mod ||= Module.new
71
+ mod.module_eval do
72
+ define_method(method_name) do |*args, &block|
73
+ deprecator.deprecation_warning(method_name, message)
74
+ super(*args, &block)
75
+ end
76
+ ruby2_keywords(method_name)
38
77
  end
39
78
  end
40
79
  end
80
+
81
+ target_module.prepend(mod) if mod
41
82
  end
42
83
  end
43
84
  end
@@ -1,8 +1,8 @@
1
- require 'active_support/inflector/methods'
1
+ # frozen_string_literal: true
2
2
 
3
3
  module ActiveSupport
4
4
  class Deprecation
5
- class DeprecationProxy #:nodoc:
5
+ class DeprecationProxy # :nodoc:
6
6
  def self.new(*args, &block)
7
7
  object = args.first
8
8
 
@@ -10,7 +10,7 @@ module ActiveSupport
10
10
  super
11
11
  end
12
12
 
13
- instance_methods.each { |m| undef_method m unless m =~ /^__|^object_id$/ }
13
+ instance_methods.each { |m| undef_method m unless /^__|^object_id$/.match?(m) }
14
14
 
15
15
  # Don't give a deprecation warning on inspect since test/unit and error
16
16
  # logs rely on it for diagnostics.
@@ -20,20 +20,22 @@ module ActiveSupport
20
20
 
21
21
  private
22
22
  def method_missing(called, *args, &block)
23
- warn caller, called, args
23
+ warn caller_locations, called, args
24
24
  target.__send__(called, *args, &block)
25
25
  end
26
26
  end
27
27
 
28
- # This DeprecatedObjectProxy transforms object to deprecated object.
28
+ # DeprecatedObjectProxy transforms an object into a deprecated one. It
29
+ # takes an object, a deprecation message and optionally a deprecator. The
30
+ # deprecator defaults to +ActiveSupport::Deprecator+ if none is specified.
29
31
  #
30
- # @old_object = DeprecatedObjectProxy.new(Object.new, "Don't use this object anymore!")
31
- # @old_object = DeprecatedObjectProxy.new(Object.new, "Don't use this object anymore!", deprecator_instance)
32
+ # deprecated_object = ActiveSupport::Deprecation::DeprecatedObjectProxy.new(Object.new, "This object is now deprecated")
33
+ # # => #<Object:0x007fb9b34c34b0>
32
34
  #
33
- # When someone executes any method except +inspect+ on proxy object this will
34
- # trigger +warn+ method on +deprecator_instance+.
35
- #
36
- # Default deprecator is <tt>ActiveSupport::Deprecation</tt>
35
+ # deprecated_object.to_s
36
+ # DEPRECATION WARNING: This object is now deprecated.
37
+ # (Backtrace)
38
+ # # => "#<Object:0x007fb9b34c34b0>"
37
39
  class DeprecatedObjectProxy < DeprecationProxy
38
40
  def initialize(object, message, deprecator = ActiveSupport::Deprecation.instance)
39
41
  @object = object
@@ -51,13 +53,16 @@ module ActiveSupport
51
53
  end
52
54
  end
53
55
 
54
- # This DeprecatedInstanceVariableProxy transforms instance variable to
55
- # deprecated instance variable.
56
+ # DeprecatedInstanceVariableProxy transforms an instance variable into a
57
+ # deprecated one. It takes an instance of a class, a method on that class
58
+ # and an instance variable. It optionally takes a deprecator as the last
59
+ # argument. The deprecator defaults to +ActiveSupport::Deprecator+ if none
60
+ # is specified.
56
61
  #
57
62
  # class Example
58
- # def initialize(deprecator)
59
- # @request = ActiveSupport::Deprecation::DeprecatedInstanceVariableProxy.new(self, :request, :@request, deprecator)
60
- # @_request = :a_request
63
+ # def initialize
64
+ # @request = ActiveSupport::Deprecation::DeprecatedInstanceVariableProxy.new(self, :request, :@request)
65
+ # @_request = :special_request
61
66
  # end
62
67
  #
63
68
  # def request
@@ -69,12 +74,17 @@ module ActiveSupport
69
74
  # end
70
75
  # end
71
76
  #
72
- # When someone execute any method on @request variable this will trigger
73
- # +warn+ method on +deprecator_instance+ and will fetch <tt>@_request</tt>
74
- # variable via +request+ method and execute the same method on non-proxy
75
- # instance variable.
77
+ # example = Example.new
78
+ # # => #<Example:0x007fb9b31090b8 @_request=:special_request, @request=:special_request>
79
+ #
80
+ # example.old_request.to_s
81
+ # # => DEPRECATION WARNING: @request is deprecated! Call request.to_s instead of
82
+ # @request.to_s
83
+ # (Backtrace information…)
84
+ # "special_request"
76
85
  #
77
- # Default deprecator is <tt>ActiveSupport::Deprecation</tt>.
86
+ # example.request.to_s
87
+ # # => "special_request"
78
88
  class DeprecatedInstanceVariableProxy < DeprecationProxy
79
89
  def initialize(instance, method, var = "@#{method}", deprecator = ActiveSupport::Deprecation.instance)
80
90
  @instance = instance
@@ -93,22 +103,57 @@ module ActiveSupport
93
103
  end
94
104
  end
95
105
 
96
- # This DeprecatedConstantProxy transforms constant to deprecated constant.
106
+ # DeprecatedConstantProxy transforms a constant into a deprecated one. It
107
+ # takes the names of an old (deprecated) constant and of a new constant
108
+ # (both in string form) and optionally a deprecator. The deprecator defaults
109
+ # to +ActiveSupport::Deprecator+ if none is specified. The deprecated constant
110
+ # now returns the value of the new one.
111
+ #
112
+ # PLANETS = %w(mercury venus earth mars jupiter saturn uranus neptune pluto)
97
113
  #
98
- # OLD_CONST = ActiveSupport::Deprecation::DeprecatedConstantProxy.new('OLD_CONST', 'NEW_CONST')
99
- # OLD_CONST = ActiveSupport::Deprecation::DeprecatedConstantProxy.new('OLD_CONST', 'NEW_CONST', deprecator_instance)
114
+ # # (In a later update, the original implementation of `PLANETS` has been removed.)
100
115
  #
101
- # When someone use old constant this will trigger +warn+ method on
102
- # +deprecator_instance+.
116
+ # PLANETS_POST_2006 = %w(mercury venus earth mars jupiter saturn uranus neptune)
117
+ # PLANETS = ActiveSupport::Deprecation::DeprecatedConstantProxy.new('PLANETS', 'PLANETS_POST_2006')
103
118
  #
104
- # Default deprecator is <tt>ActiveSupport::Deprecation</tt>.
105
- class DeprecatedConstantProxy < DeprecationProxy
106
- def initialize(old_const, new_const, deprecator = ActiveSupport::Deprecation.instance)
119
+ # PLANETS.map { |planet| planet.capitalize }
120
+ # # => DEPRECATION WARNING: PLANETS is deprecated! Use PLANETS_POST_2006 instead.
121
+ # (Backtrace information…)
122
+ # ["Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune"]
123
+ class DeprecatedConstantProxy < Module
124
+ def self.new(*args, **options, &block)
125
+ object = args.first
126
+
127
+ return object unless object
128
+ super
129
+ end
130
+
131
+ def initialize(old_const, new_const, deprecator = ActiveSupport::Deprecation.instance, message: "#{old_const} is deprecated! Use #{new_const} instead.")
132
+ Kernel.require "active_support/inflector/methods"
133
+
107
134
  @old_const = old_const
108
135
  @new_const = new_const
109
136
  @deprecator = deprecator
137
+ @message = message
138
+ end
139
+
140
+ instance_methods.each { |m| undef_method m unless /^__|^object_id$/.match?(m) }
141
+
142
+ # Don't give a deprecation warning on inspect since test/unit and error
143
+ # logs rely on it for diagnostics.
144
+ def inspect
145
+ target.inspect
110
146
  end
111
147
 
148
+ # Don't give a deprecation warning on methods that IRB may invoke
149
+ # during tab-completion.
150
+ delegate :hash, :instance_methods, :name, :respond_to?, to: :target
151
+
152
+ # Returns the class of the new constant.
153
+ #
154
+ # PLANETS_POST_2006 = %w(mercury venus earth mars jupiter saturn uranus neptune)
155
+ # PLANETS = ActiveSupport::Deprecation::DeprecatedConstantProxy.new('PLANETS', 'PLANETS_POST_2006')
156
+ # PLANETS.class # => Array
112
157
  def class
113
158
  target.class
114
159
  end
@@ -118,8 +163,14 @@ module ActiveSupport
118
163
  ActiveSupport::Inflector.constantize(@new_const.to_s)
119
164
  end
120
165
 
121
- def warn(callstack, called, args)
122
- @deprecator.warn("#{@old_const} is deprecated! Use #{@new_const} instead.", callstack)
166
+ def const_missing(name)
167
+ @deprecator.warn(@message, caller_locations)
168
+ target.const_get(name)
169
+ end
170
+
171
+ def method_missing(called, *args, &block)
172
+ @deprecator.warn(@message, caller_locations)
173
+ target.__send__(called, *args, &block)
123
174
  end
124
175
  end
125
176
  end