activesupport 4.0.13 → 5.2.5

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 (264) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +412 -444
  3. data/MIT-LICENSE +2 -2
  4. data/README.rdoc +8 -4
  5. data/lib/active_support/all.rb +5 -3
  6. data/lib/active_support/array_inquirer.rb +48 -0
  7. data/lib/active_support/backtrace_cleaner.rb +14 -12
  8. data/lib/active_support/benchmarkable.rb +6 -14
  9. data/lib/active_support/builder.rb +3 -1
  10. data/lib/active_support/cache/file_store.rb +67 -51
  11. data/lib/active_support/cache/mem_cache_store.rb +95 -97
  12. data/lib/active_support/cache/memory_store.rb +28 -30
  13. data/lib/active_support/cache/null_store.rb +7 -8
  14. data/lib/active_support/cache/redis_cache_store.rb +466 -0
  15. data/lib/active_support/cache/strategy/local_cache.rb +70 -56
  16. data/lib/active_support/cache/strategy/local_cache_middleware.rb +45 -0
  17. data/lib/active_support/cache.rb +331 -206
  18. data/lib/active_support/callbacks.rb +697 -426
  19. data/lib/active_support/concern.rb +32 -10
  20. data/lib/active_support/concurrency/load_interlock_aware_monitor.rb +17 -0
  21. data/lib/active_support/concurrency/share_lock.rb +227 -0
  22. data/lib/active_support/configurable.rb +8 -5
  23. data/lib/active_support/core_ext/array/access.rb +39 -1
  24. data/lib/active_support/core_ext/array/conversions.rb +24 -35
  25. data/lib/active_support/core_ext/array/extract_options.rb +2 -0
  26. data/lib/active_support/core_ext/array/grouping.rb +23 -13
  27. data/lib/active_support/core_ext/array/inquiry.rb +19 -0
  28. data/lib/active_support/core_ext/array/prepend_and_append.rb +7 -5
  29. data/lib/active_support/core_ext/array/wrap.rb +7 -4
  30. data/lib/active_support/core_ext/array.rb +9 -7
  31. data/lib/active_support/core_ext/benchmark.rb +3 -1
  32. data/lib/active_support/core_ext/big_decimal/conversions.rb +9 -26
  33. data/lib/active_support/core_ext/big_decimal.rb +3 -1
  34. data/lib/active_support/core_ext/class/attribute.rb +41 -23
  35. data/lib/active_support/core_ext/class/attribute_accessors.rb +5 -169
  36. data/lib/active_support/core_ext/class/subclasses.rb +20 -8
  37. data/lib/active_support/core_ext/class.rb +4 -4
  38. data/lib/active_support/core_ext/date/acts_like.rb +3 -1
  39. data/lib/active_support/core_ext/date/blank.rb +14 -0
  40. data/lib/active_support/core_ext/date/calculations.rb +21 -9
  41. data/lib/active_support/core_ext/date/conversions.rb +32 -22
  42. data/lib/active_support/core_ext/date/zones.rb +5 -34
  43. data/lib/active_support/core_ext/date.rb +6 -4
  44. data/lib/active_support/core_ext/date_and_time/calculations.rb +199 -57
  45. data/lib/active_support/core_ext/date_and_time/compatibility.rb +16 -0
  46. data/lib/active_support/core_ext/date_and_time/zones.rb +41 -0
  47. data/lib/active_support/core_ext/date_time/acts_like.rb +4 -2
  48. data/lib/active_support/core_ext/date_time/blank.rb +14 -0
  49. data/lib/active_support/core_ext/date_time/calculations.rb +78 -37
  50. data/lib/active_support/core_ext/date_time/compatibility.rb +18 -0
  51. data/lib/active_support/core_ext/date_time/conversions.rb +19 -13
  52. data/lib/active_support/core_ext/date_time.rb +7 -4
  53. data/lib/active_support/core_ext/digest/uuid.rb +53 -0
  54. data/lib/active_support/core_ext/digest.rb +3 -0
  55. data/lib/active_support/core_ext/enumerable.rb +113 -29
  56. data/lib/active_support/core_ext/file/atomic.rb +38 -31
  57. data/lib/active_support/core_ext/file.rb +3 -1
  58. data/lib/active_support/core_ext/hash/compact.rb +29 -0
  59. data/lib/active_support/core_ext/hash/conversions.rb +71 -49
  60. data/lib/active_support/core_ext/hash/deep_merge.rb +9 -13
  61. data/lib/active_support/core_ext/hash/except.rb +12 -3
  62. data/lib/active_support/core_ext/hash/indifferent_access.rb +5 -3
  63. data/lib/active_support/core_ext/hash/keys.rb +50 -38
  64. data/lib/active_support/core_ext/hash/reverse_merge.rb +5 -2
  65. data/lib/active_support/core_ext/hash/slice.rb +12 -6
  66. data/lib/active_support/core_ext/hash/transform_values.rb +32 -0
  67. data/lib/active_support/core_ext/hash.rb +11 -8
  68. data/lib/active_support/core_ext/integer/inflections.rb +3 -1
  69. data/lib/active_support/core_ext/integer/multiple.rb +2 -0
  70. data/lib/active_support/core_ext/integer/time.rb +11 -33
  71. data/lib/active_support/core_ext/integer.rb +5 -3
  72. data/lib/active_support/core_ext/kernel/agnostics.rb +2 -0
  73. data/lib/active_support/core_ext/kernel/concern.rb +14 -0
  74. data/lib/active_support/core_ext/kernel/reporting.rb +5 -74
  75. data/lib/active_support/core_ext/kernel/singleton_class.rb +2 -0
  76. data/lib/active_support/core_ext/kernel.rb +6 -4
  77. data/lib/active_support/core_ext/load_error.rb +5 -21
  78. data/lib/active_support/core_ext/marshal.rb +13 -10
  79. data/lib/active_support/core_ext/module/aliasing.rb +6 -44
  80. data/lib/active_support/core_ext/module/anonymous.rb +12 -1
  81. data/lib/active_support/core_ext/module/attr_internal.rb +8 -8
  82. data/lib/active_support/core_ext/module/attribute_accessors.rb +170 -21
  83. data/lib/active_support/core_ext/module/attribute_accessors_per_thread.rb +150 -0
  84. data/lib/active_support/core_ext/module/concerning.rb +134 -0
  85. data/lib/active_support/core_ext/module/delegation.rb +135 -45
  86. data/lib/active_support/core_ext/module/deprecation.rb +3 -3
  87. data/lib/active_support/core_ext/module/introspection.rb +9 -25
  88. data/lib/active_support/core_ext/module/reachable.rb +5 -2
  89. data/lib/active_support/core_ext/module/redefine_method.rb +49 -0
  90. data/lib/active_support/core_ext/module/remove_method.rb +8 -3
  91. data/lib/active_support/core_ext/module.rb +14 -10
  92. data/lib/active_support/core_ext/name_error.rb +22 -2
  93. data/lib/active_support/core_ext/numeric/bytes.rb +22 -0
  94. data/lib/active_support/core_ext/numeric/conversions.rb +79 -74
  95. data/lib/active_support/core_ext/numeric/inquiry.rb +28 -0
  96. data/lib/active_support/core_ext/numeric/time.rb +37 -50
  97. data/lib/active_support/core_ext/numeric.rb +6 -3
  98. data/lib/active_support/core_ext/object/acts_like.rb +12 -1
  99. data/lib/active_support/core_ext/object/blank.rb +70 -19
  100. data/lib/active_support/core_ext/object/conversions.rb +6 -4
  101. data/lib/active_support/core_ext/object/deep_dup.rb +19 -10
  102. data/lib/active_support/core_ext/object/duplicable.rb +100 -34
  103. data/lib/active_support/core_ext/object/inclusion.rb +18 -15
  104. data/lib/active_support/core_ext/object/instance_variables.rb +3 -1
  105. data/lib/active_support/core_ext/object/json.rb +227 -0
  106. data/lib/active_support/core_ext/object/to_param.rb +3 -1
  107. data/lib/active_support/core_ext/object/to_query.rb +21 -8
  108. data/lib/active_support/core_ext/object/try.rb +94 -24
  109. data/lib/active_support/core_ext/object/with_options.rb +45 -5
  110. data/lib/active_support/core_ext/object.rb +14 -12
  111. data/lib/active_support/core_ext/range/compare_range.rb +61 -0
  112. data/lib/active_support/core_ext/range/conversions.rb +27 -7
  113. data/lib/active_support/core_ext/range/each.rb +19 -17
  114. data/lib/active_support/core_ext/range/include_range.rb +2 -22
  115. data/lib/active_support/core_ext/range/include_time_with_zone.rb +23 -0
  116. data/lib/active_support/core_ext/range/overlaps.rb +2 -0
  117. data/lib/active_support/core_ext/range.rb +7 -4
  118. data/lib/active_support/core_ext/regexp.rb +6 -0
  119. data/lib/active_support/core_ext/securerandom.rb +25 -0
  120. data/lib/active_support/core_ext/string/access.rb +41 -39
  121. data/lib/active_support/core_ext/string/behavior.rb +3 -1
  122. data/lib/active_support/core_ext/string/conversions.rb +17 -13
  123. data/lib/active_support/core_ext/string/exclude.rb +5 -3
  124. data/lib/active_support/core_ext/string/filters.rb +55 -6
  125. data/lib/active_support/core_ext/string/indent.rb +6 -4
  126. data/lib/active_support/core_ext/string/inflections.rb +66 -24
  127. data/lib/active_support/core_ext/string/inquiry.rb +3 -1
  128. data/lib/active_support/core_ext/string/multibyte.rb +15 -7
  129. data/lib/active_support/core_ext/string/output_safety.rb +114 -54
  130. data/lib/active_support/core_ext/string/starts_ends_with.rb +2 -0
  131. data/lib/active_support/core_ext/string/strip.rb +4 -5
  132. data/lib/active_support/core_ext/string/zones.rb +4 -1
  133. data/lib/active_support/core_ext/string.rb +15 -13
  134. data/lib/active_support/core_ext/time/acts_like.rb +3 -1
  135. data/lib/active_support/core_ext/time/calculations.rb +123 -110
  136. data/lib/active_support/core_ext/time/compatibility.rb +16 -0
  137. data/lib/active_support/core_ext/time/conversions.rb +23 -14
  138. data/lib/active_support/core_ext/time/zones.rb +42 -26
  139. data/lib/active_support/core_ext/time.rb +7 -5
  140. data/lib/active_support/core_ext/uri.rb +6 -8
  141. data/lib/active_support/core_ext.rb +3 -2
  142. data/lib/active_support/current_attributes.rb +195 -0
  143. data/lib/active_support/dependencies/autoload.rb +3 -1
  144. data/lib/active_support/dependencies/interlock.rb +57 -0
  145. data/lib/active_support/dependencies.rb +196 -166
  146. data/lib/active_support/deprecation/behaviors.rb +48 -15
  147. data/lib/active_support/deprecation/constant_accessor.rb +52 -0
  148. data/lib/active_support/deprecation/instance_delegator.rb +17 -2
  149. data/lib/active_support/deprecation/method_wrappers.rb +66 -20
  150. data/lib/active_support/deprecation/proxy_wrappers.rb +56 -28
  151. data/lib/active_support/deprecation/reporting.rb +32 -12
  152. data/lib/active_support/deprecation.rb +14 -11
  153. data/lib/active_support/descendants_tracker.rb +2 -0
  154. data/lib/active_support/digest.rb +20 -0
  155. data/lib/active_support/duration/iso8601_parser.rb +125 -0
  156. data/lib/active_support/duration/iso8601_serializer.rb +55 -0
  157. data/lib/active_support/duration.rb +354 -34
  158. data/lib/active_support/encrypted_configuration.rb +49 -0
  159. data/lib/active_support/encrypted_file.rb +99 -0
  160. data/lib/active_support/evented_file_update_checker.rb +205 -0
  161. data/lib/active_support/execution_wrapper.rb +128 -0
  162. data/lib/active_support/executor.rb +8 -0
  163. data/lib/active_support/file_update_checker.rb +63 -37
  164. data/lib/active_support/gem_version.rb +17 -0
  165. data/lib/active_support/gzip.rb +7 -5
  166. data/lib/active_support/hash_with_indifferent_access.rb +158 -35
  167. data/lib/active_support/i18n.rb +8 -6
  168. data/lib/active_support/i18n_railtie.rb +38 -20
  169. data/lib/active_support/inflections.rb +19 -12
  170. data/lib/active_support/inflector/inflections.rb +79 -30
  171. data/lib/active_support/inflector/methods.rb +197 -129
  172. data/lib/active_support/inflector/transliterate.rb +48 -27
  173. data/lib/active_support/inflector.rb +7 -5
  174. data/lib/active_support/json/decoding.rb +21 -25
  175. data/lib/active_support/json/encoding.rb +84 -292
  176. data/lib/active_support/json.rb +4 -2
  177. data/lib/active_support/key_generator.rb +26 -28
  178. data/lib/active_support/lazy_load_hooks.rb +51 -21
  179. data/lib/active_support/locale/en.yml +2 -0
  180. data/lib/active_support/log_subscriber/test_helper.rb +14 -12
  181. data/lib/active_support/log_subscriber.rb +13 -10
  182. data/lib/active_support/logger.rb +54 -3
  183. data/lib/active_support/logger_silence.rb +12 -7
  184. data/lib/active_support/logger_thread_safe_level.rb +34 -0
  185. data/lib/active_support/message_encryptor.rb +173 -50
  186. data/lib/active_support/message_verifier.rb +159 -22
  187. data/lib/active_support/messages/metadata.rb +71 -0
  188. data/lib/active_support/messages/rotation_configuration.rb +22 -0
  189. data/lib/active_support/messages/rotator.rb +56 -0
  190. data/lib/active_support/multibyte/chars.rb +38 -26
  191. data/lib/active_support/multibyte/unicode.rb +138 -146
  192. data/lib/active_support/multibyte.rb +4 -2
  193. data/lib/active_support/notifications/fanout.rb +23 -16
  194. data/lib/active_support/notifications/instrumenter.rb +29 -8
  195. data/lib/active_support/notifications.rb +22 -13
  196. data/lib/active_support/number_helper/number_converter.rb +184 -0
  197. data/lib/active_support/number_helper/number_to_currency_converter.rb +46 -0
  198. data/lib/active_support/number_helper/number_to_delimited_converter.rb +29 -0
  199. data/lib/active_support/number_helper/number_to_human_converter.rb +68 -0
  200. data/lib/active_support/number_helper/number_to_human_size_converter.rb +59 -0
  201. data/lib/active_support/number_helper/number_to_percentage_converter.rb +14 -0
  202. data/lib/active_support/number_helper/number_to_phone_converter.rb +58 -0
  203. data/lib/active_support/number_helper/number_to_rounded_converter.rb +54 -0
  204. data/lib/active_support/number_helper/rounding_helper.rb +66 -0
  205. data/lib/active_support/number_helper.rb +125 -391
  206. data/lib/active_support/option_merger.rb +3 -1
  207. data/lib/active_support/ordered_hash.rb +6 -4
  208. data/lib/active_support/ordered_options.rb +31 -5
  209. data/lib/active_support/per_thread_registry.rb +19 -11
  210. data/lib/active_support/proxy_object.rb +2 -0
  211. data/lib/active_support/rails.rb +16 -8
  212. data/lib/active_support/railtie.rb +43 -9
  213. data/lib/active_support/reloader.rb +131 -0
  214. data/lib/active_support/rescuable.rb +108 -53
  215. data/lib/active_support/security_utils.rb +31 -0
  216. data/lib/active_support/string_inquirer.rb +11 -3
  217. data/lib/active_support/subscriber.rb +54 -17
  218. data/lib/active_support/tagged_logging.rb +14 -11
  219. data/lib/active_support/test_case.rb +42 -37
  220. data/lib/active_support/testing/assertions.rb +126 -39
  221. data/lib/active_support/testing/autorun.rb +5 -3
  222. data/lib/active_support/testing/constant_lookup.rb +3 -6
  223. data/lib/active_support/testing/declarative.rb +10 -22
  224. data/lib/active_support/testing/deprecation.rb +14 -10
  225. data/lib/active_support/testing/file_fixtures.rb +36 -0
  226. data/lib/active_support/testing/isolation.rb +55 -86
  227. data/lib/active_support/testing/method_call_assertions.rb +43 -0
  228. data/lib/active_support/testing/setup_and_teardown.rb +30 -10
  229. data/lib/active_support/testing/stream.rb +44 -0
  230. data/lib/active_support/testing/tagged_logging.rb +5 -3
  231. data/lib/active_support/testing/time_helpers.rb +200 -0
  232. data/lib/active_support/time.rb +13 -13
  233. data/lib/active_support/time_with_zone.rb +223 -73
  234. data/lib/active_support/values/time_zone.rb +261 -126
  235. data/lib/active_support/values/unicode_tables.dat +0 -0
  236. data/lib/active_support/version.rb +6 -7
  237. data/lib/active_support/xml_mini/jdom.rb +116 -113
  238. data/lib/active_support/xml_mini/libxml.rb +17 -16
  239. data/lib/active_support/xml_mini/libxmlsax.rb +16 -18
  240. data/lib/active_support/xml_mini/nokogiri.rb +15 -15
  241. data/lib/active_support/xml_mini/nokogirisax.rb +15 -16
  242. data/lib/active_support/xml_mini/rexml.rb +17 -16
  243. data/lib/active_support/xml_mini.rb +69 -51
  244. data/lib/active_support.rb +29 -3
  245. metadata +84 -54
  246. data/lib/active_support/basic_object.rb +0 -11
  247. data/lib/active_support/buffered_logger.rb +0 -21
  248. data/lib/active_support/concurrency/latch.rb +0 -27
  249. data/lib/active_support/core_ext/array/uniq_by.rb +0 -19
  250. data/lib/active_support/core_ext/class/delegating_attributes.rb +0 -40
  251. data/lib/active_support/core_ext/date_time/zones.rb +0 -24
  252. data/lib/active_support/core_ext/hash/diff.rb +0 -14
  253. data/lib/active_support/core_ext/kernel/debugger.rb +0 -10
  254. data/lib/active_support/core_ext/logger.rb +0 -67
  255. data/lib/active_support/core_ext/module/qualified_const.rb +0 -52
  256. data/lib/active_support/core_ext/object/to_json.rb +0 -27
  257. data/lib/active_support/core_ext/proc.rb +0 -17
  258. data/lib/active_support/core_ext/string/encoding.rb +0 -8
  259. data/lib/active_support/core_ext/struct.rb +0 -6
  260. data/lib/active_support/core_ext/thread.rb +0 -79
  261. data/lib/active_support/core_ext/time/marshal.rb +0 -30
  262. data/lib/active_support/file_watcher.rb +0 -36
  263. data/lib/active_support/json/variable.rb +0 -18
  264. data/lib/active_support/testing/pending.rb +0 -14
