activesupport 6.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (250) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +572 -0
  3. data/MIT-LICENSE +20 -0
  4. data/README.rdoc +40 -0
  5. data/lib/active_support.rb +96 -0
  6. data/lib/active_support/actionable_error.rb +48 -0
  7. data/lib/active_support/all.rb +5 -0
  8. data/lib/active_support/array_inquirer.rb +48 -0
  9. data/lib/active_support/backtrace_cleaner.rb +132 -0
  10. data/lib/active_support/benchmarkable.rb +51 -0
  11. data/lib/active_support/builder.rb +8 -0
  12. data/lib/active_support/cache.rb +830 -0
  13. data/lib/active_support/cache/file_store.rb +196 -0
  14. data/lib/active_support/cache/mem_cache_store.rb +212 -0
  15. data/lib/active_support/cache/memory_store.rb +174 -0
  16. data/lib/active_support/cache/null_store.rb +48 -0
  17. data/lib/active_support/cache/redis_cache_store.rb +488 -0
  18. data/lib/active_support/cache/strategy/local_cache.rb +194 -0
  19. data/lib/active_support/cache/strategy/local_cache_middleware.rb +45 -0
  20. data/lib/active_support/callbacks.rb +856 -0
  21. data/lib/active_support/concern.rb +171 -0
  22. data/lib/active_support/concurrency/load_interlock_aware_monitor.rb +17 -0
  23. data/lib/active_support/concurrency/share_lock.rb +227 -0
  24. data/lib/active_support/configurable.rb +146 -0
  25. data/lib/active_support/core_ext.rb +5 -0
  26. data/lib/active_support/core_ext/array.rb +9 -0
  27. data/lib/active_support/core_ext/array/access.rb +104 -0
  28. data/lib/active_support/core_ext/array/conversions.rb +213 -0
  29. data/lib/active_support/core_ext/array/extract.rb +21 -0
  30. data/lib/active_support/core_ext/array/extract_options.rb +31 -0
  31. data/lib/active_support/core_ext/array/grouping.rb +109 -0
  32. data/lib/active_support/core_ext/array/inquiry.rb +19 -0
  33. data/lib/active_support/core_ext/array/prepend_and_append.rb +5 -0
  34. data/lib/active_support/core_ext/array/wrap.rb +48 -0
  35. data/lib/active_support/core_ext/benchmark.rb +16 -0
  36. data/lib/active_support/core_ext/big_decimal.rb +3 -0
  37. data/lib/active_support/core_ext/big_decimal/conversions.rb +14 -0
  38. data/lib/active_support/core_ext/class.rb +4 -0
  39. data/lib/active_support/core_ext/class/attribute.rb +141 -0
  40. data/lib/active_support/core_ext/class/attribute_accessors.rb +6 -0
  41. data/lib/active_support/core_ext/class/subclasses.rb +54 -0
  42. data/lib/active_support/core_ext/date.rb +7 -0
  43. data/lib/active_support/core_ext/date/acts_like.rb +10 -0
  44. data/lib/active_support/core_ext/date/blank.rb +14 -0
  45. data/lib/active_support/core_ext/date/calculations.rb +146 -0
  46. data/lib/active_support/core_ext/date/conversions.rb +96 -0
  47. data/lib/active_support/core_ext/date/zones.rb +8 -0
  48. data/lib/active_support/core_ext/date_and_time/calculations.rb +351 -0
  49. data/lib/active_support/core_ext/date_and_time/compatibility.rb +16 -0
  50. data/lib/active_support/core_ext/date_and_time/zones.rb +41 -0
  51. data/lib/active_support/core_ext/date_time.rb +7 -0
  52. data/lib/active_support/core_ext/date_time/acts_like.rb +16 -0
  53. data/lib/active_support/core_ext/date_time/blank.rb +14 -0
  54. data/lib/active_support/core_ext/date_time/calculations.rb +211 -0
  55. data/lib/active_support/core_ext/date_time/compatibility.rb +18 -0
  56. data/lib/active_support/core_ext/date_time/conversions.rb +107 -0
  57. data/lib/active_support/core_ext/digest.rb +3 -0
  58. data/lib/active_support/core_ext/digest/uuid.rb +53 -0
  59. data/lib/active_support/core_ext/enumerable.rb +188 -0
  60. data/lib/active_support/core_ext/file.rb +3 -0
  61. data/lib/active_support/core_ext/file/atomic.rb +70 -0
  62. data/lib/active_support/core_ext/hash.rb +10 -0
  63. data/lib/active_support/core_ext/hash/compact.rb +5 -0
  64. data/lib/active_support/core_ext/hash/conversions.rb +263 -0
  65. data/lib/active_support/core_ext/hash/deep_merge.rb +34 -0
  66. data/lib/active_support/core_ext/hash/deep_transform_values.rb +46 -0
  67. data/lib/active_support/core_ext/hash/except.rb +24 -0
  68. data/lib/active_support/core_ext/hash/indifferent_access.rb +24 -0
  69. data/lib/active_support/core_ext/hash/keys.rb +143 -0
  70. data/lib/active_support/core_ext/hash/reverse_merge.rb +25 -0
  71. data/lib/active_support/core_ext/hash/slice.rb +26 -0
  72. data/lib/active_support/core_ext/hash/transform_values.rb +5 -0
  73. data/lib/active_support/core_ext/integer.rb +5 -0
  74. data/lib/active_support/core_ext/integer/inflections.rb +31 -0
  75. data/lib/active_support/core_ext/integer/multiple.rb +12 -0
  76. data/lib/active_support/core_ext/integer/time.rb +22 -0
  77. data/lib/active_support/core_ext/kernel.rb +5 -0
  78. data/lib/active_support/core_ext/kernel/concern.rb +14 -0
  79. data/lib/active_support/core_ext/kernel/reporting.rb +45 -0
  80. data/lib/active_support/core_ext/kernel/singleton_class.rb +8 -0
  81. data/lib/active_support/core_ext/load_error.rb +9 -0
  82. data/lib/active_support/core_ext/marshal.rb +24 -0
  83. data/lib/active_support/core_ext/module.rb +13 -0
  84. data/lib/active_support/core_ext/module/aliasing.rb +31 -0
  85. data/lib/active_support/core_ext/module/anonymous.rb +30 -0
  86. data/lib/active_support/core_ext/module/attr_internal.rb +38 -0
  87. data/lib/active_support/core_ext/module/attribute_accessors.rb +212 -0
  88. data/lib/active_support/core_ext/module/attribute_accessors_per_thread.rb +144 -0
  89. data/lib/active_support/core_ext/module/concerning.rb +134 -0
  90. data/lib/active_support/core_ext/module/delegation.rb +313 -0
  91. data/lib/active_support/core_ext/module/deprecation.rb +25 -0
  92. data/lib/active_support/core_ext/module/introspection.rb +86 -0
  93. data/lib/active_support/core_ext/module/reachable.rb +6 -0
  94. data/lib/active_support/core_ext/module/redefine_method.rb +40 -0
  95. data/lib/active_support/core_ext/module/remove_method.rb +17 -0
  96. data/lib/active_support/core_ext/name_error.rb +38 -0
  97. data/lib/active_support/core_ext/numeric.rb +5 -0
  98. data/lib/active_support/core_ext/numeric/bytes.rb +66 -0
  99. data/lib/active_support/core_ext/numeric/conversions.rb +136 -0
  100. data/lib/active_support/core_ext/numeric/inquiry.rb +5 -0
  101. data/lib/active_support/core_ext/numeric/time.rb +66 -0
  102. data/lib/active_support/core_ext/object.rb +16 -0
  103. data/lib/active_support/core_ext/object/acts_like.rb +21 -0
  104. data/lib/active_support/core_ext/object/blank.rb +155 -0
  105. data/lib/active_support/core_ext/object/conversions.rb +6 -0
  106. data/lib/active_support/core_ext/object/deep_dup.rb +55 -0
  107. data/lib/active_support/core_ext/object/duplicable.rb +49 -0
  108. data/lib/active_support/core_ext/object/inclusion.rb +29 -0
  109. data/lib/active_support/core_ext/object/instance_variables.rb +30 -0
  110. data/lib/active_support/core_ext/object/json.rb +228 -0
  111. data/lib/active_support/core_ext/object/to_param.rb +3 -0
  112. data/lib/active_support/core_ext/object/to_query.rb +89 -0
  113. data/lib/active_support/core_ext/object/try.rb +156 -0
  114. data/lib/active_support/core_ext/object/with_options.rb +82 -0
  115. data/lib/active_support/core_ext/range.rb +7 -0
  116. data/lib/active_support/core_ext/range/compare_range.rb +70 -0
  117. data/lib/active_support/core_ext/range/conversions.rb +41 -0
  118. data/lib/active_support/core_ext/range/each.rb +25 -0
  119. data/lib/active_support/core_ext/range/include_range.rb +9 -0
  120. data/lib/active_support/core_ext/range/include_time_with_zone.rb +23 -0
  121. data/lib/active_support/core_ext/range/overlaps.rb +10 -0
  122. data/lib/active_support/core_ext/regexp.rb +7 -0
  123. data/lib/active_support/core_ext/securerandom.rb +45 -0
  124. data/lib/active_support/core_ext/string.rb +15 -0
  125. data/lib/active_support/core_ext/string/access.rb +114 -0
  126. data/lib/active_support/core_ext/string/behavior.rb +8 -0
  127. data/lib/active_support/core_ext/string/conversions.rb +59 -0
  128. data/lib/active_support/core_ext/string/exclude.rb +13 -0
  129. data/lib/active_support/core_ext/string/filters.rb +145 -0
  130. data/lib/active_support/core_ext/string/indent.rb +45 -0
  131. data/lib/active_support/core_ext/string/inflections.rb +259 -0
  132. data/lib/active_support/core_ext/string/inquiry.rb +15 -0
  133. data/lib/active_support/core_ext/string/multibyte.rb +58 -0
  134. data/lib/active_support/core_ext/string/output_safety.rb +314 -0
  135. data/lib/active_support/core_ext/string/starts_ends_with.rb +6 -0
  136. data/lib/active_support/core_ext/string/strip.rb +27 -0
  137. data/lib/active_support/core_ext/string/zones.rb +16 -0
  138. data/lib/active_support/core_ext/time.rb +7 -0
  139. data/lib/active_support/core_ext/time/acts_like.rb +10 -0
  140. data/lib/active_support/core_ext/time/calculations.rb +344 -0
  141. data/lib/active_support/core_ext/time/compatibility.rb +16 -0
  142. data/lib/active_support/core_ext/time/conversions.rb +72 -0
  143. data/lib/active_support/core_ext/time/zones.rb +113 -0
  144. data/lib/active_support/core_ext/uri.rb +25 -0
  145. data/lib/active_support/current_attributes.rb +203 -0
  146. data/lib/active_support/dependencies.rb +806 -0
  147. data/lib/active_support/dependencies/autoload.rb +79 -0
  148. data/lib/active_support/dependencies/interlock.rb +57 -0
  149. data/lib/active_support/dependencies/zeitwerk_integration.rb +110 -0
  150. data/lib/active_support/deprecation.rb +46 -0
  151. data/lib/active_support/deprecation/behaviors.rb +109 -0
  152. data/lib/active_support/deprecation/constant_accessor.rb +52 -0
  153. data/lib/active_support/deprecation/instance_delegator.rb +39 -0
  154. data/lib/active_support/deprecation/method_wrappers.rb +78 -0
  155. data/lib/active_support/deprecation/proxy_wrappers.rb +173 -0
  156. data/lib/active_support/deprecation/reporting.rb +114 -0
  157. data/lib/active_support/descendants_tracker.rb +109 -0
  158. data/lib/active_support/digest.rb +20 -0
  159. data/lib/active_support/duration.rb +433 -0
  160. data/lib/active_support/duration/iso8601_parser.rb +124 -0
  161. data/lib/active_support/duration/iso8601_serializer.rb +54 -0
  162. data/lib/active_support/encrypted_configuration.rb +45 -0
  163. data/lib/active_support/encrypted_file.rb +100 -0
  164. data/lib/active_support/evented_file_update_checker.rb +235 -0
  165. data/lib/active_support/execution_wrapper.rb +129 -0
  166. data/lib/active_support/executor.rb +8 -0
  167. data/lib/active_support/file_update_checker.rb +163 -0
  168. data/lib/active_support/gem_version.rb +17 -0
  169. data/lib/active_support/gzip.rb +38 -0
  170. data/lib/active_support/hash_with_indifferent_access.rb +399 -0
  171. data/lib/active_support/i18n.rb +16 -0
  172. data/lib/active_support/i18n_railtie.rb +126 -0
  173. data/lib/active_support/inflections.rb +72 -0
  174. data/lib/active_support/inflector.rb +9 -0
  175. data/lib/active_support/inflector/inflections.rb +257 -0
  176. data/lib/active_support/inflector/methods.rb +398 -0
  177. data/lib/active_support/inflector/transliterate.rb +147 -0
  178. data/lib/active_support/json.rb +4 -0
  179. data/lib/active_support/json/decoding.rb +76 -0
  180. data/lib/active_support/json/encoding.rb +134 -0
  181. data/lib/active_support/key_generator.rb +41 -0
  182. data/lib/active_support/lazy_load_hooks.rb +82 -0
  183. data/lib/active_support/locale/en.rb +31 -0
  184. data/lib/active_support/locale/en.yml +135 -0
  185. data/lib/active_support/log_subscriber.rb +135 -0
  186. data/lib/active_support/log_subscriber/test_helper.rb +106 -0
  187. data/lib/active_support/logger.rb +93 -0
  188. data/lib/active_support/logger_silence.rb +45 -0
  189. data/lib/active_support/logger_thread_safe_level.rb +56 -0
  190. data/lib/active_support/message_encryptor.rb +227 -0
  191. data/lib/active_support/message_verifier.rb +205 -0
  192. data/lib/active_support/messages/metadata.rb +71 -0
  193. data/lib/active_support/messages/rotation_configuration.rb +22 -0
  194. data/lib/active_support/messages/rotator.rb +56 -0
  195. data/lib/active_support/multibyte.rb +23 -0
  196. data/lib/active_support/multibyte/chars.rb +216 -0
  197. data/lib/active_support/multibyte/unicode.rb +157 -0
  198. data/lib/active_support/notifications.rb +253 -0
  199. data/lib/active_support/notifications/fanout.rb +244 -0
  200. data/lib/active_support/notifications/instrumenter.rb +164 -0
  201. data/lib/active_support/number_helper.rb +378 -0
  202. data/lib/active_support/number_helper/number_converter.rb +184 -0
  203. data/lib/active_support/number_helper/number_to_currency_converter.rb +46 -0
  204. data/lib/active_support/number_helper/number_to_delimited_converter.rb +31 -0
  205. data/lib/active_support/number_helper/number_to_human_converter.rb +70 -0
  206. data/lib/active_support/number_helper/number_to_human_size_converter.rb +61 -0
  207. data/lib/active_support/number_helper/number_to_percentage_converter.rb +16 -0
  208. data/lib/active_support/number_helper/number_to_phone_converter.rb +60 -0
  209. data/lib/active_support/number_helper/number_to_rounded_converter.rb +56 -0
  210. data/lib/active_support/number_helper/rounding_helper.rb +66 -0
  211. data/lib/active_support/option_merger.rb +27 -0
  212. data/lib/active_support/ordered_hash.rb +50 -0
  213. data/lib/active_support/ordered_options.rb +85 -0
  214. data/lib/active_support/parameter_filter.rb +129 -0
  215. data/lib/active_support/per_thread_registry.rb +60 -0
  216. data/lib/active_support/proxy_object.rb +15 -0
  217. data/lib/active_support/rails.rb +29 -0
  218. data/lib/active_support/railtie.rb +80 -0
  219. data/lib/active_support/reloader.rb +130 -0
  220. data/lib/active_support/rescuable.rb +174 -0
  221. data/lib/active_support/security_utils.rb +31 -0
  222. data/lib/active_support/string_inquirer.rb +34 -0
  223. data/lib/active_support/subscriber.rb +169 -0
  224. data/lib/active_support/tagged_logging.rb +88 -0
  225. data/lib/active_support/test_case.rb +163 -0
  226. data/lib/active_support/testing/assertions.rb +228 -0
  227. data/lib/active_support/testing/autorun.rb +7 -0
  228. data/lib/active_support/testing/constant_lookup.rb +51 -0
  229. data/lib/active_support/testing/declarative.rb +28 -0
  230. data/lib/active_support/testing/deprecation.rb +38 -0
  231. data/lib/active_support/testing/file_fixtures.rb +38 -0
  232. data/lib/active_support/testing/isolation.rb +110 -0
  233. data/lib/active_support/testing/method_call_assertions.rb +70 -0
  234. data/lib/active_support/testing/parallelization.rb +128 -0
  235. data/lib/active_support/testing/setup_and_teardown.rb +55 -0
  236. data/lib/active_support/testing/stream.rb +44 -0
  237. data/lib/active_support/testing/tagged_logging.rb +27 -0
  238. data/lib/active_support/testing/time_helpers.rb +200 -0
  239. data/lib/active_support/time.rb +20 -0
  240. data/lib/active_support/time_with_zone.rb +561 -0
  241. data/lib/active_support/values/time_zone.rb +570 -0
  242. data/lib/active_support/version.rb +10 -0
  243. data/lib/active_support/xml_mini.rb +202 -0
  244. data/lib/active_support/xml_mini/jdom.rb +183 -0
  245. data/lib/active_support/xml_mini/libxml.rb +80 -0
  246. data/lib/active_support/xml_mini/libxmlsax.rb +83 -0
  247. data/lib/active_support/xml_mini/nokogiri.rb +83 -0
  248. data/lib/active_support/xml_mini/nokogirisax.rb +86 -0
  249. data/lib/active_support/xml_mini/rexml.rb +130 -0
  250. metadata +385 -0
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/core_ext/kernel/singleton_class"
4
+ require "active_support/core_ext/module/delegation"
5
+
6
+ module ActiveSupport
7
+ class Deprecation
8
+ module InstanceDelegator # :nodoc:
9
+ def self.included(base)
10
+ base.extend(ClassMethods)
11
+ base.singleton_class.prepend(OverrideDelegators)
12
+ base.public_class_method :new
13
+ end
14
+
15
+ module ClassMethods # :nodoc:
16
+ def include(included_module)
17
+ included_module.instance_methods.each { |m| method_added(m) }
18
+ super
19
+ end
20
+
21
+ def method_added(method_name)
22
+ singleton_class.delegate(method_name, to: :instance)
23
+ end
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
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,78 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/core_ext/array/extract_options"
4
+ require "active_support/core_ext/module/redefine_method"
5
+
6
+ module ActiveSupport
7
+ class Deprecation
8
+ module MethodWrapper
9
+ # Declare that a method has been deprecated.
10
+ #
11
+ # class Fred
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
+ # # => 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
30
+ #
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
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.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
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
56
+ mod = nil
57
+
58
+ method_names.each do |method_name|
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.redefine_method(method_name) do |*args, &block|
62
+ deprecator.deprecation_warning(method_name, options[method_name])
63
+ method.bind(self).call(*args, &block)
64
+ end
65
+ else
66
+ mod ||= Module.new
67
+ mod.define_method(method_name) do |*args, &block|
68
+ deprecator.deprecation_warning(method_name, options[method_name])
69
+ super(*args, &block)
70
+ end
71
+ end
72
+ end
73
+
74
+ target_module.prepend(mod) if mod
75
+ end
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,173 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveSupport
4
+ class Deprecation
5
+ class DeprecationProxy #:nodoc:
6
+ def self.new(*args, &block)
7
+ object = args.first
8
+
9
+ return object unless object
10
+ super
11
+ end
12
+
13
+ instance_methods.each { |m| undef_method m unless /^__|^object_id$/.match?(m) }
14
+
15
+ # Don't give a deprecation warning on inspect since test/unit and error
16
+ # logs rely on it for diagnostics.
17
+ def inspect
18
+ target.inspect
19
+ end
20
+
21
+ private
22
+ def method_missing(called, *args, &block)
23
+ warn caller_locations, called, args
24
+ target.__send__(called, *args, &block)
25
+ end
26
+ end
27
+
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)
41
+ @object = object
42
+ @message = message
43
+ @deprecator = deprecator
44
+ end
45
+
46
+ private
47
+ def target
48
+ @object
49
+ end
50
+
51
+ def warn(callstack, called, args)
52
+ @deprecator.warn(@message, callstack)
53
+ end
54
+ end
55
+
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
94
+ end
95
+
96
+ private
97
+ def target
98
+ @instance.__send__(@method)
99
+ end
100
+
101
+ def warn(callstack, called, args)
102
+ @deprecator.warn("#{@var} is deprecated! Call #{@method}.#{called} instead of #{@var}.#{called}. Args: #{args.inspect}", callstack)
103
+ end
104
+ end
105
+
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 < Module
124
+ def self.new(*args, &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
+
134
+ @old_const = old_const
135
+ @new_const = new_const
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
146
+ end
147
+
148
+ # Returns the class of the new constant.
149
+ #
150
+ # PLANETS_POST_2006 = %w(mercury venus earth mars jupiter saturn uranus neptune)
151
+ # PLANETS = ActiveSupport::Deprecation::DeprecatedConstantProxy.new('PLANETS', 'PLANETS_POST_2006')
152
+ # PLANETS.class # => Array
153
+ def class
154
+ target.class
155
+ end
156
+
157
+ private
158
+ def target
159
+ ActiveSupport::Inflector.constantize(@new_const.to_s)
160
+ end
161
+
162
+ def const_missing(name)
163
+ @deprecator.warn(@message, caller_locations)
164
+ target.const_get(name)
165
+ end
166
+
167
+ def method_missing(called, *args, &block)
168
+ @deprecator.warn(@message, caller_locations)
169
+ target.__send__(called, *args, &block)
170
+ end
171
+ end
172
+ end
173
+ end
@@ -0,0 +1,114 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rbconfig"
4
+
5
+ module ActiveSupport
6
+ class Deprecation
7
+ module Reporting
8
+ # Whether to print a message (silent mode)
9
+ attr_accessor :silenced
10
+ # Name of gem where method is deprecated
11
+ attr_accessor :gem_name
12
+
13
+ # Outputs a deprecation warning to the output configured by
14
+ # <tt>ActiveSupport::Deprecation.behavior</tt>.
15
+ #
16
+ # ActiveSupport::Deprecation.warn('something broke!')
17
+ # # => "DEPRECATION WARNING: something broke! (called from your_code.rb:1)"
18
+ def warn(message = nil, callstack = nil)
19
+ return if silenced
20
+
21
+ callstack ||= caller_locations(2)
22
+ deprecation_message(callstack, message).tap do |m|
23
+ behavior.each { |b| b.call(m, callstack, deprecation_horizon, gem_name) }
24
+ end
25
+ end
26
+
27
+ # Silence deprecation warnings within the block.
28
+ #
29
+ # ActiveSupport::Deprecation.warn('something broke!')
30
+ # # => "DEPRECATION WARNING: something broke! (called from your_code.rb:1)"
31
+ #
32
+ # ActiveSupport::Deprecation.silence do
33
+ # ActiveSupport::Deprecation.warn('something broke!')
34
+ # end
35
+ # # => nil
36
+ def silence
37
+ old_silenced, @silenced = @silenced, true
38
+ yield
39
+ ensure
40
+ @silenced = old_silenced
41
+ end
42
+
43
+ def deprecation_warning(deprecated_method_name, message = nil, caller_backtrace = nil)
44
+ caller_backtrace ||= caller_locations(2)
45
+ deprecated_method_warning(deprecated_method_name, message).tap do |msg|
46
+ warn(msg, caller_backtrace)
47
+ end
48
+ end
49
+
50
+ private
51
+ # Outputs a deprecation warning message
52
+ #
53
+ # deprecated_method_warning(:method_name)
54
+ # # => "method_name is deprecated and will be removed from Rails #{deprecation_horizon}"
55
+ # deprecated_method_warning(:method_name, :another_method)
56
+ # # => "method_name is deprecated and will be removed from Rails #{deprecation_horizon} (use another_method instead)"
57
+ # deprecated_method_warning(:method_name, "Optional message")
58
+ # # => "method_name is deprecated and will be removed from Rails #{deprecation_horizon} (Optional message)"
59
+ def deprecated_method_warning(method_name, message = nil)
60
+ warning = "#{method_name} is deprecated and will be removed from #{gem_name} #{deprecation_horizon}"
61
+ case message
62
+ when Symbol then "#{warning} (use #{message} instead)"
63
+ when String then "#{warning} (#{message})"
64
+ else warning
65
+ end
66
+ end
67
+
68
+ def deprecation_message(callstack, message = nil)
69
+ message ||= "You are using deprecated behavior which will be removed from the next major or minor release."
70
+ "DEPRECATION WARNING: #{message} #{deprecation_caller_message(callstack)}"
71
+ end
72
+
73
+ def deprecation_caller_message(callstack)
74
+ file, line, method = extract_callstack(callstack)
75
+ if file
76
+ if line && method
77
+ "(called from #{method} at #{file}:#{line})"
78
+ else
79
+ "(called from #{file}:#{line})"
80
+ end
81
+ end
82
+ end
83
+
84
+ def extract_callstack(callstack)
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
+
98
+ if offending_line
99
+ if md = offending_line.match(/^(.+?):(\d+)(?::in `(.*?)')?/)
100
+ md.captures
101
+ else
102
+ offending_line
103
+ end
104
+ end
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
112
+ end
113
+ end
114
+ end
@@ -0,0 +1,109 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "weakref"
4
+
5
+ module ActiveSupport
6
+ # This module provides an internal implementation to track descendants
7
+ # which is faster than iterating through ObjectSpace.
8
+ module DescendantsTracker
9
+ @@direct_descendants = {}
10
+
11
+ class << self
12
+ def direct_descendants(klass)
13
+ descendants = @@direct_descendants[klass]
14
+ descendants ? descendants.to_a : []
15
+ end
16
+
17
+ def descendants(klass)
18
+ arr = []
19
+ accumulate_descendants(klass, arr)
20
+ arr
21
+ end
22
+
23
+ def clear
24
+ if defined? ActiveSupport::Dependencies
25
+ @@direct_descendants.each do |klass, descendants|
26
+ if Dependencies.autoloaded?(klass)
27
+ @@direct_descendants.delete(klass)
28
+ else
29
+ descendants.reject! { |v| Dependencies.autoloaded?(v) }
30
+ end
31
+ end
32
+ else
33
+ @@direct_descendants.clear
34
+ end
35
+ end
36
+
37
+ # This is the only method that is not thread safe, but is only ever called
38
+ # during the eager loading phase.
39
+ def store_inherited(klass, descendant)
40
+ (@@direct_descendants[klass] ||= DescendantsArray.new) << descendant
41
+ end
42
+
43
+ private
44
+
45
+ def accumulate_descendants(klass, acc)
46
+ if direct_descendants = @@direct_descendants[klass]
47
+ direct_descendants.each do |direct_descendant|
48
+ acc << direct_descendant
49
+ accumulate_descendants(direct_descendant, acc)
50
+ end
51
+ end
52
+ end
53
+ end
54
+
55
+ def inherited(base)
56
+ DescendantsTracker.store_inherited(self, base)
57
+ super
58
+ end
59
+
60
+ def direct_descendants
61
+ DescendantsTracker.direct_descendants(self)
62
+ end
63
+
64
+ def descendants
65
+ DescendantsTracker.descendants(self)
66
+ end
67
+
68
+ # DescendantsArray is an array that contains weak references to classes.
69
+ class DescendantsArray # :nodoc:
70
+ include Enumerable
71
+
72
+ def initialize
73
+ @refs = []
74
+ end
75
+
76
+ def initialize_copy(orig)
77
+ @refs = @refs.dup
78
+ end
79
+
80
+ def <<(klass)
81
+ cleanup!
82
+ @refs << WeakRef.new(klass)
83
+ end
84
+
85
+ def each
86
+ @refs.each do |ref|
87
+ yield ref.__getobj__
88
+ rescue WeakRef::RefError
89
+ end
90
+ end
91
+
92
+ def refs_size
93
+ @refs.size
94
+ end
95
+
96
+ def cleanup!
97
+ @refs.delete_if { |ref| !ref.weakref_alive? }
98
+ end
99
+
100
+ def reject!
101
+ @refs.reject! do |ref|
102
+ yield ref.__getobj__
103
+ rescue WeakRef::RefError
104
+ true
105
+ end
106
+ end
107
+ end
108
+ end
109
+ end