activesupport 3.1.0 → 5.0.0

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 (276) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +798 -0
  3. data/MIT-LICENSE +20 -0
  4. data/README.rdoc +13 -7
  5. data/lib/active_support/array_inquirer.rb +44 -0
  6. data/lib/active_support/backtrace_cleaner.rb +38 -34
  7. data/lib/active_support/benchmarkable.rb +17 -28
  8. data/lib/active_support/cache/file_store.rb +85 -70
  9. data/lib/active_support/cache/mem_cache_store.rb +75 -66
  10. data/lib/active_support/cache/memory_store.rb +31 -23
  11. data/lib/active_support/cache/null_store.rb +41 -0
  12. data/lib/active_support/cache/strategy/local_cache.rb +73 -70
  13. data/lib/active_support/cache/strategy/local_cache_middleware.rb +44 -0
  14. data/lib/active_support/cache.rb +360 -294
  15. data/lib/active_support/callbacks.rb +563 -393
  16. data/lib/active_support/concern.rb +42 -34
  17. data/lib/active_support/concurrency/latch.rb +19 -0
  18. data/lib/active_support/concurrency/share_lock.rb +186 -0
  19. data/lib/active_support/configurable.rb +70 -12
  20. data/lib/active_support/core_ext/array/access.rb +53 -9
  21. data/lib/active_support/core_ext/array/conversions.rb +109 -62
  22. data/lib/active_support/core_ext/array/extract_options.rb +2 -2
  23. data/lib/active_support/core_ext/array/grouping.rb +39 -32
  24. data/lib/active_support/core_ext/array/inquiry.rb +17 -0
  25. data/lib/active_support/core_ext/array/prepend_and_append.rb +7 -0
  26. data/lib/active_support/core_ext/array/wrap.rb +16 -18
  27. data/lib/active_support/core_ext/array.rb +2 -2
  28. data/lib/active_support/core_ext/benchmark.rb +7 -0
  29. data/lib/active_support/core_ext/big_decimal/conversions.rb +8 -36
  30. data/lib/active_support/core_ext/class/attribute.rb +47 -34
  31. data/lib/active_support/core_ext/class/attribute_accessors.rb +4 -79
  32. data/lib/active_support/core_ext/class/subclasses.rb +12 -7
  33. data/lib/active_support/core_ext/class.rb +0 -3
  34. data/lib/active_support/core_ext/date/blank.rb +12 -0
  35. data/lib/active_support/core_ext/date/calculations.rb +57 -167
  36. data/lib/active_support/core_ext/date/conversions.rb +31 -42
  37. data/lib/active_support/core_ext/date/zones.rb +2 -10
  38. data/lib/active_support/core_ext/date.rb +5 -0
  39. data/lib/active_support/core_ext/date_and_time/calculations.rb +335 -0
  40. data/lib/active_support/core_ext/date_and_time/compatibility.rb +18 -0
  41. data/lib/active_support/core_ext/date_and_time/zones.rb +40 -0
  42. data/lib/active_support/core_ext/date_time/acts_like.rb +1 -0
  43. data/lib/active_support/core_ext/date_time/blank.rb +12 -0
  44. data/lib/active_support/core_ext/date_time/calculations.rb +132 -65
  45. data/lib/active_support/core_ext/date_time/compatibility.rb +5 -0
  46. data/lib/active_support/core_ext/date_time/conversions.rb +36 -34
  47. data/lib/active_support/core_ext/date_time.rb +5 -0
  48. data/lib/active_support/core_ext/digest/uuid.rb +51 -0
  49. data/lib/active_support/core_ext/enumerable.rb +81 -74
  50. data/lib/active_support/core_ext/file/atomic.rb +53 -26
  51. data/lib/active_support/core_ext/file.rb +0 -1
  52. data/lib/active_support/core_ext/hash/compact.rb +20 -0
  53. data/lib/active_support/core_ext/hash/conversions.rb +175 -70
  54. data/lib/active_support/core_ext/hash/deep_merge.rb +30 -8
  55. data/lib/active_support/core_ext/hash/except.rb +11 -12
  56. data/lib/active_support/core_ext/hash/indifferent_access.rb +7 -8
  57. data/lib/active_support/core_ext/hash/keys.rb +147 -24
  58. data/lib/active_support/core_ext/hash/reverse_merge.rb +2 -3
  59. data/lib/active_support/core_ext/hash/slice.rb +22 -14
  60. data/lib/active_support/core_ext/hash/transform_values.rb +29 -0
  61. data/lib/active_support/core_ext/hash.rb +2 -2
  62. data/lib/active_support/core_ext/integer/inflections.rb +13 -1
  63. data/lib/active_support/core_ext/integer/multiple.rb +4 -0
  64. data/lib/active_support/core_ext/integer/time.rb +12 -22
  65. data/lib/active_support/core_ext/kernel/agnostics.rb +2 -2
  66. data/lib/active_support/core_ext/kernel/concern.rb +12 -0
  67. data/lib/active_support/core_ext/kernel/debugger.rb +2 -15
  68. data/lib/active_support/core_ext/kernel/reporting.rb +12 -62
  69. data/lib/active_support/core_ext/kernel/singleton_class.rb +0 -7
  70. data/lib/active_support/core_ext/kernel.rb +2 -3
  71. data/lib/active_support/core_ext/load_error.rb +14 -7
  72. data/lib/active_support/core_ext/marshal.rb +22 -0
  73. data/lib/active_support/core_ext/module/aliasing.rb +16 -12
  74. data/lib/active_support/core_ext/module/anonymous.rb +12 -8
  75. data/lib/active_support/core_ext/module/attr_internal.rb +2 -5
  76. data/lib/active_support/core_ext/module/attribute_accessors.rb +165 -13
  77. data/lib/active_support/core_ext/module/attribute_accessors_per_thread.rb +141 -0
  78. data/lib/active_support/core_ext/module/concerning.rb +135 -0
  79. data/lib/active_support/core_ext/module/delegation.rb +141 -68
  80. data/lib/active_support/core_ext/module/deprecation.rb +17 -3
  81. data/lib/active_support/core_ext/module/introspection.rb +9 -31
  82. data/lib/active_support/core_ext/module/method_transplanting.rb +3 -0
  83. data/lib/active_support/core_ext/module/qualified_const.rb +70 -0
  84. data/lib/active_support/core_ext/module/reachable.rb +1 -3
  85. data/lib/active_support/core_ext/module/remove_method.rb +24 -5
  86. data/lib/active_support/core_ext/module.rb +3 -3
  87. data/lib/active_support/core_ext/name_error.rb +15 -2
  88. data/lib/active_support/core_ext/numeric/bytes.rb +20 -0
  89. data/lib/active_support/core_ext/numeric/conversions.rb +145 -0
  90. data/lib/active_support/core_ext/numeric/inquiry.rb +26 -0
  91. data/lib/active_support/core_ext/numeric/time.rb +31 -36
  92. data/lib/active_support/core_ext/numeric.rb +2 -0
  93. data/lib/active_support/core_ext/object/acts_like.rb +4 -4
  94. data/lib/active_support/core_ext/object/blank.rb +52 -18
  95. data/lib/active_support/core_ext/object/deep_dup.rb +53 -0
  96. data/lib/active_support/core_ext/object/duplicable.rb +12 -20
  97. data/lib/active_support/core_ext/object/inclusion.rb +13 -1
  98. data/lib/active_support/core_ext/object/instance_variables.rb +7 -12
  99. data/lib/active_support/core_ext/object/json.rb +205 -0
  100. data/lib/active_support/core_ext/object/to_param.rb +1 -55
  101. data/lib/active_support/core_ext/object/to_query.rb +66 -9
  102. data/lib/active_support/core_ext/object/try.rb +124 -33
  103. data/lib/active_support/core_ext/object/with_options.rb +37 -11
  104. data/lib/active_support/core_ext/object.rb +2 -1
  105. data/lib/active_support/core_ext/range/conversions.rb +17 -7
  106. data/lib/active_support/core_ext/range/each.rb +21 -0
  107. data/lib/active_support/core_ext/range/include_range.rb +20 -18
  108. data/lib/active_support/core_ext/range/overlaps.rb +1 -1
  109. data/lib/active_support/core_ext/range.rb +1 -2
  110. data/lib/active_support/core_ext/securerandom.rb +23 -0
  111. data/lib/active_support/core_ext/string/access.rb +95 -90
  112. data/lib/active_support/core_ext/string/behavior.rb +1 -1
  113. data/lib/active_support/core_ext/string/conversions.rb +41 -38
  114. data/lib/active_support/core_ext/string/exclude.rb +6 -1
  115. data/lib/active_support/core_ext/string/filters.rb +70 -17
  116. data/lib/active_support/core_ext/string/indent.rb +43 -0
  117. data/lib/active_support/core_ext/string/inflections.rb +139 -59
  118. data/lib/active_support/core_ext/string/inquiry.rb +2 -2
  119. data/lib/active_support/core_ext/string/multibyte.rb +46 -65
  120. data/lib/active_support/core_ext/string/output_safety.rb +153 -56
  121. data/lib/active_support/core_ext/string/strip.rb +3 -6
  122. data/lib/active_support/core_ext/string/zones.rb +14 -0
  123. data/lib/active_support/core_ext/string.rb +2 -3
  124. data/lib/active_support/core_ext/struct.rb +3 -0
  125. data/lib/active_support/core_ext/time/calculations.rb +173 -173
  126. data/lib/active_support/core_ext/time/compatibility.rb +5 -0
  127. data/lib/active_support/core_ext/time/conversions.rb +33 -29
  128. data/lib/active_support/core_ext/time/marshal.rb +2 -56
  129. data/lib/active_support/core_ext/time/zones.rb +57 -32
  130. data/lib/active_support/core_ext/time.rb +5 -0
  131. data/lib/active_support/core_ext/uri.rb +13 -19
  132. data/lib/active_support/core_ext.rb +3 -2
  133. data/lib/active_support/dependencies/autoload.rb +47 -20
  134. data/lib/active_support/dependencies/interlock.rb +51 -0
  135. data/lib/active_support/dependencies.rb +315 -265
  136. data/lib/active_support/deprecation/behaviors.rb +71 -30
  137. data/lib/active_support/deprecation/instance_delegator.rb +24 -0
  138. data/lib/active_support/deprecation/method_wrappers.rb +59 -18
  139. data/lib/active_support/deprecation/proxy_wrappers.rb +82 -14
  140. data/lib/active_support/deprecation/reporting.rb +61 -14
  141. data/lib/active_support/deprecation.rb +38 -13
  142. data/lib/active_support/descendants_tracker.rb +34 -19
  143. data/lib/active_support/duration/iso8601_parser.rb +122 -0
  144. data/lib/active_support/duration/iso8601_serializer.rb +51 -0
  145. data/lib/active_support/duration.rb +85 -14
  146. data/lib/active_support/evented_file_update_checker.rb +194 -0
  147. data/lib/active_support/execution_wrapper.rb +117 -0
  148. data/lib/active_support/executor.rb +6 -0
  149. data/lib/active_support/file_update_checker.rb +138 -17
  150. data/lib/active_support/gem_version.rb +15 -0
  151. data/lib/active_support/gzip.rb +11 -5
  152. data/lib/active_support/hash_with_indifferent_access.rb +199 -49
  153. data/lib/active_support/i18n.rb +6 -2
  154. data/lib/active_support/i18n_railtie.rb +40 -21
  155. data/lib/active_support/inflections.rb +22 -13
  156. data/lib/active_support/inflector/inflections.rb +175 -144
  157. data/lib/active_support/inflector/methods.rb +328 -91
  158. data/lib/active_support/inflector/transliterate.rb +51 -37
  159. data/lib/active_support/json/decoding.rb +31 -22
  160. data/lib/active_support/json/encoding.rb +88 -248
  161. data/lib/active_support/key_generator.rb +71 -0
  162. data/lib/active_support/lazy_load_hooks.rb +27 -25
  163. data/lib/active_support/locale/en.yml +102 -3
  164. data/lib/active_support/log_subscriber/test_helper.rb +24 -21
  165. data/lib/active_support/log_subscriber.rb +36 -49
  166. data/lib/active_support/logger.rb +106 -0
  167. data/lib/active_support/logger_silence.rb +28 -0
  168. data/lib/active_support/logger_thread_safe_level.rb +31 -0
  169. data/lib/active_support/message_encryptor.rb +72 -36
  170. data/lib/active_support/message_verifier.rb +96 -24
  171. data/lib/active_support/multibyte/chars.rb +88 -333
  172. data/lib/active_support/multibyte/unicode.rb +156 -136
  173. data/lib/active_support/multibyte.rb +5 -28
  174. data/lib/active_support/notifications/fanout.rb +115 -19
  175. data/lib/active_support/notifications/instrumenter.rb +52 -15
  176. data/lib/active_support/notifications.rb +168 -33
  177. data/lib/active_support/number_helper/number_converter.rb +182 -0
  178. data/lib/active_support/number_helper/number_to_currency_converter.rb +44 -0
  179. data/lib/active_support/number_helper/number_to_delimited_converter.rb +28 -0
  180. data/lib/active_support/number_helper/number_to_human_converter.rb +68 -0
  181. data/lib/active_support/number_helper/number_to_human_size_converter.rb +62 -0
  182. data/lib/active_support/number_helper/number_to_percentage_converter.rb +12 -0
  183. data/lib/active_support/number_helper/number_to_phone_converter.rb +58 -0
  184. data/lib/active_support/number_helper/number_to_rounded_converter.rb +92 -0
  185. data/lib/active_support/number_helper.rb +368 -0
  186. data/lib/active_support/option_merger.rb +1 -1
  187. data/lib/active_support/ordered_hash.rb +18 -183
  188. data/lib/active_support/ordered_options.rb +44 -24
  189. data/lib/active_support/per_thread_registry.rb +58 -0
  190. data/lib/active_support/proxy_object.rb +13 -0
  191. data/lib/active_support/rails.rb +27 -0
  192. data/lib/active_support/railtie.rb +25 -34
  193. data/lib/active_support/reloader.rb +129 -0
  194. data/lib/active_support/rescuable.rb +98 -48
  195. data/lib/active_support/security_utils.rb +27 -0
  196. data/lib/active_support/string_inquirer.rb +14 -9
  197. data/lib/active_support/subscriber.rb +120 -0
  198. data/lib/active_support/tagged_logging.rb +78 -0
  199. data/lib/active_support/test_case.rb +69 -17
  200. data/lib/active_support/testing/assertions.rb +43 -41
  201. data/lib/active_support/testing/autorun.rb +12 -0
  202. data/lib/active_support/testing/constant_lookup.rb +50 -0
  203. data/lib/active_support/testing/declarative.rb +7 -21
  204. data/lib/active_support/testing/deprecation.rb +14 -33
  205. data/lib/active_support/testing/file_fixtures.rb +34 -0
  206. data/lib/active_support/testing/isolation.rb +53 -95
  207. data/lib/active_support/testing/method_call_assertions.rb +41 -0
  208. data/lib/active_support/testing/setup_and_teardown.rb +21 -82
  209. data/lib/active_support/testing/stream.rb +42 -0
  210. data/lib/active_support/testing/tagged_logging.rb +25 -0
  211. data/lib/active_support/testing/time_helpers.rb +134 -0
  212. data/lib/active_support/time.rb +6 -23
  213. data/lib/active_support/time_with_zone.rb +239 -92
  214. data/lib/active_support/values/time_zone.rb +236 -160
  215. data/lib/active_support/values/unicode_tables.dat +0 -0
  216. data/lib/active_support/version.rb +5 -7
  217. data/lib/active_support/xml_mini/jdom.rb +19 -13
  218. data/lib/active_support/xml_mini/libxml.rb +3 -4
  219. data/lib/active_support/xml_mini/libxmlsax.rb +2 -3
  220. data/lib/active_support/xml_mini/nokogiri.rb +3 -4
  221. data/lib/active_support/xml_mini/nokogirisax.rb +2 -3
  222. data/lib/active_support/xml_mini/rexml.rb +8 -10
  223. data/lib/active_support/xml_mini.rb +66 -34
  224. data/lib/active_support.rb +40 -23
  225. metadata +185 -134
  226. data/CHANGELOG +0 -1534
  227. data/lib/active_support/base64.rb +0 -42
  228. data/lib/active_support/basic_object.rb +0 -21
  229. data/lib/active_support/buffered_logger.rb +0 -137
  230. data/lib/active_support/cache/compressed_mem_cache_store.rb +0 -13
  231. data/lib/active_support/cache/synchronized_memory_store.rb +0 -11
  232. data/lib/active_support/core_ext/array/random_access.rb +0 -30
  233. data/lib/active_support/core_ext/array/uniq_by.rb +0 -16
  234. data/lib/active_support/core_ext/class/delegating_attributes.rb +0 -44
  235. data/lib/active_support/core_ext/class/inheritable_attributes.rb +0 -178
  236. data/lib/active_support/core_ext/date/freeze.rb +0 -31
  237. data/lib/active_support/core_ext/date_time/zones.rb +0 -21
  238. data/lib/active_support/core_ext/exception.rb +0 -3
  239. data/lib/active_support/core_ext/file/path.rb +0 -5
  240. data/lib/active_support/core_ext/float/rounding.rb +0 -19
  241. data/lib/active_support/core_ext/float.rb +0 -1
  242. data/lib/active_support/core_ext/hash/deep_dup.rb +0 -11
  243. data/lib/active_support/core_ext/hash/diff.rb +0 -13
  244. data/lib/active_support/core_ext/kernel/requires.rb +0 -28
  245. data/lib/active_support/core_ext/logger.rb +0 -81
  246. data/lib/active_support/core_ext/module/attr_accessor_with_default.rb +0 -31
  247. data/lib/active_support/core_ext/module/method_names.rb +0 -14
  248. data/lib/active_support/core_ext/module/synchronization.rb +0 -43
  249. data/lib/active_support/core_ext/object/to_json.rb +0 -19
  250. data/lib/active_support/core_ext/proc.rb +0 -14
  251. data/lib/active_support/core_ext/process/daemon.rb +0 -23
  252. data/lib/active_support/core_ext/process.rb +0 -1
  253. data/lib/active_support/core_ext/range/blockless_step.rb +0 -29
  254. data/lib/active_support/core_ext/range/cover.rb +0 -3
  255. data/lib/active_support/core_ext/rexml.rb +0 -46
  256. data/lib/active_support/core_ext/string/encoding.rb +0 -11
  257. data/lib/active_support/core_ext/string/interpolation.rb +0 -2
  258. data/lib/active_support/core_ext/string/xchar.rb +0 -18
  259. data/lib/active_support/core_ext/time/publicize_conversion_methods.rb +0 -10
  260. data/lib/active_support/file_watcher.rb +0 -36
  261. data/lib/active_support/json/variable.rb +0 -9
  262. data/lib/active_support/memoizable.rb +0 -105
  263. data/lib/active_support/multibyte/exceptions.rb +0 -8
  264. data/lib/active_support/multibyte/utils.rb +0 -60
  265. data/lib/active_support/ruby/shim.rb +0 -22
  266. data/lib/active_support/secure_random.rb +0 -6
  267. data/lib/active_support/testing/mochaing.rb +0 -7
  268. data/lib/active_support/testing/pending.rb +0 -52
  269. data/lib/active_support/testing/performance/jruby.rb +0 -115
  270. data/lib/active_support/testing/performance/rubinius.rb +0 -113
  271. data/lib/active_support/testing/performance/ruby/mri.rb +0 -57
  272. data/lib/active_support/testing/performance/ruby/yarv.rb +0 -57
  273. data/lib/active_support/testing/performance/ruby.rb +0 -152
  274. data/lib/active_support/testing/performance.rb +0 -317
  275. data/lib/active_support/time/autoload.rb +0 -5
  276. data/lib/active_support/whiny_nil.rb +0 -60