@@ -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 +$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,89 @@
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/module/aliasing"
4
+ require "active_support/core_ext/array/extract_options"
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 = Module.new
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|
59
+ if target_module.method_defined?(method_name) || target_module.private_method_defined?(method_name)
60
+ aliased_method, punctuation = method_name.to_s.sub(/([?!=])$/, ""), $1
61
+ with_method = "#{aliased_method}_with_deprecation#{punctuation}"
62
+ without_method = "#{aliased_method}_without_deprecation#{punctuation}"
63
+
64
+ target_module.send(:define_method, with_method) do |*args, &block|
36
65
  deprecator.deprecation_warning(method_name, options[method_name])
37
- send(:"#{target}_without_deprecation#{punctuation}", *args, &block)
66
+ send(without_method, *args, &block)
67
+ end
68
+
69
+ target_module.send(:alias_method, without_method, method_name)
70
+ target_module.send(:alias_method, method_name, with_method)
71
+
72
+ case
73
+ when target_module.protected_method_defined?(without_method)
74
+ target_module.send(:protected, method_name)
75
+ when target_module.private_method_defined?(without_method)
76
+ target_module.send(:private, method_name)
77
+ end
78
+ else
79
+ mod.send(:define_method, method_name) do |*args, &block|
80
+ deprecator.deprecation_warning(method_name, options[method_name])
81
+ super(*args, &block)
38
82
  end
