activesupport 6.0.4.4 → 7.0.4.1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of activesupport might be problematic. Click here for more details.

Files changed (212) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +257 -532
  3. data/MIT-LICENSE +1 -1
  4. data/lib/active_support/actionable_error.rb +1 -1
  5. data/lib/active_support/array_inquirer.rb +2 -2
  6. data/lib/active_support/backtrace_cleaner.rb +5 -5
  7. data/lib/active_support/benchmarkable.rb +3 -3
  8. data/lib/active_support/cache/file_store.rb +16 -10
  9. data/lib/active_support/cache/mem_cache_store.rb +163 -42
  10. data/lib/active_support/cache/memory_store.rb +57 -29
  11. data/lib/active_support/cache/null_store.rb +10 -2
  12. data/lib/active_support/cache/redis_cache_store.rb +79 -98
  13. data/lib/active_support/cache/strategy/local_cache.rb +49 -57
  14. data/lib/active_support/cache.rb +378 -179
  15. data/lib/active_support/callbacks.rb +230 -122
  16. data/lib/active_support/code_generator.rb +65 -0
  17. data/lib/active_support/concern.rb +49 -5
  18. data/lib/active_support/concurrency/load_interlock_aware_monitor.rb +2 -4
  19. data/lib/active_support/concurrency/share_lock.rb +2 -2
  20. data/lib/active_support/configurable.rb +9 -6
  21. data/lib/active_support/configuration_file.rb +51 -0
  22. data/lib/active_support/core_ext/array/access.rb +1 -5
  23. data/lib/active_support/core_ext/array/conversions.rb +13 -12
  24. data/lib/active_support/core_ext/array/deprecated_conversions.rb +25 -0
  25. data/lib/active_support/core_ext/array/grouping.rb +6 -6
  26. data/lib/active_support/core_ext/array/inquiry.rb +2 -2
  27. data/lib/active_support/core_ext/array.rb +1 -0
  28. data/lib/active_support/core_ext/benchmark.rb +2 -2
  29. data/lib/active_support/core_ext/big_decimal/conversions.rb +1 -1
  30. data/lib/active_support/core_ext/class/attribute.rb +34 -44
  31. data/lib/active_support/core_ext/class/subclasses.rb +9 -22
  32. data/lib/active_support/core_ext/date/blank.rb +1 -1
  33. data/lib/active_support/core_ext/date/calculations.rb +9 -9
  34. data/lib/active_support/core_ext/date/conversions.rb +16 -15
  35. data/lib/active_support/core_ext/date/deprecated_conversions.rb +26 -0
  36. data/lib/active_support/core_ext/date.rb +1 -0
  37. data/lib/active_support/core_ext/date_and_time/calculations.rb +17 -4
  38. data/lib/active_support/core_ext/date_and_time/compatibility.rb +15 -0
  39. data/lib/active_support/core_ext/date_time/blank.rb +1 -1
  40. data/lib/active_support/core_ext/date_time/conversions.rb +13 -13
  41. data/lib/active_support/core_ext/date_time/deprecated_conversions.rb +22 -0
  42. data/lib/active_support/core_ext/date_time.rb +1 -0
  43. data/lib/active_support/core_ext/digest/uuid.rb +39 -13
  44. data/lib/active_support/core_ext/enumerable.rb +164 -23
  45. data/lib/active_support/core_ext/file/atomic.rb +3 -1
  46. data/lib/active_support/core_ext/hash/conversions.rb +2 -3
  47. data/lib/active_support/core_ext/hash/deep_transform_values.rb +1 -1
  48. data/lib/active_support/core_ext/hash/indifferent_access.rb +3 -3
  49. data/lib/active_support/core_ext/hash/keys.rb +2 -2
  50. data/lib/active_support/core_ext/hash/slice.rb +3 -2
  51. data/lib/active_support/core_ext/kernel/reporting.rb +4 -4
  52. data/lib/active_support/core_ext/kernel/singleton_class.rb +1 -1
  53. data/lib/active_support/core_ext/load_error.rb +1 -1
  54. data/lib/active_support/core_ext/module/attr_internal.rb +2 -2
  55. data/lib/active_support/core_ext/module/attribute_accessors.rb +25 -29
  56. data/lib/active_support/core_ext/module/attribute_accessors_per_thread.rb +26 -13
  57. data/lib/active_support/core_ext/module/concerning.rb +8 -2
  58. data/lib/active_support/core_ext/module/delegation.rb +40 -36
  59. data/lib/active_support/core_ext/module/introspection.rb +1 -25
  60. data/lib/active_support/core_ext/name_error.rb +23 -2
  61. data/lib/active_support/core_ext/numeric/conversions.rb +80 -73
  62. data/lib/active_support/core_ext/numeric/deprecated_conversions.rb +60 -0
  63. data/lib/active_support/core_ext/numeric.rb +1 -0
  64. data/lib/active_support/core_ext/object/acts_like.rb +29 -5
  65. data/lib/active_support/core_ext/object/blank.rb +2 -2
  66. data/lib/active_support/core_ext/object/deep_dup.rb +1 -1
  67. data/lib/active_support/core_ext/object/duplicable.rb +11 -0
  68. data/lib/active_support/core_ext/object/json.rb +42 -26
  69. data/lib/active_support/core_ext/object/to_query.rb +2 -2
  70. data/lib/active_support/core_ext/object/try.rb +20 -20
  71. data/lib/active_support/core_ext/object/with_options.rb +20 -1
  72. data/lib/active_support/core_ext/pathname/existence.rb +21 -0
  73. data/lib/active_support/core_ext/pathname.rb +3 -0
  74. data/lib/active_support/core_ext/range/compare_range.rb +6 -25
  75. data/lib/active_support/core_ext/range/conversions.rb +8 -8
  76. data/lib/active_support/core_ext/range/deprecated_conversions.rb +26 -0
  77. data/lib/active_support/core_ext/range/each.rb +1 -1
  78. data/lib/active_support/core_ext/range/include_time_with_zone.rb +4 -20
  79. data/lib/active_support/core_ext/range/overlaps.rb +1 -1
  80. data/lib/active_support/core_ext/range.rb +1 -1
  81. data/lib/active_support/core_ext/regexp.rb +8 -1
  82. data/lib/active_support/core_ext/securerandom.rb +1 -1
  83. data/lib/active_support/core_ext/string/access.rb +5 -24
  84. data/lib/active_support/core_ext/string/conversions.rb +3 -2
  85. data/lib/active_support/core_ext/string/filters.rb +1 -1
  86. data/lib/active_support/core_ext/string/inflections.rb +39 -5
  87. data/lib/active_support/core_ext/string/inquiry.rb +2 -1
  88. data/lib/active_support/core_ext/string/multibyte.rb +2 -2
  89. data/lib/active_support/core_ext/string/output_safety.rb +92 -41
  90. data/lib/active_support/core_ext/string/starts_ends_with.rb +2 -2
  91. data/lib/active_support/core_ext/symbol/starts_ends_with.rb +6 -0
  92. data/lib/active_support/core_ext/symbol.rb +3 -0
  93. data/lib/active_support/core_ext/time/calculations.rb +25 -7
  94. data/lib/active_support/core_ext/time/conversions.rb +15 -12
  95. data/lib/active_support/core_ext/time/deprecated_conversions.rb +22 -0
  96. data/lib/active_support/core_ext/time/zones.rb +7 -22
  97. data/lib/active_support/core_ext/time.rb +1 -0
  98. data/lib/active_support/core_ext/uri.rb +3 -23
  99. data/lib/active_support/core_ext.rb +2 -1
  100. data/lib/active_support/current_attributes/test_helper.rb +13 -0
  101. data/lib/active_support/current_attributes.rb +39 -16
  102. data/lib/active_support/dependencies/interlock.rb +10 -18
  103. data/lib/active_support/dependencies/require_dependency.rb +28 -0
  104. data/lib/active_support/dependencies.rb +58 -769
  105. data/lib/active_support/deprecation/behaviors.rb +23 -7
  106. data/lib/active_support/deprecation/disallowed.rb +56 -0
  107. data/lib/active_support/deprecation/instance_delegator.rb +0 -1
  108. data/lib/active_support/deprecation/method_wrappers.rb +6 -5
  109. data/lib/active_support/deprecation/proxy_wrappers.rb +4 -4
  110. data/lib/active_support/deprecation/reporting.rb +50 -7
  111. data/lib/active_support/deprecation.rb +7 -2
  112. data/lib/active_support/descendants_tracker.rb +174 -64
  113. data/lib/active_support/digest.rb +5 -3
  114. data/lib/active_support/duration/iso8601_parser.rb +3 -3
  115. data/lib/active_support/duration/iso8601_serializer.rb +24 -10
  116. data/lib/active_support/duration.rb +134 -55
  117. data/lib/active_support/encrypted_configuration.rb +13 -2
  118. data/lib/active_support/encrypted_file.rb +32 -3
  119. data/lib/active_support/environment_inquirer.rb +20 -0
  120. data/lib/active_support/error_reporter.rb +117 -0
  121. data/lib/active_support/evented_file_update_checker.rb +72 -138
  122. data/lib/active_support/execution_context/test_helper.rb +13 -0
  123. data/lib/active_support/execution_context.rb +53 -0
  124. data/lib/active_support/execution_wrapper.rb +43 -21
  125. data/lib/active_support/executor/test_helper.rb +7 -0
  126. data/lib/active_support/fork_tracker.rb +71 -0
  127. data/lib/active_support/gem_version.rb +3 -3
  128. data/lib/active_support/hash_with_indifferent_access.rb +51 -25
  129. data/lib/active_support/html_safe_translation.rb +43 -0
  130. data/lib/active_support/i18n.rb +1 -0
  131. data/lib/active_support/i18n_railtie.rb +14 -19
  132. data/lib/active_support/inflector/inflections.rb +24 -9
  133. data/lib/active_support/inflector/methods.rb +29 -49
  134. data/lib/active_support/inflector/transliterate.rb +5 -5
  135. data/lib/active_support/isolated_execution_state.rb +72 -0
  136. data/lib/active_support/json/decoding.rb +4 -4
  137. data/lib/active_support/json/encoding.rb +8 -4
  138. data/lib/active_support/key_generator.rb +23 -6
  139. data/lib/active_support/lazy_load_hooks.rb +28 -4
  140. data/lib/active_support/locale/en.yml +8 -4
  141. data/lib/active_support/log_subscriber/test_helper.rb +2 -2
  142. data/lib/active_support/log_subscriber.rb +23 -5
  143. data/lib/active_support/logger.rb +1 -1
  144. data/lib/active_support/logger_silence.rb +2 -26
  145. data/lib/active_support/logger_thread_safe_level.rb +34 -21
  146. data/lib/active_support/message_encryptor.rb +16 -13
  147. data/lib/active_support/message_verifier.rb +50 -18
  148. data/lib/active_support/messages/metadata.rb +2 -2
  149. data/lib/active_support/messages/rotation_configuration.rb +2 -1
  150. data/lib/active_support/messages/rotator.rb +6 -5
  151. data/lib/active_support/multibyte/chars.rb +13 -52
  152. data/lib/active_support/multibyte/unicode.rb +1 -87
  153. data/lib/active_support/multibyte.rb +1 -1
  154. data/lib/active_support/notifications/fanout.rb +110 -69
  155. data/lib/active_support/notifications/instrumenter.rb +37 -29
  156. data/lib/active_support/notifications.rb +55 -28
  157. data/lib/active_support/number_helper/number_converter.rb +2 -4
  158. data/lib/active_support/number_helper/number_to_currency_converter.rb +11 -6
  159. data/lib/active_support/number_helper/number_to_delimited_converter.rb +1 -1
  160. data/lib/active_support/number_helper/number_to_human_converter.rb +1 -1
  161. data/lib/active_support/number_helper/number_to_human_size_converter.rb +2 -2
  162. data/lib/active_support/number_helper/number_to_phone_converter.rb +1 -1
  163. data/lib/active_support/number_helper/number_to_rounded_converter.rb +9 -5
  164. data/lib/active_support/number_helper/rounding_helper.rb +12 -32
  165. data/lib/active_support/number_helper.rb +29 -16
  166. data/lib/active_support/option_merger.rb +11 -18
  167. data/lib/active_support/ordered_hash.rb +1 -1
  168. data/lib/active_support/ordered_options.rb +9 -3
  169. data/lib/active_support/parameter_filter.rb +21 -11
  170. data/lib/active_support/per_thread_registry.rb +6 -1
  171. data/lib/active_support/rails.rb +1 -4
  172. data/lib/active_support/railtie.rb +77 -5
  173. data/lib/active_support/reloader.rb +1 -1
  174. data/lib/active_support/rescuable.rb +16 -16
  175. data/lib/active_support/ruby_features.rb +7 -0
  176. data/lib/active_support/secure_compare_rotator.rb +51 -0
  177. data/lib/active_support/security_utils.rb +19 -12
  178. data/lib/active_support/string_inquirer.rb +2 -2
  179. data/lib/active_support/subscriber.rb +19 -25
  180. data/lib/active_support/tagged_logging.rb +31 -6
  181. data/lib/active_support/test_case.rb +13 -21
  182. data/lib/active_support/testing/assertions.rb +50 -13
  183. data/lib/active_support/testing/deprecation.rb +52 -1
  184. data/lib/active_support/testing/isolation.rb +2 -2
  185. data/lib/active_support/testing/method_call_assertions.rb +5 -5
  186. data/lib/active_support/testing/parallelization/server.rb +82 -0
  187. data/lib/active_support/testing/parallelization/worker.rb +103 -0
  188. data/lib/active_support/testing/parallelization.rb +16 -95
  189. data/lib/active_support/testing/parallelize_executor.rb +76 -0
  190. data/lib/active_support/testing/stream.rb +3 -5
  191. data/lib/active_support/testing/tagged_logging.rb +1 -1
  192. data/lib/active_support/testing/time_helpers.rb +53 -5
  193. data/lib/active_support/time_with_zone.rb +126 -62
  194. data/lib/active_support/values/time_zone.rb +54 -23
  195. data/lib/active_support/version.rb +1 -1
  196. data/lib/active_support/xml_mini/jdom.rb +1 -1
  197. data/lib/active_support/xml_mini/libxml.rb +5 -5
  198. data/lib/active_support/xml_mini/libxmlsax.rb +1 -1
  199. data/lib/active_support/xml_mini/nokogiri.rb +4 -4
  200. data/lib/active_support/xml_mini/nokogirisax.rb +1 -1
  201. data/lib/active_support/xml_mini/rexml.rb +9 -2
  202. data/lib/active_support/xml_mini.rb +5 -4
  203. data/lib/active_support.rb +29 -1
  204. metadata +46 -45
  205. data/lib/active_support/core_ext/array/prepend_and_append.rb +0 -5
  206. data/lib/active_support/core_ext/hash/compact.rb +0 -5
  207. data/lib/active_support/core_ext/hash/transform_values.rb +0 -5
  208. data/lib/active_support/core_ext/marshal.rb +0 -24
  209. data/lib/active_support/core_ext/module/reachable.rb +0 -6
  210. data/lib/active_support/core_ext/numeric/inquiry.rb +0 -5
  211. data/lib/active_support/core_ext/range/include_range.rb +0 -9
  212. data/lib/active_support/dependencies/zeitwerk_integration.rb +0 -117
