activesupport 4.2.11.1 → 6.0.3.1

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 (263) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +399 -411
  3. data/MIT-LICENSE +2 -2
  4. data/README.rdoc +7 -7
  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 +34 -6
  9. data/lib/active_support/benchmarkable.rb +6 -4
  10. data/lib/active_support/builder.rb +3 -1
  11. data/lib/active_support/cache/file_store.rb +58 -53
  12. data/lib/active_support/cache/mem_cache_store.rb +95 -91
  13. data/lib/active_support/cache/memory_store.rb +39 -36
  14. data/lib/active_support/cache/null_store.rb +11 -7
  15. data/lib/active_support/cache/redis_cache_store.rb +493 -0
  16. data/lib/active_support/cache/strategy/local_cache.rb +75 -42
  17. data/lib/active_support/cache/strategy/local_cache_middleware.rb +10 -9
  18. data/lib/active_support/cache.rb +331 -217
  19. data/lib/active_support/callbacks.rb +650 -592
  20. data/lib/active_support/concern.rb +35 -6
  21. data/lib/active_support/concurrency/load_interlock_aware_monitor.rb +35 -0
  22. data/lib/active_support/concurrency/share_lock.rb +226 -0
  23. data/lib/active_support/configurable.rb +13 -14
  24. data/lib/active_support/core_ext/array/access.rb +41 -1
  25. data/lib/active_support/core_ext/array/conversions.rb +24 -20
  26. data/lib/active_support/core_ext/array/extract.rb +21 -0
  27. data/lib/active_support/core_ext/array/extract_options.rb +2 -0
  28. data/lib/active_support/core_ext/array/grouping.rb +11 -18
  29. data/lib/active_support/core_ext/array/inquiry.rb +19 -0
  30. data/lib/active_support/core_ext/array/prepend_and_append.rb +4 -6
  31. data/lib/active_support/core_ext/array/wrap.rb +7 -4
  32. data/lib/active_support/core_ext/array.rb +9 -6
  33. data/lib/active_support/core_ext/benchmark.rb +3 -1
  34. data/lib/active_support/core_ext/big_decimal/conversions.rb +10 -12
  35. data/lib/active_support/core_ext/big_decimal.rb +3 -1
  36. data/lib/active_support/core_ext/class/attribute.rb +45 -31
  37. data/lib/active_support/core_ext/class/attribute_accessors.rb +3 -1
  38. data/lib/active_support/core_ext/class/subclasses.rb +20 -6
  39. data/lib/active_support/core_ext/class.rb +4 -3
  40. data/lib/active_support/core_ext/date/acts_like.rb +3 -1
  41. data/lib/active_support/core_ext/date/blank.rb +14 -0
  42. data/lib/active_support/core_ext/date/calculations.rb +17 -14
  43. data/lib/active_support/core_ext/date/conversions.rb +25 -23
  44. data/lib/active_support/core_ext/date/zones.rb +4 -2
  45. data/lib/active_support/core_ext/date.rb +6 -4
  46. data/lib/active_support/core_ext/date_and_time/calculations.rb +154 -65
  47. data/lib/active_support/core_ext/date_and_time/compatibility.rb +4 -3
  48. data/lib/active_support/core_ext/date_and_time/zones.rb +12 -13
  49. data/lib/active_support/core_ext/date_time/acts_like.rb +4 -2
  50. data/lib/active_support/core_ext/date_time/blank.rb +14 -0
  51. data/lib/active_support/core_ext/date_time/calculations.rb +37 -19
  52. data/lib/active_support/core_ext/date_time/compatibility.rb +8 -6
  53. data/lib/active_support/core_ext/date_time/conversions.rb +16 -13
  54. data/lib/active_support/core_ext/date_time.rb +7 -5
  55. data/lib/active_support/core_ext/digest/uuid.rb +7 -5
  56. data/lib/active_support/core_ext/digest.rb +3 -0
  57. data/lib/active_support/core_ext/enumerable.rb +114 -22
  58. data/lib/active_support/core_ext/file/atomic.rb +38 -31
  59. data/lib/active_support/core_ext/file.rb +3 -1
  60. data/lib/active_support/core_ext/hash/compact.rb +4 -23
  61. data/lib/active_support/core_ext/hash/conversions.rb +62 -41
  62. data/lib/active_support/core_ext/hash/deep_merge.rb +9 -13
  63. data/lib/active_support/core_ext/hash/deep_transform_values.rb +46 -0
  64. data/lib/active_support/core_ext/hash/except.rb +12 -9
  65. data/lib/active_support/core_ext/hash/indifferent_access.rb +4 -3
  66. data/lib/active_support/core_ext/hash/keys.rb +19 -42
  67. data/lib/active_support/core_ext/hash/reverse_merge.rb +5 -2
  68. data/lib/active_support/core_ext/hash/slice.rb +5 -27
  69. data/lib/active_support/core_ext/hash/transform_values.rb +4 -22
  70. data/lib/active_support/core_ext/hash.rb +10 -9
  71. data/lib/active_support/core_ext/integer/inflections.rb +3 -1
  72. data/lib/active_support/core_ext/integer/multiple.rb +3 -1
  73. data/lib/active_support/core_ext/integer/time.rb +11 -18
  74. data/lib/active_support/core_ext/integer.rb +5 -3
  75. data/lib/active_support/core_ext/kernel/concern.rb +5 -1
  76. data/lib/active_support/core_ext/kernel/reporting.rb +4 -84
  77. data/lib/active_support/core_ext/kernel/singleton_class.rb +2 -0
  78. data/lib/active_support/core_ext/kernel.rb +5 -5
  79. data/lib/active_support/core_ext/load_error.rb +3 -22
  80. data/lib/active_support/core_ext/marshal.rb +8 -8
  81. data/lib/active_support/core_ext/module/aliasing.rb +6 -44
  82. data/lib/active_support/core_ext/module/anonymous.rb +12 -1
  83. data/lib/active_support/core_ext/module/attr_internal.rb +8 -9
  84. data/lib/active_support/core_ext/module/attribute_accessors.rb +46 -46
  85. data/lib/active_support/core_ext/module/attribute_accessors_per_thread.rb +144 -0
  86. data/lib/active_support/core_ext/module/concerning.rb +11 -12
  87. data/lib/active_support/core_ext/module/delegation.rb +133 -30
  88. data/lib/active_support/core_ext/module/deprecation.rb +4 -2
  89. data/lib/active_support/core_ext/module/introspection.rb +44 -19
  90. data/lib/active_support/core_ext/module/reachable.rb +5 -7
  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 -11
  94. data/lib/active_support/core_ext/name_error.rb +22 -2
  95. data/lib/active_support/core_ext/numeric/bytes.rb +22 -0
  96. data/lib/active_support/core_ext/numeric/conversions.rb +129 -136
  97. data/lib/active_support/core_ext/numeric/inquiry.rb +5 -0
  98. data/lib/active_support/core_ext/numeric/time.rb +35 -23
  99. data/lib/active_support/core_ext/numeric.rb +5 -3
  100. data/lib/active_support/core_ext/object/acts_like.rb +12 -1
  101. data/lib/active_support/core_ext/object/blank.rb +27 -3
  102. data/lib/active_support/core_ext/object/conversions.rb +6 -4
  103. data/lib/active_support/core_ext/object/deep_dup.rb +13 -4
  104. data/lib/active_support/core_ext/object/duplicable.rb +13 -93
  105. data/lib/active_support/core_ext/object/inclusion.rb +5 -3
  106. data/lib/active_support/core_ext/object/instance_variables.rb +3 -1
  107. data/lib/active_support/core_ext/object/json.rb +51 -20
  108. data/lib/active_support/core_ext/object/to_param.rb +3 -1
  109. data/lib/active_support/core_ext/object/to_query.rb +10 -5
  110. data/lib/active_support/core_ext/object/try.rb +81 -23
  111. data/lib/active_support/core_ext/object/with_options.rb +16 -3
  112. data/lib/active_support/core_ext/object.rb +14 -13
  113. data/lib/active_support/core_ext/range/compare_range.rb +76 -0
  114. data/lib/active_support/core_ext/range/conversions.rb +37 -15
  115. data/lib/active_support/core_ext/range/each.rb +18 -17
  116. data/lib/active_support/core_ext/range/include_range.rb +7 -21
  117. data/lib/active_support/core_ext/range/include_time_with_zone.rb +23 -0
  118. data/lib/active_support/core_ext/range/overlaps.rb +2 -0
  119. data/lib/active_support/core_ext/range.rb +7 -4
  120. data/lib/active_support/core_ext/regexp.rb +2 -0
  121. data/lib/active_support/core_ext/securerandom.rb +45 -0
  122. data/lib/active_support/core_ext/string/access.rb +16 -6
  123. data/lib/active_support/core_ext/string/behavior.rb +3 -1
  124. data/lib/active_support/core_ext/string/conversions.rb +7 -4
  125. data/lib/active_support/core_ext/string/exclude.rb +2 -0
  126. data/lib/active_support/core_ext/string/filters.rb +48 -6
  127. data/lib/active_support/core_ext/string/indent.rb +6 -4
  128. data/lib/active_support/core_ext/string/inflections.rb +66 -24
  129. data/lib/active_support/core_ext/string/inquiry.rb +3 -1
  130. data/lib/active_support/core_ext/string/multibyte.rb +16 -7
  131. data/lib/active_support/core_ext/string/output_safety.rb +93 -40
  132. data/lib/active_support/core_ext/string/starts_ends_with.rb +2 -0
  133. data/lib/active_support/core_ext/string/strip.rb +6 -5
  134. data/lib/active_support/core_ext/string/zones.rb +4 -2
  135. data/lib/active_support/core_ext/string.rb +15 -13
  136. data/lib/active_support/core_ext/time/acts_like.rb +3 -1
  137. data/lib/active_support/core_ext/time/calculations.rb +115 -52
  138. data/lib/active_support/core_ext/time/compatibility.rb +4 -2
  139. data/lib/active_support/core_ext/time/conversions.rb +20 -13
  140. data/lib/active_support/core_ext/time/zones.rb +41 -7
  141. data/lib/active_support/core_ext/time.rb +7 -6
  142. data/lib/active_support/core_ext/uri.rb +6 -7
  143. data/lib/active_support/core_ext.rb +3 -1
  144. data/lib/active_support/current_attributes.rb +203 -0
  145. data/lib/active_support/dependencies/autoload.rb +2 -0
  146. data/lib/active_support/dependencies/interlock.rb +57 -0
  147. data/lib/active_support/dependencies/zeitwerk_integration.rb +117 -0
  148. data/lib/active_support/dependencies.rb +208 -166
  149. data/lib/active_support/deprecation/behaviors.rb +44 -11
  150. data/lib/active_support/deprecation/constant_accessor.rb +52 -0
  151. data/lib/active_support/deprecation/instance_delegator.rb +17 -2
  152. data/lib/active_support/deprecation/method_wrappers.rb +61 -21
  153. data/lib/active_support/deprecation/proxy_wrappers.rb +81 -30
  154. data/lib/active_support/deprecation/reporting.rb +32 -12
  155. data/lib/active_support/deprecation.rb +12 -9
  156. data/lib/active_support/descendants_tracker.rb +57 -9
  157. data/lib/active_support/digest.rb +20 -0
  158. data/lib/active_support/duration/iso8601_parser.rb +123 -0
  159. data/lib/active_support/duration/iso8601_serializer.rb +53 -0
  160. data/lib/active_support/duration.rb +315 -40
  161. data/lib/active_support/encrypted_configuration.rb +45 -0
  162. data/lib/active_support/encrypted_file.rb +100 -0
  163. data/lib/active_support/evented_file_update_checker.rb +234 -0
  164. data/lib/active_support/execution_wrapper.rb +129 -0
  165. data/lib/active_support/executor.rb +8 -0
  166. data/lib/active_support/file_update_checker.rb +62 -37
  167. data/lib/active_support/gem_version.rb +6 -4
  168. data/lib/active_support/gzip.rb +7 -5
  169. data/lib/active_support/hash_with_indifferent_access.rb +129 -30
  170. data/lib/active_support/i18n.rb +9 -6
  171. data/lib/active_support/i18n_railtie.rb +50 -14
  172. data/lib/active_support/inflections.rb +13 -11
  173. data/lib/active_support/inflector/inflections.rb +58 -13
  174. data/lib/active_support/inflector/methods.rb +159 -145
  175. data/lib/active_support/inflector/transliterate.rb +84 -34
  176. data/lib/active_support/inflector.rb +7 -5
  177. data/lib/active_support/json/decoding.rb +32 -30
  178. data/lib/active_support/json/encoding.rb +17 -60
  179. data/lib/active_support/json.rb +4 -2
  180. data/lib/active_support/key_generator.rb +11 -43
  181. data/lib/active_support/lazy_load_hooks.rb +53 -20
  182. data/lib/active_support/locale/en.rb +33 -0
  183. data/lib/active_support/locale/en.yml +2 -0
  184. data/lib/active_support/log_subscriber/test_helper.rb +14 -12
  185. data/lib/active_support/log_subscriber.rb +44 -19
  186. data/lib/active_support/logger.rb +9 -23
  187. data/lib/active_support/logger_silence.rb +32 -14
  188. data/lib/active_support/logger_thread_safe_level.rb +32 -8
  189. data/lib/active_support/message_encryptor.rb +166 -53
  190. data/lib/active_support/message_verifier.rb +149 -16
  191. data/lib/active_support/messages/metadata.rb +72 -0
  192. data/lib/active_support/messages/rotation_configuration.rb +22 -0
  193. data/lib/active_support/messages/rotator.rb +56 -0
  194. data/lib/active_support/multibyte/chars.rb +56 -63
  195. data/lib/active_support/multibyte/unicode.rb +56 -290
  196. data/lib/active_support/multibyte.rb +4 -2
  197. data/lib/active_support/notifications/fanout.rb +109 -22
  198. data/lib/active_support/notifications/instrumenter.rb +107 -16
  199. data/lib/active_support/notifications.rb +51 -10
  200. data/lib/active_support/number_helper/number_converter.rb +16 -15
  201. data/lib/active_support/number_helper/number_to_currency_converter.rb +14 -15
  202. data/lib/active_support/number_helper/number_to_delimited_converter.rb +11 -4
  203. data/lib/active_support/number_helper/number_to_human_converter.rb +13 -10
  204. data/lib/active_support/number_helper/number_to_human_size_converter.rb +11 -9
  205. data/lib/active_support/number_helper/number_to_percentage_converter.rb +5 -1
  206. data/lib/active_support/number_helper/number_to_phone_converter.rb +15 -5
  207. data/lib/active_support/number_helper/number_to_rounded_converter.rb +25 -57
  208. data/lib/active_support/number_helper/rounding_helper.rb +66 -0
  209. data/lib/active_support/number_helper.rb +105 -68
  210. data/lib/active_support/option_merger.rb +24 -4
  211. data/lib/active_support/ordered_hash.rb +7 -5
  212. data/lib/active_support/ordered_options.rb +27 -5
  213. data/lib/active_support/parameter_filter.rb +128 -0
  214. data/lib/active_support/per_thread_registry.rb +9 -4
  215. data/lib/active_support/proxy_object.rb +2 -0
  216. data/lib/active_support/rails.rb +10 -8
  217. data/lib/active_support/railtie.rb +43 -9
  218. data/lib/active_support/reloader.rb +130 -0
  219. data/lib/active_support/rescuable.rb +108 -53
  220. data/lib/active_support/security_utils.rb +15 -11
  221. data/lib/active_support/string_inquirer.rb +11 -4
  222. data/lib/active_support/subscriber.rb +74 -30
  223. data/lib/active_support/tagged_logging.rb +25 -13
  224. data/lib/active_support/test_case.rb +107 -44
  225. data/lib/active_support/testing/assertions.rb +151 -20
  226. data/lib/active_support/testing/autorun.rb +4 -2
  227. data/lib/active_support/testing/constant_lookup.rb +2 -1
  228. data/lib/active_support/testing/declarative.rb +3 -1
  229. data/lib/active_support/testing/deprecation.rb +13 -10
  230. data/lib/active_support/testing/file_fixtures.rb +38 -0
  231. data/lib/active_support/testing/isolation.rb +35 -26
  232. data/lib/active_support/testing/method_call_assertions.rb +70 -0
  233. data/lib/active_support/testing/parallelization.rb +134 -0
  234. data/lib/active_support/testing/setup_and_teardown.rb +13 -8
  235. data/lib/active_support/testing/stream.rb +43 -0
  236. data/lib/active_support/testing/tagged_logging.rb +3 -1
  237. data/lib/active_support/testing/time_helpers.rb +84 -20
  238. data/lib/active_support/time.rb +14 -12
  239. data/lib/active_support/time_with_zone.rb +179 -39
  240. data/lib/active_support/values/time_zone.rb +203 -63
  241. data/lib/active_support/version.rb +3 -1
  242. data/lib/active_support/xml_mini/jdom.rb +116 -115
  243. data/lib/active_support/xml_mini/libxml.rb +16 -13
  244. data/lib/active_support/xml_mini/libxmlsax.rb +15 -14
  245. data/lib/active_support/xml_mini/nokogiri.rb +14 -12
  246. data/lib/active_support/xml_mini/nokogirisax.rb +14 -13
  247. data/lib/active_support/xml_mini/rexml.rb +11 -9
  248. data/lib/active_support/xml_mini.rb +38 -46
  249. data/lib/active_support.rb +13 -11
  250. metadata +84 -26
  251. data/lib/active_support/concurrency/latch.rb +0 -27
  252. data/lib/active_support/core_ext/big_decimal/yaml_conversions.rb +0 -16
  253. data/lib/active_support/core_ext/class/delegating_attributes.rb +0 -45
  254. data/lib/active_support/core_ext/date_time/zones.rb +0 -6
  255. data/lib/active_support/core_ext/kernel/agnostics.rb +0 -11
  256. data/lib/active_support/core_ext/kernel/debugger.rb +0 -10
  257. data/lib/active_support/core_ext/module/method_transplanting.rb +0 -13
  258. data/lib/active_support/core_ext/module/qualified_const.rb +0 -52
  259. data/lib/active_support/core_ext/object/itself.rb +0 -15
  260. data/lib/active_support/core_ext/struct.rb +0 -6
  261. data/lib/active_support/core_ext/thread.rb +0 -86
  262. data/lib/active_support/core_ext/time/marshal.rb +0 -30
  263. 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