39
83
  end
40
84
  end
85
+
86
+ target_module.prepend(mod) unless mod.instance_methods(false).empty?
41
87
  end
42
88
  end
43
89
  end
@@ -1,4 +1,6 @@
1
- require 'active_support/inflector/methods'
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/core_ext/regexp"
2
4
 
3
5
  module ActiveSupport
4
6
  class Deprecation
@@ -10,7 +12,7 @@ module ActiveSupport
10
12
  super
11
13
  end
12
14
 
13
- instance_methods.each { |m| undef_method m unless m =~ /^__|^object_id$/ }
15
+ instance_methods.each { |m| undef_method m unless /^__|^object_id$/.match?(m) }
14
16
 
15
17
  # Don't give a deprecation warning on inspect since test/unit and error
16
18
  # logs rely on it for diagnostics.
@@ -20,20 +22,22 @@ module ActiveSupport
20
22
 
21
23
  private
22
24
  def method_missing(called, *args, &block)
23
- warn caller, called, args
25
+ warn caller_locations, called, args
24
26
  target.__send__(called, *args, &block)
25
27
  end
26
28
  end
27
29
 
28
- # This DeprecatedObjectProxy transforms object to deprecated object.
29
- #
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)
30
+ # DeprecatedObjectProxy transforms an object into a deprecated one. It
31
+ # takes an object, a deprecation message and optionally a deprecator. The
32
+ # deprecator defaults to +ActiveSupport::Deprecator+ if none is specified.
32
33
  #