@@ -2,13 +2,14 @@
2
2
 
3
3
  require "active_support/core_ext/big_decimal/conversions"
4
4
  require "active_support/number_helper"
5
- require "active_support/core_ext/module/deprecation"
6
5
 
7
6
  module ActiveSupport
8
7
  module NumericWithFormat
9
8
  # Provides options for converting numbers into formatted strings.
10
9
  # Options are provided for phone numbers, currency, percentage,
11
- # precision, positional notation, file size and pretty printing.
10
+ # precision, positional notation, file size, and pretty printing.
11
+ #
12
+ # This method is aliased to <tt>to_formatted_s</tt>.
12
13
  #
13
14
  # ==== Options
14
15
  #
@@ -17,97 +18,102 @@ module ActiveSupport
17
18
  # ==== Examples
18
19
  #
19
20
  # Phone Numbers:
20
- # 5551234.to_s(:phone) # => "555-1234"
21
- # 1235551234.to_s(:phone) # => "123-555-1234"
22
- # 1235551234.to_s(:phone, area_code: true) # => "(123) 555-1234"
23
- # 1235551234.to_s(:phone, delimiter: ' ') # => "123 555 1234"
24
- # 1235551234.to_s(:phone, area_code: true, extension: 555) # => "(123) 555-1234 x 555"
25
- # 1235551234.to_s(:phone, country_code: 1) # => "+1-123-555-1234"
26
- # 1235551234.to_s(:phone, country_code: 1, extension: 1343, delimiter: '.')
21
+ # 5551234.to_fs(:phone) # => "555-1234"
22
+ # 1235551234.to_fs(:phone) # => "123-555-1234"
23
+ # 1235551234.to_fs(:phone, area_code: true) # => "(123) 555-1234"
24
+ # 1235551234.to_fs(:phone, delimiter: ' ') # => "123 555 1234"
25
+ # 1235551234.to_fs(:phone, area_code: true, extension: 555) # => "(123) 555-1234 x 555"
26
+ # 1235551234.to_fs(:phone, country_code: 1) # => "+1-123-555-1234"
27
+ # 1235551234.to_fs(:phone, country_code: 1, extension: 1343, delimiter: '.')
27
28
  # # => "+1.123.555.1234 x 1343"