24
 
21
- log: ->(message, callstack) {
25
+ log: ->(message, callstack, deprecation_horizon, gem_name) {
22
26
  logger =
23
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
36
 
33
- notify: ->(message, callstack) {
34
- ActiveSupport::Notifications.instrument("deprecation.rails",
35
- :message => message, :callstack => callstack)
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
45
 
38
- silence: ->(message, callstack) {},
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 +$stderr+.
55
+ # [+log+] Log all deprecation warnings to +Rails.logger+.
56
+ # [+notify+] Use +ActiveSupport::Notifications+ to notify +deprecation.rails+.
57
+ # [+silence+] Do nothing.
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
@@ -65,12 +85,25 @@ module ActiveSupport
65
85
  # ActiveSupport::Deprecation.behavior = :stderr
66
86
  # ActiveSupport::Deprecation.behavior = [:stderr, :log]
67
87
  # ActiveSupport::Deprecation.behavior = MyCustomHandler
68
- # ActiveSupport::Deprecation.behavior = ->(message, callstack) {
88
+ # ActiveSupport::Deprecation.behavior = ->(message, callstack, deprecation_horizon, gem_name) {
69
89
  # # custom stuff
70
90
  # }
71
91
  def behavior=(behavior)
72
- @behavior = Array(behavior).map { |b| DEFAULT_BEHAVIORS[b] || b }
92
+ @behavior = Array(behavior).map { |b| DEFAULT_BEHAVIORS[b] || arity_coerce(b) }
73
93
  end
94
+
95
+ private
96
+ def arity_coerce(behavior)
97
+ unless behavior.respond_to?(:call)
98
+ raise ArgumentError, "#{behavior.inspect} is not a valid deprecation behavior."
99
+ end
100
+
101
+ if behavior.arity == 4 || behavior.arity == -1
102
+ behavior
103
+ else
104
+ -> message, callstack, _, _ { behavior.call(message, callstack) }
105
+ end
106
+ end
74
107
  end
75
108
  end
76
109
  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
@@ -1,11 +1,14 @@
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/kernel/singleton_class"
4
+ require "active_support/core_ext/module/delegation"
3
5
 
4
6
  module ActiveSupport
5
7
  class Deprecation
6
8
  module InstanceDelegator # :nodoc:
7
9
  def self.included(base)
8
10
  base.extend(ClassMethods)
11
+ base.singleton_class.prepend(OverrideDelegators)
9
12
  base.public_class_method :new
10
13
  end
11
14
 
@@ -19,6 +22,18 @@ module ActiveSupport
19
22
  singleton_class.delegate(method_name, to: :instance)
20
23
  end
21
24
  end
25
+
26
+ module OverrideDelegators # :nodoc:
27
+ def warn(message = nil, callstack = nil)
28
+ callstack ||= caller_locations(2)
29
+ super
30
+ end
31
+
32
+ def deprecation_warning(deprecated_method_name, message = nil, caller_backtrace = nil)
33
+ caller_backtrace ||= caller_locations(2)
34
+ super
35
+ end
36
+ end
22
37
  end
23
38
  end
24
39
  end
@@ -1,43 +1,83 @@
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
+ if target_module.method_defined?(method_name) || target_module.private_method_defined?(method_name)
60
+ method = target_module.instance_method(method_name)
61
+ target_module.module_eval do
62
+ redefine_method(method_name) do |*args, &block|
63
+ deprecator.deprecation_warning(method_name, options[method_name])
64
+ method.bind(self).call(*args, &block)
65
+ end
66
+ ruby2_keywords(method_name) if respond_to?(:ruby2_keywords, true)
67
+ end
68
+ else
69
+ mod ||= Module.new
70
+ mod.module_eval do
71
+ define_method(method_name) do |*args, &block|
72
+ deprecator.deprecation_warning(method_name, options[method_name])
73
+ super(*args, &block)
74
+ end
75
+ ruby2_keywords(method_name) if respond_to?(:ruby2_keywords, true)
38
76
  end
39
77
  end
40
78
  end
79
+
80
+ target_module.prepend(mod) if mod
41
81
  end
42
82
  end
43
83
  end
@@ -1,4 +1,4 @@
1
- require 'active_support/inflector/methods'
1
+ # frozen_string_literal: true
2
2
 
3
3
  module ActiveSupport
4
4
  class Deprecation
@@ -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, **kwargs, &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
+ 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, 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
@@ -1,3 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rbconfig"
4
+
1
5
  module ActiveSupport
2
6
  class Deprecation
3
7
  module Reporting
@@ -14,9 +18,9 @@ module ActiveSupport
14
18
  def warn(message = nil, callstack = nil)
15
19
  return if silenced
16
20
 
17
- callstack ||= caller(2)
21
+ callstack ||= caller_locations(2)
18
22
  deprecation_message(callstack, message).tap do |m|
19
- behavior.each { |b| b.call(m, callstack) }
23
+ behavior.each { |b| b.call(m, callstack, deprecation_horizon, gem_name) }
20
24
  end
21
25
  end
22
26
 
@@ -37,7 +41,7 @@ module ActiveSupport
37
41
  end
38
42
 
39
43
  def deprecation_warning(deprecated_method_name, message = nil, caller_backtrace = nil)
40
- caller_backtrace ||= caller(2)
44
+ caller_backtrace ||= caller_locations(2)
41
45
  deprecated_method_warning(deprecated_method_name, message).tap do |msg|
42
46
  warn(msg, caller_backtrace)
43
47
  end
@@ -46,24 +50,23 @@ module ActiveSupport
46
50
  private
47
51
  # Outputs a deprecation warning message
48
52
  #
49
- # ActiveSupport::Deprecation.deprecated_method_warning(:method_name)
53
+ # deprecated_method_warning(:method_name)
50
54
  # # => "method_name is deprecated and will be removed from Rails #{deprecation_horizon}"
51
- # ActiveSupport::Deprecation.deprecated_method_warning(:method_name, :another_method)
55
+ # deprecated_method_warning(:method_name, :another_method)
52
56
  # # => "method_name is deprecated and will be removed from Rails #{deprecation_horizon} (use another_method instead)"
53
- # ActiveSupport::Deprecation.deprecated_method_warning(:method_name, "Optional message")
57
+ # deprecated_method_warning(:method_name, "Optional message")
54
58
  # # => "method_name is deprecated and will be removed from Rails #{deprecation_horizon} (Optional message)"
55
59
  def deprecated_method_warning(method_name, message = nil)
56
60
  warning = "#{method_name} is deprecated and will be removed from #{gem_name} #{deprecation_horizon}"
57
61
  case message
58
- when Symbol then "#{warning} (use #{message} instead)"
59
- when String then "#{warning} (#{message})"
60
- else warning
62
+ when Symbol then "#{warning} (use #{message} instead)"
63
+ when String then "#{warning} (#{message})"
64
+ else warning
61
65
  end
62
66
  end
63
67
 
64
68
  def deprecation_message(callstack, message = nil)
65
69
  message ||= "You are using deprecated behavior which will be removed from the next major or minor release."
66
- message += '.' unless message =~ /\.$/
67
70
  "DEPRECATION WARNING: #{message} #{deprecation_caller_message(callstack)}"
68
71
  end
69
72
 
@@ -79,8 +82,19 @@ module ActiveSupport
79
82
  end
80
83
 
81
84
  def extract_callstack(callstack)
82
- rails_gem_root = File.expand_path("../../../../..", __FILE__) + "/"
83
- offending_line = callstack.find { |line| !line.start_with?(rails_gem_root) } || callstack.first
85
+ return _extract_callstack(callstack) if callstack.first.is_a? String
86
+
87
+ offending_line = callstack.find { |frame|
88
+ frame.absolute_path && !ignored_callstack(frame.absolute_path)
89
+ } || callstack.first
90
+
91
+ [offending_line.path, offending_line.lineno, offending_line.label]
92
+ end
93
+
94
+ def _extract_callstack(callstack)
95
+ warn "Please pass `caller_locations` to the deprecation API" if $VERBOSE
96
+ offending_line = callstack.find { |line| !ignored_callstack(line) } || callstack.first
97
+
84
98
  if offending_line
85
99
  if md = offending_line.match(/^(.+?):(\d+)(?::in `(.*?)')?/)
86
100
  md.captures
@@ -89,6 +103,12 @@ module ActiveSupport
89
103
  end
90
104
  end
91
105
  end
106
+
107
+ RAILS_GEM_ROOT = File.expand_path("../../../..", __dir__) + "/"
108
+
109
+ def ignored_callstack(path)
110
+ path.start_with?(RAILS_GEM_ROOT) || path.start_with?(RbConfig::CONFIG["rubylibdir"])
111
+ end
92
112
  end
93
113
  end
94
114
  end
@@ -1,4 +1,6 @@
1
- require 'singleton'
1
+ # frozen_string_literal: true
2
+
3
+ require "singleton"
2
4
 
3
5
  module ActiveSupport
4
6
  # \Deprecation specifies the API used by Rails to deprecate methods, instance
@@ -12,12 +14,13 @@ module ActiveSupport
12
14
  # a circular require warning for active_support/deprecation.rb.
13
15
  #
14
16
  # So, we define the constant first, and load dependencies later.
15
- require 'active_support/deprecation/instance_delegator'
16
- require 'active_support/deprecation/behaviors'
17
- require 'active_support/deprecation/reporting'
18
- require 'active_support/deprecation/method_wrappers'
19
- require 'active_support/deprecation/proxy_wrappers'
20
- require 'active_support/core_ext/module/deprecation'
17
+ require "active_support/deprecation/instance_delegator"
18
+ require "active_support/deprecation/behaviors"
19
+ require "active_support/deprecation/reporting"
20
+ require "active_support/deprecation/constant_accessor"
21
+ require "active_support/deprecation/method_wrappers"
22
+ require "active_support/deprecation/proxy_wrappers"
23
+ require "active_support/core_ext/module/deprecation"
21
24
 
22
25
  include Singleton
23
26
  include InstanceDelegator
@@ -29,10 +32,10 @@ module ActiveSupport
29
32
  attr_accessor :deprecation_horizon
30
33
 
31
34
  # It accepts two parameters on initialization. The first is a version of library
32
- # and the second is a library name
35
+ # and the second is a library name.
33
36
  #
34
37
  # ActiveSupport::Deprecation.new('2.0', 'MyLibrary')
35
- def initialize(deprecation_horizon = '5.0', gem_name = 'Rails')
38
+ def initialize(deprecation_horizon = "6.1", gem_name = "Rails")
36
39
  self.gem_name = gem_name
37
40
  self.deprecation_horizon = deprecation_horizon
38
41
  # By default, warnings are not silenced and debugging is off.