33
- # When someone executes any method except +inspect+ on proxy object this will
34
- # trigger +warn+ method on +deprecator_instance+.
34
+ # deprecated_object = ActiveSupport::Deprecation::DeprecatedObjectProxy.new(Object.new, "This object is now deprecated")
35
+ # # => #<Object:0x007fb9b34c34b0>
35
36
  #
36
- # Default deprecator is <tt>ActiveSupport::Deprecation</tt>
37
+ # deprecated_object.to_s
38
+ # DEPRECATION WARNING: This object is now deprecated.
39
+ # (Backtrace)
40
+ # # => "#<Object:0x007fb9b34c34b0>"
37
41
  class DeprecatedObjectProxy < DeprecationProxy
38
42
  def initialize(object, message, deprecator = ActiveSupport::Deprecation.instance)
39
43
  @object = object
@@ -51,13 +55,16 @@ module ActiveSupport
51
55
  end
52
56
  end
53
57
 
54
- # This DeprecatedInstanceVariableProxy transforms instance variable to
55
- # deprecated instance variable.
58
+ # DeprecatedInstanceVariableProxy transforms an instance variable into a
59
+ # deprecated one. It takes an instance of a class, a method on that class
60
+ # and an instance variable. It optionally takes a deprecator as the last
61
+ # argument. The deprecator defaults to +ActiveSupport::Deprecator+ if none
62
+ # is specified.
56
63
  #