28
29
  #
29
30
  # Currency:
30
- # 1234567890.50.to_s(:currency) # => "$1,234,567,890.50"
31
- # 1234567890.506.to_s(:currency) # => "$1,234,567,890.51"
32
- # 1234567890.506.to_s(:currency, precision: 3) # => "$1,234,567,890.506"
33
- # 1234567890.506.to_s(:currency, locale: :fr) # => "1 234 567 890,51 €"
34
- # -1234567890.50.to_s(:currency, negative_format: '(%u%n)')
31
+ # 1234567890.50.to_fs(:currency) # => "$1,234,567,890.50"
32
+ # 1234567890.506.to_fs(:currency) # => "$1,234,567,890.51"
33
+ # 1234567890.506.to_fs(:currency, precision: 3) # => "$1,234,567,890.506"
34
+ # 1234567890.506.to_fs(:currency, round_mode: :down) # => "$1,234,567,890.50"
35
+ # 1234567890.506.to_fs(:currency, locale: :fr) # => "1 234 567 890,51 €"
36
+ # -1234567890.50.to_fs(:currency, negative_format: '(%u%n)')
35
37
  # # => "($1,234,567,890.50)"
36
- # 1234567890.50.to_s(:currency, unit: '&pound;', separator: ',', delimiter: '')
38
+ # 1234567890.50.to_fs(:currency, unit: '&pound;', separator: ',', delimiter: '')
37
39
  # # => "&pound;1234567890,50"