@@ -1,49 +1,90 @@
1
1
  require "active_support/notifications"
2
- require "active_support/core_ext/array/wrap"
3
2
 
4
3
  module ActiveSupport
5
- module Deprecation
6
- class << self
4
+ # Raised when <tt>ActiveSupport::Deprecation::Behavior#behavior</tt> is set with <tt>:raise</tt>.
5
+ # You would set <tt>:raise</tt>, as a behaviour to raise errors and proactively report exceptions from deprecations.
6
+ class DeprecationException < StandardError
7
+ end
8
+
9
+ class Deprecation
10
+ # Default warning behaviors per Rails.env.
11
+ DEFAULT_BEHAVIORS = {
12
+ raise: ->(message, callstack) {
13
+ e = DeprecationException.new(message)
14
+ e.set_backtrace(callstack.map(&:to_s))
15
+ raise e
16
+ },
17
+
18
+ stderr: ->(message, callstack) {
19
+ $stderr.puts(message)
20
+ $stderr.puts callstack.join("\n ") if debug
21
+ },
22
+
23
+ log: ->(message, callstack) {
24
+ logger =
25
+ if defined?(Rails.logger) && Rails.logger
26
+ Rails.logger
27
+ else
28
+ require 'active_support/logger'
29
+ ActiveSupport::Logger.new($stderr)
30
+ end
31
+ logger.warn message
32
+ logger.debug callstack.join("\n ") if debug
33
+ },
34
+
35
+ notify: ->(message, callstack) {
36
+ ActiveSupport::Notifications.instrument("deprecation.rails",
37
+ :message => message, :callstack => callstack)
38
+ },
39
+
40
+ silence: ->(message, callstack) {},
41
+ }
42
+
43
+ # Behavior module allows to determine how to display deprecation messages.
44
+ # You can create a custom behavior or set any from the +DEFAULT_BEHAVIORS+
45
+ # constant. Available behaviors are:
46
+ #
47
+ # [+raise+] Raise <tt>ActiveSupport::DeprecationException</tt>.
48
+ # [+stderr+] Log all deprecation warnings to +$stderr+.
49
+ # [+log+] Log all deprecation warnings to +Rails.logger+.
50
+ # [+notify+] Use +ActiveSupport::Notifications+ to notify +deprecation.rails+.
51
+ # [+silence+] Do nothing.
52
+ #
53
+ # Setting behaviors only affects deprecations that happen after boot time.
54
+ # For more information you can read the documentation of the +behavior=+ method.
55
+ module Behavior
7
56
  # Whether to print a backtrace along with the warning.