57
64
  # class Example
58
- # def initialize(deprecator)
59
- # @request = ActiveSupport::Deprecation::DeprecatedInstanceVariableProxy.new(self, :request, :@request, deprecator)
60
- # @_request = :a_request
65
+ # def initialize
66
+ # @request = ActiveSupport::Deprecation::DeprecatedInstanceVariableProxy.new(self, :request, :@request)
67
+ # @_request = :special_request
61
68
  # end
62
69
  #
63
70
  # def request
@@ -69,12 +76,17 @@ module ActiveSupport
69
76
  # end
70
77
  # end
71
78
  #
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.
79
+ # example = Example.new
80
+ # # => #<Example:0x007fb9b31090b8 @_request=:special_request, @request=:special_request>
76
81
  #
77
- # Default deprecator is <tt>ActiveSupport::Deprecation</tt>.
82
+ # example.old_request.to_s
83
+ # # => DEPRECATION WARNING: @request is deprecated! Call request.to_s instead of
84
+ # @request.to_s
85
+ # (Backtrace information…)
86
+ # "special_request"
87
+ #
88
+ # example.request.to_s
89
+ # # => "special_request"
78
90
  class DeprecatedInstanceVariableProxy < DeprecationProxy
79
91
  def initialize(instance, method, var = "@#{method}", deprecator = ActiveSupport::Deprecation.instance)