38
- # 1234567890.50.to_s(:currency, unit: '&pound;', separator: ',', delimiter: '', format: '%n %u')
40
+ # 1234567890.50.to_fs(:currency, unit: '&pound;', separator: ',', delimiter: '', format: '%n %u')
39
41
  # # => "1234567890,50 &pound;"
40
42
  #
41
43
  # Percentage:
42
- # 100.to_s(:percentage) # => "100.000%"
43
- # 100.to_s(:percentage, precision: 0) # => "100%"
44
- # 1000.to_s(:percentage, delimiter: '.', separator: ',') # => "1.000,000%"
45
- # 302.24398923423.to_s(:percentage, precision: 5) # => "302.24399%"
46
- # 1000.to_s(:percentage, locale: :fr) # => "1 000,000%"
47
- # 100.to_s(:percentage, format: '%n %') # => "100.000 %"
44
+ # 100.to_fs(:percentage) # => "100.000%"
45
+ # 100.to_fs(:percentage, precision: 0) # => "100%"
46
+ # 1000.to_fs(:percentage, delimiter: '.', separator: ',') # => "1.000,000%"
47
+ # 302.24398923423.to_fs(:percentage, precision: 5) # => "302.24399%"
48
+ # 302.24398923423.to_fs(:percentage, round_mode: :down) # => "302.243%"
49
+ # 1000.to_fs(:percentage, locale: :fr) # => "1 000,000%"
50
+ # 100.to_fs(:percentage, format: '%n %') # => "100.000 %"
48
51
  #
49
52
  # Delimited:
50
- # 12345678.to_s(:delimited) # => "12,345,678"
51
- # 12345678.05.to_s(:delimited) # => "12,345,678.05"
52
- # 12345678.to_s(:delimited, delimiter: '.') # => "12.345.678"
53
- # 12345678.to_s(:delimited, delimiter: ',') # => "12,345,678"
54
- # 12345678.05.to_s(:delimited, separator: ' ') # => "12,345,678 05"
55
- # 12345678.05.to_s(:delimited, locale: :fr) # => "12 345 678,05"
56
- # 98765432.98.to_s(:delimited, delimiter: ' ', separator: ',')
53
+ # 12345678.to_fs(:delimited) # => "12,345,678"
54
+ # 12345678.05.to_fs(:delimited) # => "12,345,678.05"
55
+ # 12345678.to_fs(:delimited, delimiter: '.') # => "12.345.678"
56
+ # 12345678.to_fs(:delimited, delimiter: ',') # => "12,345,678"
57
+ # 12345678.05.to_fs(:delimited, separator: ' ') # => "12,345,678 05"
58
+ # 12345678.05.to_fs(:delimited, locale: :fr) # => "12 345 678,05"
59
+ # 98765432.98.to_fs(:delimited, delimiter: ' ', separator: ',')
57
60
  # # => "98 765 432,98"
58
61
  #
59
62
  # Rounded:
60
- # 111.2345.to_s(:rounded) # => "111.235"
61
- # 111.2345.to_s(:rounded, precision: 2) # => "111.23"
62
- # 13.to_s(:rounded, precision: 5) # => "13.00000"
63
- # 389.32314.to_s(:rounded, precision: 0) # => "389"
64
- # 111.2345.to_s(:rounded, significant: true) # => "111"
65
- # 111.2345.to_s(:rounded, precision: 1, significant: true) # => "100"
66
- # 13.to_s(:rounded, precision: 5, significant: true) # => "13.000"
67
- # 111.234.to_s(:rounded, locale: :fr) # => "111,234"
68
- # 13.to_s(:rounded, precision: 5, significant: true, strip_insignificant_zeros: true)
63
+ # 111.2345.to_fs(:rounded) # => "111.235"
64
+ # 111.2345.to_fs(:rounded, precision: 2) # => "111.23"
65
+ # 111.2345.to_fs(:rounded, precision: 2, round_mode: :up) # => "111.24"
66
+ # 13.to_fs(:rounded, precision: 5) # => "13.00000"
67
+ # 389.32314.to_fs(:rounded, precision: 0) # => "389"
68
+ # 111.2345.to_fs(:rounded, significant: true) # => "111"
69
+ # 111.2345.to_fs(:rounded, precision: 1, significant: true) # => "100"
70
+ # 13.to_fs(:rounded, precision: 5, significant: true) # => "13.000"
71
+ # 111.234.to_fs(:rounded, locale: :fr) # => "111,234"
72
+ # 13.to_fs(:rounded, precision: 5, significant: true, strip_insignificant_zeros: true)
69
73
  # # => "13"
70
- # 389.32314.to_s(:rounded, precision: 4, significant: true) # => "389.3"
71
- # 1111.2345.to_s(:rounded, precision: 2, separator: ',', delimiter: '.')
74
+ # 389.32314.to_fs(:rounded, precision: 4, significant: true) # => "389.3"
75
+ # 1111.2345.to_fs(:rounded, precision: 2, separator: ',', delimiter: '.')
72
76
  # # => "1.111,23"
73
77
  #
74
78
  # Human-friendly size in Bytes:
75
- # 123.to_s(:human_size) # => "123 Bytes"
76
- # 1234.to_s(:human_size) # => "1.21 KB"
77
- # 12345.to_s(:human_size) # => "12.1 KB"
78
- # 1234567.to_s(:human_size) # => "1.18 MB"
79
- # 1234567890.to_s(:human_size) # => "1.15 GB"
80
- # 1234567890123.to_s(:human_size) # => "1.12 TB"
81
- # 1234567890123456.to_s(:human_size) # => "1.1 PB"
82
- # 1234567890123456789.to_s(:human_size) # => "1.07 EB"
83
- # 1234567.to_s(:human_size, precision: 2) # => "1.2 MB"
84
- # 483989.to_s(:human_size, precision: 2) # => "470 KB"
85
- # 1234567.to_s(:human_size, precision: 2, separator: ',') # => "1,2 MB"
86
- # 1234567890123.to_s(:human_size, precision: 5) # => "1.1228 TB"
87
- # 524288000.to_s(:human_size, precision: 5) # => "500 MB"
79
+ # 123.to_fs(:human_size) # => "123 Bytes"
80
+ # 1234.to_fs(:human_size) # => "1.21 KB"
81
+ # 12345.to_fs(:human_size) # => "12.1 KB"
82
+ # 1234567.to_fs(:human_size) # => "1.18 MB"
83
+ # 1234567890.to_fs(:human_size) # => "1.15 GB"
84
+ # 1234567890123.to_fs(:human_size) # => "1.12 TB"
85
+ # 1234567890123456.to_fs(:human_size) # => "1.1 PB"
86
+ # 1234567890123456789.to_fs(:human_size) # => "1.07 EB"
87
+ # 1234567.to_fs(:human_size, precision: 2) # => "1.2 MB"
88
+ # 1234567.to_fs(:human_size, precision: 2, round_mode: :up) # => "1.3 MB"
89
+ # 483989.to_fs(:human_size, precision: 2) # => "470 KB"
90
+ # 1234567.to_fs(:human_size, precision: 2, separator: ',') # => "1,2 MB"
91
+ # 1234567890123.to_fs(:human_size, precision: 5) # => "1.1228 TB"
92
+ # 524288000.to_fs(:human_size, precision: 5) # => "500 MB"
88
93
  #
89
94
  # Human-friendly format:
90
- # 123.to_s(:human) # => "123"
91
- # 1234.to_s(:human) # => "1.23 Thousand"
92
- # 12345.to_s(:human) # => "12.3 Thousand"
93
- # 1234567.to_s(:human) # => "1.23 Million"
94
- # 1234567890.to_s(:human) # => "1.23 Billion"
95
- # 1234567890123.to_s(:human) # => "1.23 Trillion"
96
- # 1234567890123456.to_s(:human) # => "1.23 Quadrillion"
97
- # 1234567890123456789.to_s(:human) # => "1230 Quadrillion"
98
- # 489939.to_s(:human, precision: 2) # => "490 Thousand"
99
- # 489939.to_s(:human, precision: 4) # => "489.9 Thousand"
100
- # 1234567.to_s(:human, precision: 4,
101
- # significant: false) # => "1.2346 Million"
102
- # 1234567.to_s(:human, precision: 1,
95
+ # 123.to_fs(:human) # => "123"
96
+ # 1234.to_fs(:human) # => "1.23 Thousand"
97
+ # 12345.to_fs(:human) # => "12.3 Thousand"
98
+ # 1234567.to_fs(:human) # => "1.23 Million"
99
+ # 1234567890.to_fs(:human) # => "1.23 Billion"
100
+ # 1234567890123.to_fs(:human) # => "1.23 Trillion"
101
+ # 1234567890123456.to_fs(:human) # => "1.23 Quadrillion"
102
+ # 1234567890123456789.to_fs(:human) # => "1230 Quadrillion"
103
+ # 489939.to_fs(:human, precision: 2) # => "490 Thousand"
104
+ # 489939.to_fs(:human, precision: 2, round_mode: :down) # => "480 Thousand"
105
+ # 489939.to_fs(:human, precision: 4) # => "489.9 Thousand"
106
+ # 1234567.to_fs(:human, precision: 4,
107
+ # significant: false) # => "1.2346 Million"
108
+ # 1234567.to_fs(:human, precision: 1,
103
109
  # separator: ',',