8
57
  attr_accessor :debug
9
58
 
10
- # Returns the set behavior or if one isn't set, defaults to +:stderr+
59
+ # Returns the current behavior or if one isn't set, defaults to +:stderr+.
11
60
  def behavior
12
61
  @behavior ||= [DEFAULT_BEHAVIORS[:stderr]]
13
62
  end
14
63
 
15
- # Sets the behavior to the specified value. Can be a single value or an array.
64
+ # Sets the behavior to the specified value. Can be a single value, array,
65
+ # or an object that responds to +call+.
66
+ #
67
+ # Available behaviors:
68
+ #
69
+ # [+raise+] Raise <tt>ActiveSupport::DeprecationException</tt>.
70
+ # [+stderr+] Log all deprecation warnings to +$stderr+.
71
+ # [+log+] Log all deprecation warnings to +Rails.logger+.
72
+ # [+notify+] Use +ActiveSupport::Notifications+ to notify +deprecation.rails+.
73
+ # [+silence+] Do nothing.
16
74
  #
17
- # Examples
75
+ # Setting behaviors only affects deprecations that happen after boot time.
76
+ # Deprecation warnings raised by gems are not affected by this setting
77
+ # because they happen before Rails boots up.
18
78
  #
19
79
  # ActiveSupport::Deprecation.behavior = :stderr