80
92
  @instance = instance
@@ -93,22 +105,38 @@ module ActiveSupport
93
105
  end
94
106
  end
95
107
 
96
- # This DeprecatedConstantProxy transforms constant to deprecated constant.
108
+ # DeprecatedConstantProxy transforms a constant into a deprecated one. It
109
+ # takes the names of an old (deprecated) constant and of a new constant
110
+ # (both in string form) and optionally a deprecator. The deprecator defaults
111
+ # to +ActiveSupport::Deprecator+ if none is specified. The deprecated constant
112
+ # now returns the value of the new one.
113
+ #
114
+ # PLANETS = %w(mercury venus earth mars jupiter saturn uranus neptune pluto)
97
115
  #
98
- # OLD_CONST = ActiveSupport::Deprecation::DeprecatedConstantProxy.new('OLD_CONST', 'NEW_CONST')
99
- # OLD_CONST = ActiveSupport::Deprecation::DeprecatedConstantProxy.new('OLD_CONST', 'NEW_CONST', deprecator_instance)
116
+ # # (In a later update, the original implementation of `PLANETS` has been removed.)
100
117
  #
101
- # When someone use old constant this will trigger +warn+ method on
102
- # +deprecator_instance+.
118
+ # PLANETS_POST_2006 = %w(mercury venus earth mars jupiter saturn uranus neptune)
119
+ # PLANETS = ActiveSupport::Deprecation::DeprecatedConstantProxy.new('PLANETS', 'PLANETS_POST_2006')
103
120
  #