104
- # significant: false) # => "1,2 Million"
105
- def to_s(format = nil, options = nil)
110
+ # significant: false) # => "1,2 Million"
111
+ def to_fs(format = nil, options = nil)
112
+ return to_s if format.nil?
113
+
106
114
  case format
107
- when nil
108
- super()
109
115
  when Integer, String
110
- super(format)
116
+ to_s(format)
111
117
  when :phone
112
118
  ActiveSupport::NumberHelper.number_to_phone(self, options || {})
113
119
  when :currency
@@ -123,11 +129,12 @@ module ActiveSupport
123
129
  when :human_size
124
130
  ActiveSupport::NumberHelper.number_to_human_size(self, options || {})
125
131
  when Symbol
126
- super()
132
+ to_s
127
133
  else
128
- super(format)
134
+ to_s(format)
129
135
  end
130
136
  end
137
+ alias_method :to_formatted_s, :to_fs
131
138
  end
132
139
  end
133
140
 
@@ -0,0 +1,60 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveSupport
4
+ module DeprecatedNumericWithFormat # :nodoc:
5
+ def to_s(format = nil, options = nil)
6
+ return super() if format.nil?
7
+
8
+ case format
9
+ when Integer, String
10
+ super(format)
11
+ when :phone
12
+ ActiveSupport::Deprecation.warn(
13
+ "#{self.class}#to_s(#{format.inspect}) is deprecated. Please use #{self.class}#to_fs(#{format.inspect}) instead."
14
+ )
15
+ ActiveSupport::NumberHelper.number_to_phone(self, options || {})
16
+ when :currency
17
+ ActiveSupport::Deprecation.warn(
18
+ "#{self.class}#to_s(#{format.inspect}) is deprecated. Please use #{self.class}#to_fs(#{format.inspect}) instead."
19
+ )
20
+ ActiveSupport::NumberHelper.number_to_currency(self, options || {})
21
+ when :percentage
22
+ ActiveSupport::Deprecation.warn(
23
+ "#{self.class}#to_s(#{format.inspect}) is deprecated. Please use #{self.class}#to_fs(#{format.inspect}) instead."
24
+ )
25
+ ActiveSupport::NumberHelper.number_to_percentage(self, options || {})
26
+ when :delimited
27
+ ActiveSupport::Deprecation.warn(
28
+ "#{self.class}#to_s(#{format.inspect}) is deprecated. Please use #{self.class}#to_fs(#{format.inspect}) instead."
29
+ )
30
+ ActiveSupport::NumberHelper.number_to_delimited(self, options || {})
31
+ when :rounded
32
+ ActiveSupport::Deprecation.warn(
33
+ "#{self.class}#to_s(#{format.inspect}) is deprecated. Please use #{self.class}#to_fs(#{format.inspect}) instead."
34
+ )
35
+ ActiveSupport::NumberHelper.number_to_rounded(self, options || {})
36
+ when :human
37
+ ActiveSupport::Deprecation.warn(
38
+ "#{self.class}#to_s(#{format.inspect}) is deprecated. Please use #{self.class}#to_fs(#{format.inspect}) instead."
39
+ )
40
+ ActiveSupport::NumberHelper.number_to_human(self, options || {})
41
+ when :human_size
42
+ ActiveSupport::Deprecation.warn(
43
+ "#{self.class}#to_s(#{format.inspect}) is deprecated. Please use #{self.class}#to_fs(#{format.inspect}) instead."
44
+ )
45
+ ActiveSupport::NumberHelper.number_to_human_size(self, options || {})
46
+ when Symbol
47
+ ActiveSupport::Deprecation.warn(
48
+ "#{self.class}#to_s(#{format.inspect}) is deprecated. Please use #{self.class}#to_fs(#{format.inspect}) instead."
49
+ )
50
+ super()
51
+ else
52
+ super(format)
53
+ end
54
+ end
55
+ end
56
+ end
57
+
58
+ Integer.prepend ActiveSupport::DeprecatedNumericWithFormat
59
+ Float.prepend ActiveSupport::DeprecatedNumericWithFormat
60
+ BigDecimal.prepend ActiveSupport::DeprecatedNumericWithFormat
@@ -3,3 +3,4 @@
3
3
  require "active_support/core_ext/numeric/bytes"
4
4
  require "active_support/core_ext/numeric/time"
5
5
  require "active_support/core_ext/numeric/conversions"