20
80
  # ActiveSupport::Deprecation.behavior = [:stderr, :log]
81
+ # ActiveSupport::Deprecation.behavior = MyCustomHandler
82
+ # ActiveSupport::Deprecation.behavior = ->(message, callstack) {
83
+ # # custom stuff
84
+ # }
21
85
  def behavior=(behavior)
22
- @behavior = Array.wrap(behavior).map { |b| DEFAULT_BEHAVIORS[b] || b }
86
+ @behavior = Array(behavior).map { |b| DEFAULT_BEHAVIORS[b] || b }
23
87
  end
24
88
  end
25
-
26
- # Default warning behaviors per Rails.env.
27
- DEFAULT_BEHAVIORS = {
28
- :stderr => Proc.new { |message, callstack|
29
- $stderr.puts(message)
30
- $stderr.puts callstack.join("\n ") if debug
31
- },
32
- :log => Proc.new { |message, callstack|
33
- logger =
34
- if defined?(Rails) && Rails.logger
35
- Rails.logger
36
- else
37
- require 'logger'
38
- Logger.new($stderr)
39
- end
40
- logger.warn message
41
- logger.debug callstack.join("\n ") if debug
42
- },
43
- :notify => Proc.new { |message, callstack|
44
- ActiveSupport::Notifications.instrument("deprecation.rails",
45
- :message => message, :callstack => callstack)
46
- }
47
- }
48
89
  end