104
- # Default deprecator is <tt>ActiveSupport::Deprecation</tt>.
121
+ # PLANETS.map { |planet| planet.capitalize }
122
+ # # => DEPRECATION WARNING: PLANETS is deprecated! Use PLANETS_POST_2006 instead.
123
+ # (Backtrace information…)
124
+ # ["Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune"]
105
125
  class DeprecatedConstantProxy < DeprecationProxy
106
- def initialize(old_const, new_const, deprecator = ActiveSupport::Deprecation.instance)
126
+ def initialize(old_const, new_const, deprecator = ActiveSupport::Deprecation.instance, message: "#{old_const} is deprecated! Use #{new_const} instead.")
127
+ require "active_support/inflector/methods"
128
+
107
129
  @old_const = old_const
108
130
  @new_const = new_const
109
131
  @deprecator = deprecator
132
+ @message = message
110
133
  end
111
134
 
135
+ # Returns the class of the new constant.
136
+ #
137
+ # PLANETS_POST_2006 = %w(mercury venus earth mars jupiter saturn uranus neptune)
138
+ # PLANETS = ActiveSupport::Deprecation::DeprecatedConstantProxy.new('PLANETS', 'PLANETS_POST_2006')
139
+ # PLANETS.class # => Array
112
140
  def class
113
141
  target.class
114
142
  end
@@ -119,7 +147,7 @@ module ActiveSupport
119
147
  end
120
148
 
121
149
  def warn(callstack, called, args)
122
- @deprecator.warn("#{@old_const} is deprecated! Use #{@new_const} instead.", callstack)
150
+ @deprecator.warn(@message, callstack)
123
151
  end
124
152
  end
125
153
  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
@@ -25,14 +28,14 @@ module ActiveSupport
25
28
  include Reporting
26
29
  include MethodWrapper
27
30
 
28
- # The version the deprecated behavior will be removed, by default.
31
+ # The version number in which the deprecated behavior will be removed, by default.
29
32
  attr_accessor :deprecation_horizon
30
33
 
31
- # It accepts two parameters on initialization. The first is an version of library
32
- # and the second is an library name
34
+ # It accepts two parameters on initialization. The first is a version of library
35
+ # and the second is a library name.
33
36
  #
34
37
  # ActiveSupport::Deprecation.new('2.0', 'MyLibrary')
35
- def initialize(deprecation_horizon = '4.1', gem_name = 'Rails')
38
+ def initialize(deprecation_horizon = "6.0", 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.
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveSupport
2
4
  # This module provides an internal implementation to track descendants
3
5
  # which is faster than iterating through ObjectSpace.
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveSupport
4
+ class Digest #:nodoc:
5
+ class <<self
6
+ def hash_digest_class
7
+ @hash_digest_class ||= ::Digest::MD5
8
+ end
9
+
10
+ def hash_digest_class=(klass)
11
+ raise ArgumentError, "#{klass} is expected to implement hexdigest class method" unless klass.respond_to?(:hexdigest)
12
+ @hash_digest_class = klass
13
+ end
14
+
15
+ def hexdigest(arg)
16
+ hash_digest_class.hexdigest(arg)[0...32]
17
+ end
18
+ end
19
+ end
20
+ end