6
+ require "active_support/core_ext/numeric/deprecated_conversions" unless ENV["RAILS_DISABLE_DEPRECATED_TO_S_CONVERSION"]
@@ -1,11 +1,35 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Object
4
- # A duck-type assistant method. For example, Active Support extends Date
5
- # to define an <tt>acts_like_date?</tt> method, and extends Time to define
6
- # <tt>acts_like_time?</tt>. As a result, we can do <tt>x.acts_like?(:time)</tt> and
7
- # <tt>x.acts_like?(:date)</tt> to do duck-type-safe comparisons, since classes that
8
- # we want to act like Time simply need to define an <tt>acts_like_time?</tt> method.
4
+ # Provides a way to check whether some class acts like some other class based on the existence of
5
+ # an appropriately-named marker method.
6
+ #
7
+ # A class that provides the same interface as <tt>SomeClass</tt> may define a marker method named
8
+ # <tt>acts_like_some_class?</tt> to signal its compatibility to callers of
9
+ # <tt>acts_like?(:some_class)</tt>.
10
+ #
11
+ # For example, Active Support extends <tt>Date</tt> to define an <tt>acts_like_date?</tt> method,
12
+ # and extends <tt>Time</tt> to define <tt>acts_like_time?</tt>. As a result, developers can call
13
+ # <tt>x.acts_like?(:time)</tt> and <tt>x.acts_like?(:date)</tt> to test duck-type compatibility,
14
+ # and classes that are able to act like <tt>Time</tt> can also define an <tt>acts_like_time?</tt>
15
+ # method to interoperate.
16
+ #
17
+ # Note that the marker method is only expected to exist. It isn't called, so its body or return
18
+ # value are irrelevant.
19
+ #
20
+ # ==== Example: A class that provides the same interface as <tt>String</tt>
21
+ #
22
+ # This class may define:
23
+ #
24
+ # class Stringish
25
+ # def acts_like_string?
26
+ # end
27
+ # end
28
+ #
29
+ # Then client code can query for duck-type-safeness this way:
30
+ #
31
+ # Stringish.new.acts_like?(:string) # => true
32
+ #
9
33
  def acts_like?(duck)
10
34
  case duck
11
35
  when :time
@@ -131,7 +131,7 @@ class String
131
131
  end
132
132
  end
133
133
 
134
- class Numeric #:nodoc:
134
+ class Numeric # :nodoc:
135
135
  # No number is blank:
136
136
  #
137
137
  # 1.blank? # => false
@@ -143,7 +143,7 @@ class Numeric #:nodoc:
143
143
  end
144
144
  end
145
145
 
146
- class Time #:nodoc:
146
+ class Time # :nodoc:
147
147
  # No Time is blank:
148
148
  #
149
149
  # Time.now.blank? # => false
@@ -43,7 +43,7 @@ class Hash
43
43
  def deep_dup
44
44
  hash = dup
45
45
  each_pair do |key, value|
46
- if key.frozen? && ::String === key
46
+ if ::String === key || ::Symbol === key
47
47
  hash[key] = value.deep_dup
48
48
  else
49
49
  hash.delete(key)
@@ -47,3 +47,14 @@ class UnboundMethod
47
47
  false
48
48
  end
49
49
  end
50
+
51
+ require "singleton"
52
+
53
+ module Singleton
54
+ # Singleton instances are not duplicable:
55
+ #
56
+ # Class.new.include(Singleton).instance.dup # TypeError (can't dup instance of singleton
57
+ def duplicable?
58
+ false
59
+ end
60
+ end
@@ -1,8 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Hack to load json gem first so we can overwrite its to_json.
3
+ # Hack to load json gem first so we can override its to_json.
4
4
  require "json"
5
5
  require "bigdecimal"
6
+ require "ipaddr"
6
7
  require "uri/generic"
7
8
  require "pathname"
8
9
  require "active_support/core_ext/big_decimal/conversions" # for #to_s
@@ -18,8 +19,7 @@ require "active_support/core_ext/date/conversions"
18
19
  # The JSON gem adds a few modules to Ruby core classes containing :to_json definition, overwriting
19
20
  # their default behavior. That said, we need to define the basic to_json method in all of them,
20
21
  # otherwise they will always use to_json gem implementation, which is backwards incompatible in
21
- # several cases (for instance, the JSON implementation for Hash does not work) with inheritance
22
- # and consequently classes as ActiveSupport::OrderedHash cannot be serialized to json.
22
+ # several cases (for instance, the JSON implementation for Hash does not work) with inheritance.
23
23
  #
24
24
  # On the other hand, we should avoid conflict with ::JSON.{generate,dump}(obj). Unfortunately, the
25
25
  # JSON gem's encoder relies on its own to_json implementation to encode objects. Since it always
@@ -49,8 +49,14 @@ end
49
49
  klass.prepend(ActiveSupport::ToJsonWithActiveSupportEncoder)
50
50
  end
51
51
 
52
+ class Module
53
+ def as_json(options = nil) # :nodoc:
54
+ name
55
+ end
56
+ end
57
+
52
58
  class Object
53
- def as_json(options = nil) #:nodoc:
59
+ def as_json(options = nil) # :nodoc:
54
60
  if respond_to?(:to_hash)
55
61
  to_hash.as_json(options)
56
62
  else
@@ -59,44 +65,44 @@ class Object
59
65
  end
60
66
  end
61
67
 
62
- class Struct #:nodoc:
68
+ class Struct # :nodoc:
63
69
  def as_json(options = nil)
64
70
  Hash[members.zip(values)].as_json(options)
65
71
  end
66
72
  end
67
73
 
68
74
  class TrueClass
69
- def as_json(options = nil) #:nodoc:
75
+ def as_json(options = nil) # :nodoc:
70
76
  self
71
77
  end
72
78
  end
73
79
 
74
80
  class FalseClass
75
- def as_json(options = nil) #:nodoc:
81
+ def as_json(options = nil) # :nodoc:
76
82
  self
77
83
  end
78
84
  end
79
85
 
80
86
  class NilClass
81
- def as_json(options = nil) #:nodoc:
87
+ def as_json(options = nil) # :nodoc:
82
88
  self
83
89
  end
84
90
  end
85
91
 
86
92
  class String
87
- def as_json(options = nil) #:nodoc:
93
+ def as_json(options = nil) # :nodoc:
88
94
  self