49
90
  end
@@ -0,0 +1,24 @@
1
+ require 'active_support/core_ext/kernel/singleton_class'
2
+ require 'active_support/core_ext/module/delegation'
3
+
4
+ module ActiveSupport
5
+ class Deprecation
6
+ module InstanceDelegator # :nodoc:
7
+ def self.included(base)
8
+ base.extend(ClassMethods)
9
+ base.public_class_method :new
10
+ end
11
+
12
+ module ClassMethods # :nodoc:
13
+ def include(included_module)
14
+ included_module.instance_methods.each { |m| method_added(m) }
15
+ super
16
+ end
17
+
18
+ def method_added(method_name)
19
+ singleton_class.delegate(method_name, to: :instance)
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -1,28 +1,69 @@
1
- require 'active_support/core_ext/module/deprecation'
2
1
  require 'active_support/core_ext/module/aliasing'
3
2
  require 'active_support/core_ext/array/extract_options'
4
3
 
5
4
  module ActiveSupport
6
- class << Deprecation
7
- # Declare that a method has been deprecated.
8
- def deprecate_methods(target_module, *method_names)
9
- options = method_names.extract_options!
10
- method_names += options.keys
5
+ class Deprecation
6
+ module MethodWrapper
7
+ # Declare that a method has been deprecated.
8
+ #
9
+ # module Fred
10
+ # extend self
11
+ #
12
+ # def aaa; end
13
+ # def bbb; end
14
+ # def ccc; end
15
+ # def ddd; end
16
+ # def eee; end
17
+ # end
18
+ #
19
+ # Using the default deprecator:
20
+ # ActiveSupport::Deprecation.deprecate_methods(Fred, :aaa, bbb: :zzz, ccc: 'use Bar#ccc instead')
21
+ # # => [:aaa, :bbb, :ccc]
22
+ #
23
+ # Fred.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.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
30
+ #
31
+ # Fred.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
34
+ #
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]
39
+ #
40
+ # Fred.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.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
52
+ def deprecate_methods(target_module, *method_names)
53
+ options = method_names.extract_options!
54
+ deprecator = options.delete(:deprecator) || self
55
+ method_names += options.keys
11
56
 