89
95
  end
90
96
  end
91
97
 
92
98
  class Symbol
93
- def as_json(options = nil) #:nodoc:
99
+ def as_json(options = nil) # :nodoc:
94
100
  to_s
95
101
  end
96
102
  end
97
103
 
98
104
  class Numeric
99
- def as_json(options = nil) #:nodoc:
105
+ def as_json(options = nil) # :nodoc:
100
106
  self
101
107
  end
102
108
  end
@@ -104,7 +110,7 @@ end
104
110
  class Float
105
111
  # Encoding Infinity or NaN to JSON should return "null". The default returns
106
112
  # "Infinity" or "NaN" which are not valid JSON.
107
- def as_json(options = nil) #:nodoc:
113
+ def as_json(options = nil) # :nodoc:
108
114
  finite? ? self : nil
109
115
  end
110
116
  end
@@ -119,43 +125,43 @@ class BigDecimal
119
125
  # if the other end knows by contract that the data is supposed to be a
120
126
  # BigDecimal, it still has the chance to post-process the string and get the
121
127
  # real value.
122
- def as_json(options = nil) #:nodoc:
128
+ def as_json(options = nil) # :nodoc:
123
129
  finite? ? to_s : nil
124
130
  end
125
131
  end
126
132
 
127
133
  class Regexp
128
- def as_json(options = nil) #:nodoc:
134
+ def as_json(options = nil) # :nodoc:
129
135
  to_s
130
136
  end
131
137
  end
132
138
 
133
139
  module Enumerable
134
- def as_json(options = nil) #:nodoc:
140
+ def as_json(options = nil) # :nodoc:
135
141
  to_a.as_json(options)
136
142
  end
137
143
  end
138
144
 
139
145
  class IO
140
- def as_json(options = nil) #:nodoc:
146
+ def as_json(options = nil) # :nodoc:
141
147
  to_s
142
148
  end
143
149
  end
144
150
 
145
151
  class Range
146
- def as_json(options = nil) #:nodoc:
152
+ def as_json(options = nil) # :nodoc:
147
153
  to_s
148
154
  end
149
155
  end
150
156
 
151
157
  class Array
152
- def as_json(options = nil) #:nodoc:
158
+ def as_json(options = nil) # :nodoc:
153
159
  map { |v| options ? v.as_json(options.dup) : v.as_json }
154
160
  end
155
161
  end
156
162
 
157
163
  class Hash
158
- def as_json(options = nil) #:nodoc:
164
+ def as_json(options = nil) # :nodoc:
159
165
  # create a subset of the hash by applying :only or :except
160
166
  subset = if options
161
167
  if attrs = options[:only]
@@ -169,12 +175,16 @@ class Hash
169
175
  self
170
176
  end
171
177
 
172
- Hash[subset.map { |k, v| [k.to_s, options ? v.as_json(options.dup) : v.as_json] }]
178
+ result = {}
179
+ subset.each do |k, v|
180
+ result[k.to_s] = options ? v.as_json(options.dup) : v.as_json
181
+ end
182
+ result
173
183
  end
174
184
  end
175
185
 
176
186
  class Time
177
- def as_json(options = nil) #:nodoc:
187
+ def as_json(options = nil) # :nodoc:
178
188
  if ActiveSupport::JSON::Encoding.use_standard_json_time_format
179
189
  xmlschema(ActiveSupport::JSON::Encoding.time_precision)
180
190
  else
@@ -184,7 +194,7 @@ class Time
184
194
  end
185
195
 
186
196
  class Date
187
- def as_json(options = nil) #:nodoc:
197
+ def as_json(options = nil) # :nodoc:
188
198
  if ActiveSupport::JSON::Encoding.use_standard_json_time_format
189
199
  strftime("%Y-%m-%d")
190
200
  else
@@ -194,7 +204,7 @@ class Date
194
204
  end
195
205
 
196
206
  class DateTime
197
- def as_json(options = nil) #:nodoc:
207
+ def as_json(options = nil) # :nodoc:
198
208
  if ActiveSupport::JSON::Encoding.use_standard_json_time_format
199
209
  xmlschema(ActiveSupport::JSON::Encoding.time_precision)
200
210
  else
@@ -203,19 +213,25 @@ class DateTime
203
213
  end
204
214
  end
205
215
 
206
- class URI::Generic #:nodoc:
216
+ class URI::Generic # :nodoc:
217
+ def as_json(options = nil)
218
+ to_s
219
+ end
220
+ end
221
+
222
+ class Pathname # :nodoc:
207
223
  def as_json(options = nil)
208
224
  to_s
209
225
  end
210
226
  end
211
227
 
212
- class Pathname #:nodoc:
228
+ class IPAddr # :nodoc:
213
229
  def as_json(options = nil)
214
230
  to_s
215
231
  end
216
232
  end
217
233
 
218
- class Process::Status #:nodoc:
234
+ class Process::Status # :nodoc:
219
235
  def as_json(options = nil)
220
236
  { exitstatus: exitstatus, pid: pid }
221
237
  end
@@ -75,11 +75,11 @@ class Hash
75
75
  #
76
76
  # This method is also aliased as +to_param+.
77
77
  def to_query(namespace = nil)
78
- query = collect do |key, value|
78
+ query = filter_map do |key, value|
79
79
  unless (value.is_a?(Hash) || value.is_a?(Array)) && value.empty?
80
80
  value.to_query(namespace ? "#{namespace}[#{key}]" : key)
81
81
  end
82
- end.compact
82
+ end
83
83
 
84
84
  query.sort! unless namespace.to_s.include?("[]")
85
85
  query.join("&")