12
- method_names.each do |method_name|
13
- target_module.alias_method_chain(method_name, :deprecation) do |target, punctuation|
14
- target_module.module_eval(<<-end_eval, __FILE__, __LINE__ + 1)
15
- def #{target}_with_deprecation#{punctuation}(*args, &block)
16
- ::ActiveSupport::Deprecation.warn(
17
- ::ActiveSupport::Deprecation.deprecated_method_warning(
18
- :#{method_name},
19
- #{options[method_name].inspect}),
20
- caller
21
- )
22
- send(:#{target}_without_deprecation#{punctuation}, *args, &block)
57
+ mod = Module.new do
58
+ method_names.each do |method_name|
59
+ define_method(method_name) do |*args, &block|
60
+ deprecator.deprecation_warning(method_name, options[method_name])
61
+ super(*args, &block)
23
62
  end
24
- end_eval
63
+ end
25
64
  end
65
+
66
+ target_module.prepend(mod)
26
67
  end
27
68
  end
28
69
  end
@@ -1,7 +1,7 @@
1
1
  require 'active_support/inflector/methods'
2
2
 
3
3
  module ActiveSupport
4
- module Deprecation
4
+ class Deprecation
5
5
  class DeprecationProxy #:nodoc:
6
6
  def self.new(*args, &block)
7
7
  object = args.first
@@ -20,15 +20,27 @@ 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
- class DeprecatedObjectProxy < DeprecationProxy #:nodoc:
29
- def initialize(object, message)
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.
31
+ #
32
+ # deprecated_object = ActiveSupport::Deprecation::DeprecatedObjectProxy.new(Object.new, "This object is now deprecated")
33
+ # # => #<Object:0x007fb9b34c34b0>
34
+ #
35
+ # deprecated_object.to_s
36
+ # DEPRECATION WARNING: This object is now deprecated.
37
+ # (Backtrace)
38
+ # # => "#<Object:0x007fb9b34c34b0>"
39
+ class DeprecatedObjectProxy < DeprecationProxy
40
+ def initialize(object, message, deprecator = ActiveSupport::Deprecation.instance)
30
41
  @object = object
31
42
  @message = message
43
+ @deprecator = deprecator
32
44
  end
33
45
 
34
46
  private
@@ -37,15 +49,48 @@ module ActiveSupport
37
49
  end
38
50
 
39
51
  def warn(callstack, called, args)
40
- ActiveSupport::Deprecation.warn(@message, callstack)
52
+ @deprecator.warn(@message, callstack)
41
53
  end
42
54
  end
43
55
 
44
- # Stand-in for <tt>@request</tt>, <tt>@attributes</tt>, <tt>@params</tt>, etc.
45
- # which emits deprecation warnings on any method call (except +inspect+).
46
- class DeprecatedInstanceVariableProxy < DeprecationProxy #:nodoc:
47
- def initialize(instance, method, var = "@#{method}")
48
- @instance, @method, @var = instance, method, var
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.
61
+ #
62
+ # class Example
63
+ # def initialize
64
+ # @request = ActiveSupport::Deprecation::DeprecatedInstanceVariableProxy.new(self, :request, :@request)
65
+ # @_request = :special_request
66
+ # end
67
+ #
68
+ # def request
69
+ # @_request
70
+ # end
71
+ #
72
+ # def old_request
73
+ # @request
74
+ # end
75
+ # end
76
+ #
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"
85
+ #
86
+ # example.request.to_s
87
+ # # => "special_request"
88
+ class DeprecatedInstanceVariableProxy < DeprecationProxy
89
+ def initialize(instance, method, var = "@#{method}", deprecator = ActiveSupport::Deprecation.instance)
90
+ @instance = instance
91
+ @method = method
92
+ @var = var
93
+ @deprecator = deprecator
49
94
  end
50
95
 
51
96
  private
@@ -54,16 +99,39 @@ module ActiveSupport
54
99
  end
55
100
 
56
101
  def warn(callstack, called, args)
57
- ActiveSupport::Deprecation.warn("#{@var} is deprecated! Call #{@method}.#{called} instead of #{@var}.#{called}. Args: #{args.inspect}", callstack)
102
+ @deprecator.warn("#{@var} is deprecated! Call #{@method}.#{called} instead of #{@var}.#{called}. Args: #{args.inspect}", callstack)
58
103
  end
59
104
  end
60
105
 
61
- class DeprecatedConstantProxy < DeprecationProxy #:nodoc:all
62
- def initialize(old_const, new_const)
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)
113
+ #
114
+ # (In a later update, the original implementation of `PLANETS` has been removed.)
115
+ #
116
+ # PLANETS_POST_2006 = %w(mercury venus earth mars jupiter saturn uranus neptune)
117
+ # PLANETS = ActiveSupport::Deprecation::DeprecatedConstantProxy.new('PLANETS', 'PLANETS_POST_2006')
118
+ #
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 < DeprecationProxy
124
+ def initialize(old_const, new_const, deprecator = ActiveSupport::Deprecation.instance)
63
125
  @old_const = old_const
64
126
  @new_const = new_const
127
+ @deprecator = deprecator
65
128
  end
66
129
 
130
+ # Returns the class of the new constant.
131
+ #
132
+ # PLANETS_POST_2006 = %w(mercury venus earth mars jupiter saturn uranus neptune)
133
+ # PLANETS = ActiveSupport::Deprecation::DeprecatedConstantProxy.new('PLANETS', 'PLANETS_POST_2006')
134
+ # PLANETS.class # => Array
67
135
  def class
68
136
  target.class
69
137
  end
@@ -74,7 +142,7 @@ module ActiveSupport
74
142
  end
75
143
 
76
144
  def warn(callstack, called, args)
77
- ActiveSupport::Deprecation.warn("#{@old_const} is deprecated! Use #{@new_const} instead.", callstack)
145
+ @deprecator.warn("#{@old_const} is deprecated! Use #{@new_const} instead.", callstack)
78
146
  end
79
147
  end
80
148
  end
@@ -1,20 +1,36 @@
1
+ require 'rbconfig'
2
+
1
3
  module ActiveSupport
2
- module Deprecation
3
- class << self
4
+ class Deprecation
5
+ module Reporting
6
+ # Whether to print a message (silent mode)
4
7
  attr_accessor :silenced
8
+ # Name of gem where method is deprecated
9
+ attr_accessor :gem_name
5
10
 
6
- # Outputs a deprecation warning to the output configured by <tt>ActiveSupport::Deprecation.behavior</tt>
11
+ # Outputs a deprecation warning to the output configured by
12
+ # <tt>ActiveSupport::Deprecation.behavior</tt>.
7
13
  #
8
- # ActiveSupport::Deprecation.warn("something broke!")
14
+ # ActiveSupport::Deprecation.warn('something broke!')
9
15
  # # => "DEPRECATION WARNING: something broke! (called from your_code.rb:1)"
10
- def warn(message = nil, callstack = caller)
16
+ def warn(message = nil, callstack = nil)
11
17
  return if silenced
18
+
19
+ callstack ||= caller_locations(2)
12
20
  deprecation_message(callstack, message).tap do |m|
13
21
  behavior.each { |b| b.call(m, callstack) }
14
22
  end
15
23
  end
16
24
 
17
25
  # Silence deprecation warnings within the block.
26
+ #
27
+ # ActiveSupport::Deprecation.warn('something broke!')
28
+ # # => "DEPRECATION WARNING: something broke! (called from your_code.rb:1)"
29
+ #
30
+ # ActiveSupport::Deprecation.silence do
31
+ # ActiveSupport::Deprecation.warn('something broke!')
32
+ # end
33
+ # # => nil
18
34
  def silence
19
35
  old_silenced, @silenced = @silenced, true
20
36
  yield
@@ -22,19 +38,33 @@ module ActiveSupport
22
38
  @silenced = old_silenced
23
39
  end
24
40
 
25
- def deprecated_method_warning(method_name, message = nil)
26
- warning = "#{method_name} is deprecated and will be removed from Rails #{deprecation_horizon}"
27
- case message
28
- when Symbol then "#{warning} (use #{message} instead)"
29
- when String then "#{warning} (#{message})"
30
- else warning
41
+ def deprecation_warning(deprecated_method_name, message = nil, caller_backtrace = nil)
42
+ caller_backtrace ||= caller_locations(2)
43
+ deprecated_method_warning(deprecated_method_name, message).tap do |msg|
44
+ warn(msg, caller_backtrace)
31
45
  end
32
46
  end
33
47
 
34
48
  private
49
+ # Outputs a deprecation warning message
50
+ #
51
+ # ActiveSupport::Deprecation.deprecated_method_warning(:method_name)
52
+ # # => "method_name is deprecated and will be removed from Rails #{deprecation_horizon}"
53
+ # ActiveSupport::Deprecation.deprecated_method_warning(:method_name, :another_method)
54
+ # # => "method_name is deprecated and will be removed from Rails #{deprecation_horizon} (use another_method instead)"
55
+ # ActiveSupport::Deprecation.deprecated_method_warning(:method_name, "Optional message")
56
+ # # => "method_name is deprecated and will be removed from Rails #{deprecation_horizon} (Optional message)"
57
+ def deprecated_method_warning(method_name, message = nil)
58
+ warning = "#{method_name} is deprecated and will be removed from #{gem_name} #{deprecation_horizon}"
59
+ case message
60
+ when Symbol then "#{warning} (use #{message} instead)"
61
+ when String then "#{warning} (#{message})"
62
+ else warning
63
+ end
64
+ end
65
+
35
66
  def deprecation_message(callstack, message = nil)
36
67
  message ||= "You are using deprecated behavior which will be removed from the next major or minor release."
37
- message += '.' unless message =~ /\.$/
38
68
  "DEPRECATION WARNING: #{message} #{deprecation_caller_message(callstack)}"
39
69
  end
40
70
 
@@ -50,8 +80,19 @@ module ActiveSupport
50
80
  end
51
81
 
52
82
  def extract_callstack(callstack)
53
- rails_gem_root = File.expand_path("../../../../..", __FILE__) + "/"
54
- offending_line = callstack.find { |line| !line.start_with?(rails_gem_root) } || callstack.first
83
+ return _extract_callstack(callstack) if callstack.first.is_a? String
84
+
85
+ offending_line = callstack.find { |frame|
86
+ frame.absolute_path && !ignored_callstack(frame.absolute_path)
87
+ } || callstack.first
88
+
89
+ [offending_line.path, offending_line.lineno, offending_line.label]
90
+ end
91
+
92
+ def _extract_callstack(callstack)
93
+ warn "Please pass `caller_locations` to the deprecation API" if $VERBOSE
94
+ offending_line = callstack.find { |line| !ignored_callstack(line) } || callstack.first
95
+
55
96
  if offending_line
56
97
  if md = offending_line.match(/^(.+?):(\d+)(?::in `(.*?)')?/)
57
98
  md.captures
@@ -60,6 +101,12 @@ module ActiveSupport
60
101
  end
61
102
  end
62
103
  end
104
+
105
+ RAILS_GEM_ROOT = File.expand_path("../../../../..", __FILE__) + "/"
106
+
107
+ def ignored_callstack(path)
108
+ path.start_with?(RAILS_GEM_ROOT) || path.start_with?(RbConfig::CONFIG['rubylibdir'])
109
+ end
63
110
  end
64
111
  end
65
112
  end
@@ -1,18 +1,43 @@
1
- require 'active_support/deprecation/behaviors'
2
- require 'active_support/deprecation/reporting'
3
- require 'active_support/deprecation/method_wrappers'
4
- require 'active_support/deprecation/proxy_wrappers'
1
+ require 'singleton'
5
2
 
6
3
  module ActiveSupport
7
- module Deprecation
8
- class << self
9
- # The version the deprecated behavior will be removed, by default.
10
- attr_accessor :deprecation_horizon
11
- end
12
- self.deprecation_horizon = '3.2'
4
+ # \Deprecation specifies the API used by Rails to deprecate methods, instance
5
+ # variables, objects and constants.
6
+ class Deprecation
7
+ # active_support.rb sets an autoload for ActiveSupport::Deprecation.
8
+ #
9
+ # If these requires were at the top of the file the constant would not be
10
+ # defined by the time their files were loaded. Since some of them reopen
11
+ # ActiveSupport::Deprecation its autoload would be triggered, resulting in
12
+ # a circular require warning for active_support/deprecation.rb.
13
+ #
14
+ # 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'
21
+
22
+ include Singleton
23
+ include InstanceDelegator
24
+ include Behavior
25
+ include Reporting
26
+ include MethodWrapper
13
27
 
14
- # By default, warnings are not silenced and debugging is off.
15
- self.silenced = false
16
- self.debug = false
28
+ # The version number in which the deprecated behavior will be removed, by default.
29
+ attr_accessor :deprecation_horizon
30
+
31
+ # It accepts two parameters on initialization. The first is a version of library
32
+ # and the second is a library name
33
+ #
34
+ # ActiveSupport::Deprecation.new('2.0', 'MyLibrary')
35
+ def initialize(deprecation_horizon = '5.1', gem_name = 'Rails')
36
+ self.gem_name = gem_name
37
+ self.deprecation_horizon = deprecation_horizon
38
+ # By default, warnings are not silenced and debugging is off.
39
+ self.silenced = false
40
+ self.debug = false
41
+ end
17
42
  end
18
43
  end
@@ -2,35 +2,50 @@ module ActiveSupport
2
2
  # This module provides an internal implementation to track descendants
3
3
  # which is faster than iterating through ObjectSpace.
4
4
  module DescendantsTracker
5
- @@direct_descendants = Hash.new { |h, k| h[k] = [] }
5
+ @@direct_descendants = {}
6
6
 
7
- def self.direct_descendants(klass)
8
- @@direct_descendants[klass]
9
- end
7
+ class << self
8
+ def direct_descendants(klass)
9
+ @@direct_descendants[klass] || []
10
+ end
10
11
 
11
- def self.descendants(klass)
12
- @@direct_descendants[klass].inject([]) do |descendants, _klass|
13
- descendants << _klass
14
- descendants.concat _klass.descendants
12
+ def descendants(klass)
13
+ arr = []
14
+ accumulate_descendants(klass, arr)
15
+ arr
15
16
  end
16
- end
17
17
 
18
- def self.clear
19
- if defined? ActiveSupport::Dependencies
20
- @@direct_descendants.each do |klass, descendants|
21
- if ActiveSupport::Dependencies.autoloaded?(klass)
22
- @@direct_descendants.delete(klass)
23
- else
24
- descendants.reject! { |v| ActiveSupport::Dependencies.autoloaded?(v) }
18
+ def clear
19
+ if defined? ActiveSupport::Dependencies
20
+ @@direct_descendants.each do |klass, descendants|
21
+ if ActiveSupport::Dependencies.autoloaded?(klass)
22
+ @@direct_descendants.delete(klass)
23
+ else
24
+ descendants.reject! { |v| ActiveSupport::Dependencies.autoloaded?(v) }
25
+ end
25
26
  end
27
+ else
28
+ @@direct_descendants.clear
29
+ end
30
+ end
31
+
32
+ # This is the only method that is not thread safe, but is only ever called
33
+ # during the eager loading phase.
34
+ def store_inherited(klass, descendant)
35
+ (@@direct_descendants[klass] ||= []) << descendant
36
+ end
37
+
38
+ private
39
+ def accumulate_descendants(klass, acc)
40
+ if direct_descendants = @@direct_descendants[klass]
41
+ acc.concat(direct_descendants)
42
+ direct_descendants.each { |direct_descendant| accumulate_descendants(direct_descendant, acc) }
26
43
  end
27
- else
28
- @@direct_descendants.clear
29
44
  end
30
45
  end
31
46
 
32
47
  def inherited(base)
33
- self.direct_descendants << base
48
+ DescendantsTracker.store_inherited(self, base)
34
49
  super
35
50
  end
36
51