activesupport 4.2.11.1 → 5.2.4

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 (256) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +399 -440
  3. data/MIT-LICENSE +2 -2
  4. data/README.rdoc +4 -5
  5. data/lib/active_support/all.rb +5 -3
  6. data/lib/active_support/array_inquirer.rb +48 -0
  7. data/lib/active_support/backtrace_cleaner.rb +7 -5
  8. data/lib/active_support/benchmarkable.rb +6 -4
  9. data/lib/active_support/builder.rb +3 -1
  10. data/lib/active_support/cache/file_store.rb +41 -35
  11. data/lib/active_support/cache/mem_cache_store.rb +97 -88
  12. data/lib/active_support/cache/memory_store.rb +27 -30
  13. data/lib/active_support/cache/null_store.rb +7 -8
  14. data/lib/active_support/cache/redis_cache_store.rb +461 -0
  15. data/lib/active_support/cache/strategy/local_cache.rb +67 -34
  16. data/lib/active_support/cache/strategy/local_cache_middleware.rb +10 -9
  17. data/lib/active_support/cache.rb +287 -196
  18. data/lib/active_support/callbacks.rb +640 -590
  19. data/lib/active_support/concern.rb +11 -5
  20. data/lib/active_support/concurrency/load_interlock_aware_monitor.rb +17 -0
  21. data/lib/active_support/concurrency/share_lock.rb +227 -0
  22. data/lib/active_support/configurable.rb +8 -5
  23. data/lib/active_support/core_ext/array/access.rb +29 -1
  24. data/lib/active_support/core_ext/array/conversions.rb +22 -18
  25. data/lib/active_support/core_ext/array/extract_options.rb +2 -0
  26. data/lib/active_support/core_ext/array/grouping.rb +11 -18
  27. data/lib/active_support/core_ext/array/inquiry.rb +19 -0
  28. data/lib/active_support/core_ext/array/prepend_and_append.rb +5 -3
  29. data/lib/active_support/core_ext/array/wrap.rb +7 -4
  30. data/lib/active_support/core_ext/array.rb +9 -6
  31. data/lib/active_support/core_ext/benchmark.rb +3 -1
  32. data/lib/active_support/core_ext/big_decimal/conversions.rb +10 -12
  33. data/lib/active_support/core_ext/big_decimal.rb +3 -1
  34. data/lib/active_support/core_ext/class/attribute.rb +41 -22
  35. data/lib/active_support/core_ext/class/attribute_accessors.rb +3 -1
  36. data/lib/active_support/core_ext/class/subclasses.rb +20 -6
  37. data/lib/active_support/core_ext/class.rb +4 -3
  38. data/lib/active_support/core_ext/date/acts_like.rb +3 -1
  39. data/lib/active_support/core_ext/date/blank.rb +14 -0
  40. data/lib/active_support/core_ext/date/calculations.rb +11 -9
  41. data/lib/active_support/core_ext/date/conversions.rb +25 -23
  42. data/lib/active_support/core_ext/date/zones.rb +4 -2
  43. data/lib/active_support/core_ext/date.rb +6 -4
  44. data/lib/active_support/core_ext/date_and_time/calculations.rb +170 -58
  45. data/lib/active_support/core_ext/date_and_time/compatibility.rb +4 -3
  46. data/lib/active_support/core_ext/date_and_time/zones.rb +12 -12
  47. data/lib/active_support/core_ext/date_time/acts_like.rb +4 -2
  48. data/lib/active_support/core_ext/date_time/blank.rb +14 -0
  49. data/lib/active_support/core_ext/date_time/calculations.rb +36 -18
  50. data/lib/active_support/core_ext/date_time/compatibility.rb +8 -6
  51. data/lib/active_support/core_ext/date_time/conversions.rb +16 -12
  52. data/lib/active_support/core_ext/date_time.rb +7 -5
  53. data/lib/active_support/core_ext/digest/uuid.rb +7 -5
  54. data/lib/active_support/core_ext/digest.rb +3 -0
  55. data/lib/active_support/core_ext/enumerable.rb +101 -33
  56. data/lib/active_support/core_ext/file/atomic.rb +38 -31
  57. data/lib/active_support/core_ext/file.rb +3 -1
  58. data/lib/active_support/core_ext/hash/compact.rb +14 -9
  59. data/lib/active_support/core_ext/hash/conversions.rb +62 -41
  60. data/lib/active_support/core_ext/hash/deep_merge.rb +9 -13
  61. data/lib/active_support/core_ext/hash/except.rb +11 -8
  62. data/lib/active_support/core_ext/hash/indifferent_access.rb +4 -3
  63. data/lib/active_support/core_ext/hash/keys.rb +33 -27
  64. data/lib/active_support/core_ext/hash/reverse_merge.rb +5 -2
  65. data/lib/active_support/core_ext/hash/slice.rb +8 -8
  66. data/lib/active_support/core_ext/hash/transform_values.rb +14 -5
  67. data/lib/active_support/core_ext/hash.rb +11 -9
  68. data/lib/active_support/core_ext/integer/inflections.rb +3 -1
  69. data/lib/active_support/core_ext/integer/multiple.rb +2 -0
  70. data/lib/active_support/core_ext/integer/time.rb +11 -18
  71. data/lib/active_support/core_ext/integer.rb +5 -3
  72. data/lib/active_support/core_ext/kernel/agnostics.rb +2 -0
  73. data/lib/active_support/core_ext/kernel/concern.rb +5 -1
  74. data/lib/active_support/core_ext/kernel/reporting.rb +4 -84
  75. data/lib/active_support/core_ext/kernel/singleton_class.rb +2 -0
  76. data/lib/active_support/core_ext/kernel.rb +6 -5
  77. data/lib/active_support/core_ext/load_error.rb +3 -22
  78. data/lib/active_support/core_ext/marshal.rb +8 -8
  79. data/lib/active_support/core_ext/module/aliasing.rb +6 -44
  80. data/lib/active_support/core_ext/module/anonymous.rb +12 -1
  81. data/lib/active_support/core_ext/module/attr_internal.rb +8 -9
  82. data/lib/active_support/core_ext/module/attribute_accessors.rb +43 -40
  83. data/lib/active_support/core_ext/module/attribute_accessors_per_thread.rb +150 -0
  84. data/lib/active_support/core_ext/module/concerning.rb +11 -12
  85. data/lib/active_support/core_ext/module/delegation.rb +99 -29
  86. data/lib/active_support/core_ext/module/deprecation.rb +4 -2
  87. data/lib/active_support/core_ext/module/introspection.rb +9 -9
  88. data/lib/active_support/core_ext/module/reachable.rb +5 -2
  89. data/lib/active_support/core_ext/module/redefine_method.rb +49 -0
  90. data/lib/active_support/core_ext/module/remove_method.rb +8 -3
  91. data/lib/active_support/core_ext/module.rb +14 -11
  92. data/lib/active_support/core_ext/name_error.rb +22 -2
  93. data/lib/active_support/core_ext/numeric/bytes.rb +22 -0
  94. data/lib/active_support/core_ext/numeric/conversions.rb +78 -81
  95. data/lib/active_support/core_ext/numeric/inquiry.rb +28 -0
  96. data/lib/active_support/core_ext/numeric/time.rb +35 -23
  97. data/lib/active_support/core_ext/numeric.rb +6 -3
  98. data/lib/active_support/core_ext/object/acts_like.rb +12 -1
  99. data/lib/active_support/core_ext/object/blank.rb +27 -2
  100. data/lib/active_support/core_ext/object/conversions.rb +6 -4
  101. data/lib/active_support/core_ext/object/deep_dup.rb +13 -4
  102. data/lib/active_support/core_ext/object/duplicable.rb +41 -14
  103. data/lib/active_support/core_ext/object/inclusion.rb +5 -3
  104. data/lib/active_support/core_ext/object/instance_variables.rb +3 -1
  105. data/lib/active_support/core_ext/object/json.rb +49 -19
  106. data/lib/active_support/core_ext/object/to_param.rb +3 -1
  107. data/lib/active_support/core_ext/object/to_query.rb +10 -5
  108. data/lib/active_support/core_ext/object/try.rb +69 -21
  109. data/lib/active_support/core_ext/object/with_options.rb +16 -3
  110. data/lib/active_support/core_ext/object.rb +14 -13
  111. data/lib/active_support/core_ext/range/compare_range.rb +61 -0
  112. data/lib/active_support/core_ext/range/conversions.rb +27 -7
  113. data/lib/active_support/core_ext/range/each.rb +19 -17
  114. data/lib/active_support/core_ext/range/include_range.rb +2 -22
  115. data/lib/active_support/core_ext/range/include_time_with_zone.rb +23 -0
  116. data/lib/active_support/core_ext/range/overlaps.rb +2 -0
  117. data/lib/active_support/core_ext/range.rb +7 -4
  118. data/lib/active_support/core_ext/regexp.rb +6 -0
  119. data/lib/active_support/core_ext/securerandom.rb +25 -0
  120. data/lib/active_support/core_ext/string/access.rb +8 -6
  121. data/lib/active_support/core_ext/string/behavior.rb +3 -1
  122. data/lib/active_support/core_ext/string/conversions.rb +7 -4
  123. data/lib/active_support/core_ext/string/exclude.rb +2 -0
  124. data/lib/active_support/core_ext/string/filters.rb +6 -5
  125. data/lib/active_support/core_ext/string/indent.rb +6 -4
  126. data/lib/active_support/core_ext/string/inflections.rb +61 -24
  127. data/lib/active_support/core_ext/string/inquiry.rb +3 -1
  128. data/lib/active_support/core_ext/string/multibyte.rb +15 -7
  129. data/lib/active_support/core_ext/string/output_safety.rb +34 -38
  130. data/lib/active_support/core_ext/string/starts_ends_with.rb +2 -0
  131. data/lib/active_support/core_ext/string/strip.rb +4 -5
  132. data/lib/active_support/core_ext/string/zones.rb +4 -2
  133. data/lib/active_support/core_ext/string.rb +15 -13
  134. data/lib/active_support/core_ext/time/acts_like.rb +3 -1
  135. data/lib/active_support/core_ext/time/calculations.rb +85 -51
  136. data/lib/active_support/core_ext/time/compatibility.rb +4 -2
  137. data/lib/active_support/core_ext/time/conversions.rb +20 -13
  138. data/lib/active_support/core_ext/time/zones.rb +41 -7
  139. data/lib/active_support/core_ext/time.rb +7 -6
  140. data/lib/active_support/core_ext/uri.rb +6 -8
  141. data/lib/active_support/core_ext.rb +3 -1
  142. data/lib/active_support/current_attributes.rb +195 -0
  143. data/lib/active_support/dependencies/autoload.rb +2 -0
  144. data/lib/active_support/dependencies/interlock.rb +57 -0
  145. data/lib/active_support/dependencies.rb +152 -161
  146. data/lib/active_support/deprecation/behaviors.rb +44 -11
  147. data/lib/active_support/deprecation/constant_accessor.rb +52 -0
  148. data/lib/active_support/deprecation/instance_delegator.rb +17 -2
  149. data/lib/active_support/deprecation/method_wrappers.rb +66 -20
  150. data/lib/active_support/deprecation/proxy_wrappers.rb +56 -28
  151. data/lib/active_support/deprecation/reporting.rb +32 -12
  152. data/lib/active_support/deprecation.rb +12 -9
  153. data/lib/active_support/descendants_tracker.rb +2 -0
  154. data/lib/active_support/digest.rb +20 -0
  155. data/lib/active_support/duration/iso8601_parser.rb +125 -0
  156. data/lib/active_support/duration/iso8601_serializer.rb +55 -0
  157. data/lib/active_support/duration.rb +307 -35
  158. data/lib/active_support/encrypted_configuration.rb +49 -0
  159. data/lib/active_support/encrypted_file.rb +99 -0
  160. data/lib/active_support/evented_file_update_checker.rb +205 -0
  161. data/lib/active_support/execution_wrapper.rb +128 -0
  162. data/lib/active_support/executor.rb +8 -0
  163. data/lib/active_support/file_update_checker.rb +63 -37
  164. data/lib/active_support/gem_version.rb +6 -4
  165. data/lib/active_support/gzip.rb +7 -5
  166. data/lib/active_support/hash_with_indifferent_access.rb +123 -28
  167. data/lib/active_support/i18n.rb +8 -6
  168. data/lib/active_support/i18n_railtie.rb +37 -13
  169. data/lib/active_support/inflections.rb +13 -11
  170. data/lib/active_support/inflector/inflections.rb +61 -12
  171. data/lib/active_support/inflector/methods.rb +163 -136
  172. data/lib/active_support/inflector/transliterate.rb +48 -27
  173. data/lib/active_support/inflector.rb +7 -5
  174. data/lib/active_support/json/decoding.rb +16 -13
  175. data/lib/active_support/json/encoding.rb +11 -58
  176. data/lib/active_support/json.rb +4 -2
  177. data/lib/active_support/key_generator.rb +25 -25
  178. data/lib/active_support/lazy_load_hooks.rb +50 -20
  179. data/lib/active_support/locale/en.yml +2 -0
  180. data/lib/active_support/log_subscriber/test_helper.rb +14 -12
  181. data/lib/active_support/log_subscriber.rb +13 -10
  182. data/lib/active_support/logger.rb +8 -7
  183. data/lib/active_support/logger_silence.rb +6 -4
  184. data/lib/active_support/logger_thread_safe_level.rb +7 -5
  185. data/lib/active_support/message_encryptor.rb +168 -53
  186. data/lib/active_support/message_verifier.rb +150 -17
  187. data/lib/active_support/messages/metadata.rb +71 -0
  188. data/lib/active_support/messages/rotation_configuration.rb +22 -0
  189. data/lib/active_support/messages/rotator.rb +56 -0
  190. data/lib/active_support/multibyte/chars.rb +36 -23
  191. data/lib/active_support/multibyte/unicode.rb +100 -96
  192. data/lib/active_support/multibyte.rb +4 -2
  193. data/lib/active_support/notifications/fanout.rb +11 -9
  194. data/lib/active_support/notifications/instrumenter.rb +27 -7
  195. data/lib/active_support/notifications.rb +11 -7
  196. data/lib/active_support/number_helper/number_converter.rb +13 -11
  197. data/lib/active_support/number_helper/number_to_currency_converter.rb +9 -9
  198. data/lib/active_support/number_helper/number_to_delimited_converter.rb +9 -3
  199. data/lib/active_support/number_helper/number_to_human_converter.rb +11 -9
  200. data/lib/active_support/number_helper/number_to_human_size_converter.rb +9 -8
  201. data/lib/active_support/number_helper/number_to_percentage_converter.rb +3 -1
  202. data/lib/active_support/number_helper/number_to_phone_converter.rb +13 -4
  203. data/lib/active_support/number_helper/number_to_rounded_converter.rb +23 -56
  204. data/lib/active_support/number_helper/rounding_helper.rb +66 -0
  205. data/lib/active_support/number_helper.rb +94 -68
  206. data/lib/active_support/option_merger.rb +3 -1
  207. data/lib/active_support/ordered_hash.rb +6 -4
  208. data/lib/active_support/ordered_options.rb +23 -5
  209. data/lib/active_support/per_thread_registry.rb +9 -4
  210. data/lib/active_support/proxy_object.rb +2 -0
  211. data/lib/active_support/rails.rb +16 -8
  212. data/lib/active_support/railtie.rb +43 -9
  213. data/lib/active_support/reloader.rb +131 -0
  214. data/lib/active_support/rescuable.rb +108 -53
  215. data/lib/active_support/security_utils.rb +15 -11
  216. data/lib/active_support/string_inquirer.rb +11 -3
  217. data/lib/active_support/subscriber.rb +21 -16
  218. data/lib/active_support/tagged_logging.rb +14 -11
  219. data/lib/active_support/test_case.rb +19 -47
  220. data/lib/active_support/testing/assertions.rb +137 -20
  221. data/lib/active_support/testing/autorun.rb +4 -2
  222. data/lib/active_support/testing/constant_lookup.rb +2 -1
  223. data/lib/active_support/testing/declarative.rb +3 -1
  224. data/lib/active_support/testing/deprecation.rb +14 -10
  225. data/lib/active_support/testing/file_fixtures.rb +36 -0
  226. data/lib/active_support/testing/isolation.rb +34 -25
  227. data/lib/active_support/testing/method_call_assertions.rb +43 -0
  228. data/lib/active_support/testing/setup_and_teardown.rb +13 -8
  229. data/lib/active_support/testing/stream.rb +44 -0
  230. data/lib/active_support/testing/tagged_logging.rb +3 -1
  231. data/lib/active_support/testing/time_helpers.rb +81 -15
  232. data/lib/active_support/time.rb +14 -12
  233. data/lib/active_support/time_with_zone.rb +169 -39
  234. data/lib/active_support/values/time_zone.rb +196 -61
  235. data/lib/active_support/values/unicode_tables.dat +0 -0
  236. data/lib/active_support/version.rb +3 -1
  237. data/lib/active_support/xml_mini/jdom.rb +116 -114
  238. data/lib/active_support/xml_mini/libxml.rb +16 -13
  239. data/lib/active_support/xml_mini/libxmlsax.rb +15 -14
  240. data/lib/active_support/xml_mini/nokogiri.rb +14 -12
  241. data/lib/active_support/xml_mini/nokogirisax.rb +14 -13
  242. data/lib/active_support/xml_mini/rexml.rb +11 -9
  243. data/lib/active_support/xml_mini.rb +37 -37
  244. data/lib/active_support.rb +12 -11
  245. metadata +54 -24
  246. data/lib/active_support/concurrency/latch.rb +0 -27
  247. data/lib/active_support/core_ext/big_decimal/yaml_conversions.rb +0 -16
  248. data/lib/active_support/core_ext/class/delegating_attributes.rb +0 -45
  249. data/lib/active_support/core_ext/date_time/zones.rb +0 -6
  250. data/lib/active_support/core_ext/kernel/debugger.rb +0 -10
  251. data/lib/active_support/core_ext/module/method_transplanting.rb +0 -13
  252. data/lib/active_support/core_ext/module/qualified_const.rb +0 -52
  253. data/lib/active_support/core_ext/object/itself.rb +0 -15
  254. data/lib/active_support/core_ext/struct.rb +0 -6
  255. data/lib/active_support/core_ext/thread.rb +0 -86
  256. data/lib/active_support/core_ext/time/marshal.rb +0 -30
@@ -1,9 +1,12 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveSupport
2
4
  module NumberHelper
3
5
  extend ActiveSupport::Autoload
4
6
 
5
7
  eager_autoload do
6
8
  autoload :NumberConverter
9
+ autoload :RoundingHelper
7
10
  autoload :NumberToRoundedConverter
8
11
  autoload :NumberToDelimitedConverter
9
12
  autoload :NumberToHumanConverter
@@ -15,7 +18,7 @@ module ActiveSupport
15
18
 
16
19
  extend self
17
20
 
18
- # Formats a +number+ into a US phone number (e.g., (555)
21
+ # Formats a +number+ into a phone number (US by default e.g., (555)
19
22
  # 123-9876). You can customize the format in the +options+ hash.
20
23
  #
21
24
  # ==== Options
@@ -27,19 +30,26 @@ module ActiveSupport
27
30
  # end of the generated number.
28
31
  # * <tt>:country_code</tt> - Sets the country code for the phone
29
32
  # number.
33
+ # * <tt>:pattern</tt> - Specifies how the number is divided into three
34
+ # groups with the custom regexp to override the default format.
30
35
  # ==== Examples
31
36
  #
32
- # number_to_phone(5551234) # => 555-1234
33
- # number_to_phone('5551234') # => 555-1234
34
- # number_to_phone(1235551234) # => 123-555-1234
35
- # number_to_phone(1235551234, area_code: true) # => (123) 555-1234
36
- # number_to_phone(1235551234, delimiter: ' ') # => 123 555 1234
37
- # number_to_phone(1235551234, area_code: true, extension: 555) # => (123) 555-1234 x 555
38
- # number_to_phone(1235551234, country_code: 1) # => +1-123-555-1234
39
- # number_to_phone('123a456') # => 123a456
37
+ # number_to_phone(5551234) # => "555-1234"
38
+ # number_to_phone('5551234') # => "555-1234"
39
+ # number_to_phone(1235551234) # => "123-555-1234"
40
+ # number_to_phone(1235551234, area_code: true) # => "(123) 555-1234"
41
+ # number_to_phone(1235551234, delimiter: ' ') # => "123 555 1234"
42
+ # number_to_phone(1235551234, area_code: true, extension: 555) # => "(123) 555-1234 x 555"
43
+ # number_to_phone(1235551234, country_code: 1) # => "+1-123-555-1234"
44
+ # number_to_phone('123a456') # => "123a456"
40
45
  #
41
46
  # number_to_phone(1235551234, country_code: 1, extension: 1343, delimiter: '.')
42
- # # => +1.123.555.1234 x 1343
47
+ # # => "+1.123.555.1234 x 1343"
48
+ #
49
+ # number_to_phone(75561234567, pattern: /(\d{1,4})(\d{4})(\d{4})$/, area_code: true)
50
+ # # => "(755) 6123-4567"
51
+ # number_to_phone(13312345678, pattern: /(\d{3})(\d{4})(\d{4})$/)
52
+ # # => "133-1234-5678"
43
53
  def number_to_phone(number, options = {})
44
54
  NumberToPhoneConverter.convert(number, options)
45
55
  end
@@ -47,6 +57,14 @@ module ActiveSupport
47
57
  # Formats a +number+ into a currency string (e.g., $13.65). You
48
58
  # can customize the format in the +options+ hash.
49
59
  #
60
+ # The currency unit and number formatting of the current locale will be used
61
+ # unless otherwise specified in the provided options. No currency conversion
62
+ # is performed. If the user is given a way to change their locale, they will
63
+ # also be able to change the relative value of the currency displayed with
64
+ # this helper. If your application will ever support multiple locales, you
65
+ # may want to specify a constant <tt>:locale</tt> option or consider
66
+ # using a library capable of currency conversion.
67
+ #
50
68
  # ==== Options
51
69
  #
52
70
  # * <tt>:locale</tt> - Sets the locale to be used for formatting
@@ -63,25 +81,25 @@ module ActiveSupport
63
81
  # (defaults to "%u%n"). Fields are <tt>%u</tt> for the
64
82
  # currency, and <tt>%n</tt> for the number.
65
83
  # * <tt>:negative_format</tt> - Sets the format for negative
66
- # numbers (defaults to prepending an hyphen to the formatted
84
+ # numbers (defaults to prepending a hyphen to the formatted
67
85
  # number given by <tt>:format</tt>). Accepts the same fields
68
86
  # than <tt>:format</tt>, except <tt>%n</tt> is here the
69
87
  # absolute value of the number.
70
88
  #
71
89
  # ==== Examples
72
90
  #
73
- # number_to_currency(1234567890.50) # => $1,234,567,890.50
74
- # number_to_currency(1234567890.506) # => $1,234,567,890.51
75
- # number_to_currency(1234567890.506, precision: 3) # => $1,234,567,890.506
76
- # number_to_currency(1234567890.506, locale: :fr) # => 1 234 567 890,51 €
77
- # number_to_currency('123a456') # => $123a456
91
+ # number_to_currency(1234567890.50) # => "$1,234,567,890.50"
92
+ # number_to_currency(1234567890.506) # => "$1,234,567,890.51"
93
+ # number_to_currency(1234567890.506, precision: 3) # => "$1,234,567,890.506"
94
+ # number_to_currency(1234567890.506, locale: :fr) # => "1 234 567 890,51 €"
95
+ # number_to_currency('123a456') # => "$123a456"
78
96
  #
79
97
  # number_to_currency(-1234567890.50, negative_format: '(%u%n)')
80
- # # => ($1,234,567,890.50)
98
+ # # => "($1,234,567,890.50)"
81
99
  # number_to_currency(1234567890.50, unit: '&pound;', separator: ',', delimiter: '')
82
- # # => &pound;1234567890,50
100
+ # # => "&pound;1234567890,50"
83
101
  # number_to_currency(1234567890.50, unit: '&pound;', separator: ',', delimiter: '', format: '%n %u')
84
- # # => 1234567890,50 &pound;
102
+ # # => "1234567890,50 &pound;"
85
103
  def number_to_currency(number, options = {})
86
104
  NumberToCurrencyConverter.convert(number, options)
87
105
  end
@@ -94,9 +112,9 @@ module ActiveSupport
94
112
  # * <tt>:locale</tt> - Sets the locale to be used for formatting
95
113
  # (defaults to current locale).
96
114
  # * <tt>:precision</tt> - Sets the precision of the number
97
- # (defaults to 3).
98
- # * <tt>:significant</tt> - If +true+, precision will be the #
99
- # of significant_digits. If +false+, the # of fractional
115
+ # (defaults to 3). Keeps the number's precision if +nil+.
116
+ # * <tt>:significant</tt> - If +true+, precision will be the number
117
+ # of significant_digits. If +false+, the number of fractional
100
118
  # digits (defaults to +false+).
101
119
  # * <tt>:separator</tt> - Sets the separator between the
102
120
  # fractional and integer digits (defaults to ".").
@@ -110,14 +128,15 @@ module ActiveSupport
110
128
  #
111
129
  # ==== Examples
112
130
  #
113
- # number_to_percentage(100) # => 100.000%
114
- # number_to_percentage('98') # => 98.000%
115
- # number_to_percentage(100, precision: 0) # => 100%
116
- # number_to_percentage(1000, delimiter: '.', separator: ',') # => 1.000,000%
117
- # number_to_percentage(302.24398923423, precision: 5) # => 302.24399%
118
- # number_to_percentage(1000, locale: :fr) # => 1 000,000%
119
- # number_to_percentage('98a') # => 98a%
120
- # number_to_percentage(100, format: '%n %') # => 100 %
131
+ # number_to_percentage(100) # => "100.000%"
132
+ # number_to_percentage('98') # => "98.000%"
133
+ # number_to_percentage(100, precision: 0) # => "100%"
134
+ # number_to_percentage(1000, delimiter: '.', separator: ',') # => "1.000,000%"
135
+ # number_to_percentage(302.24398923423, precision: 5) # => "302.24399%"
136
+ # number_to_percentage(1000, locale: :fr) # => "1000,000%"
137
+ # number_to_percentage(1000, precision: nil) # => "1000%"
138
+ # number_to_percentage('98a') # => "98a%"
139
+ # number_to_percentage(100, format: '%n %') # => "100.000 %"
121
140
  def number_to_percentage(number, options = {})
122
141
  NumberToPercentageConverter.convert(number, options)
123
142
  end
@@ -134,19 +153,25 @@ module ActiveSupport
134
153
  # to ",").
135
154
  # * <tt>:separator</tt> - Sets the separator between the
136
155
  # fractional and integer digits (defaults to ".").
156
+ # * <tt>:delimiter_pattern</tt> - Sets a custom regular expression used for
157
+ # deriving the placement of delimiter. Helpful when using currency formats
158
+ # like INR.
137
159
  #
138
160
  # ==== Examples
139
161
  #
140
- # number_to_delimited(12345678) # => 12,345,678
141
- # number_to_delimited('123456') # => 123,456
142
- # number_to_delimited(12345678.05) # => 12,345,678.05
143
- # number_to_delimited(12345678, delimiter: '.') # => 12.345.678
144
- # number_to_delimited(12345678, delimiter: ',') # => 12,345,678
145
- # number_to_delimited(12345678.05, separator: ' ') # => 12,345,678 05
146
- # number_to_delimited(12345678.05, locale: :fr) # => 12 345 678,05
147
- # number_to_delimited('112a') # => 112a
162
+ # number_to_delimited(12345678) # => "12,345,678"
163
+ # number_to_delimited('123456') # => "123,456"
164
+ # number_to_delimited(12345678.05) # => "12,345,678.05"
165
+ # number_to_delimited(12345678, delimiter: '.') # => "12.345.678"
166
+ # number_to_delimited(12345678, delimiter: ',') # => "12,345,678"
167
+ # number_to_delimited(12345678.05, separator: ' ') # => "12,345,678 05"
168
+ # number_to_delimited(12345678.05, locale: :fr) # => "12 345 678,05"
169
+ # number_to_delimited('112a') # => "112a"
148
170
  # number_to_delimited(98765432.98, delimiter: ' ', separator: ',')
149
- # # => 98 765 432,98
171
+ # # => "98 765 432,98"
172
+ # number_to_delimited("123456.78",
173
+ # delimiter_pattern: /(\d+?)(?=(\d\d)+(\d)(?!\d))/)
174
+ # # => "1,23,456.78"
150
175
  def number_to_delimited(number, options = {})
151
176
  NumberToDelimitedConverter.convert(number, options)
152
177
  end
@@ -161,9 +186,9 @@ module ActiveSupport
161
186
  # * <tt>:locale</tt> - Sets the locale to be used for formatting
162
187
  # (defaults to current locale).
163
188
  # * <tt>:precision</tt> - Sets the precision of the number
164
- # (defaults to 3).
165
- # * <tt>:significant</tt> - If +true+, precision will be the #
166
- # of significant_digits. If +false+, the # of fractional
189
+ # (defaults to 3). Keeps the number's precision if +nil+.
190
+ # * <tt>:significant</tt> - If +true+, precision will be the number
191
+ # of significant_digits. If +false+, the number of fractional
167
192
  # digits (defaults to +false+).
168
193
  # * <tt>:separator</tt> - Sets the separator between the
169
194
  # fractional and integer digits (defaults to ".").
@@ -175,21 +200,22 @@ module ActiveSupport
175
200
  #
176
201
  # ==== Examples
177
202
  #
178
- # number_to_rounded(111.2345) # => 111.235
179
- # number_to_rounded(111.2345, precision: 2) # => 111.23
180
- # number_to_rounded(13, precision: 5) # => 13.00000
181
- # number_to_rounded(389.32314, precision: 0) # => 389
182
- # number_to_rounded(111.2345, significant: true) # => 111
183
- # number_to_rounded(111.2345, precision: 1, significant: true) # => 100
184
- # number_to_rounded(13, precision: 5, significant: true) # => 13.000
185
- # number_to_rounded(111.234, locale: :fr) # => 111,234
203
+ # number_to_rounded(111.2345) # => "111.235"
204
+ # number_to_rounded(111.2345, precision: 2) # => "111.23"
205
+ # number_to_rounded(13, precision: 5) # => "13.00000"
206
+ # number_to_rounded(389.32314, precision: 0) # => "389"
207
+ # number_to_rounded(111.2345, significant: true) # => "111"
208
+ # number_to_rounded(111.2345, precision: 1, significant: true) # => "100"
209
+ # number_to_rounded(13, precision: 5, significant: true) # => "13.000"
210
+ # number_to_rounded(13, precision: nil) # => "13"
211
+ # number_to_rounded(111.234, locale: :fr) # => "111,234"
186
212
  #
187
213
  # number_to_rounded(13, precision: 5, significant: true, strip_insignificant_zeros: true)
188
- # # => 13
214
+ # # => "13"
189
215
  #
190
- # number_to_rounded(389.32314, precision: 4, significant: true) # => 389.3
216
+ # number_to_rounded(389.32314, precision: 4, significant: true) # => "389.3"
191
217
  # number_to_rounded(1111.2345, precision: 2, separator: ',', delimiter: '.')
192
- # # => 1.111,23
218
+ # # => "1.111,23"
193
219
  def number_to_rounded(number, options = {})
194
220
  NumberToRoundedConverter.convert(number, options)
195
221
  end
@@ -208,8 +234,8 @@ module ActiveSupport
208
234
  # (defaults to current locale).
209
235
  # * <tt>:precision</tt> - Sets the precision of the number
210
236
  # (defaults to 3).
211
- # * <tt>:significant</tt> - If +true+, precision will be the #
212
- # of significant_digits. If +false+, the # of fractional
237
+ # * <tt>:significant</tt> - If +true+, precision will be the number
238
+ # of significant_digits. If +false+, the number of fractional
213
239
  # digits (defaults to +true+)
214
240
  # * <tt>:separator</tt> - Sets the separator between the
215
241
  # fractional and integer digits (defaults to ".").
@@ -218,20 +244,20 @@ module ActiveSupport
218
244
  # * <tt>:strip_insignificant_zeros</tt> - If +true+ removes
219
245
  # insignificant zeros after the decimal separator (defaults to
220
246
  # +true+)
221
- # * <tt>:prefix</tt> - If +:si+ formats the number using the SI
222
- # prefix (defaults to :binary)
223
247
  #
224
248
  # ==== Examples
225
249
  #
226
- # number_to_human_size(123) # => 123 Bytes
227
- # number_to_human_size(1234) # => 1.21 KB
228
- # number_to_human_size(12345) # => 12.1 KB
229
- # number_to_human_size(1234567) # => 1.18 MB
230
- # number_to_human_size(1234567890) # => 1.15 GB
231
- # number_to_human_size(1234567890123) # => 1.12 TB
232
- # number_to_human_size(1234567, precision: 2) # => 1.2 MB
233
- # number_to_human_size(483989, precision: 2) # => 470 KB
234
- # number_to_human_size(1234567, precision: 2, separator: ',') # => 1,2 MB
250
+ # number_to_human_size(123) # => "123 Bytes"
251
+ # number_to_human_size(1234) # => "1.21 KB"
252
+ # number_to_human_size(12345) # => "12.1 KB"
253
+ # number_to_human_size(1234567) # => "1.18 MB"
254
+ # number_to_human_size(1234567890) # => "1.15 GB"
255
+ # number_to_human_size(1234567890123) # => "1.12 TB"
256
+ # number_to_human_size(1234567890123456) # => "1.1 PB"
257
+ # number_to_human_size(1234567890123456789) # => "1.07 EB"
258
+ # number_to_human_size(1234567, precision: 2) # => "1.2 MB"
259
+ # number_to_human_size(483989, precision: 2) # => "470 KB"
260
+ # number_to_human_size(1234567, precision: 2, separator: ',') # => "1,2 MB"
235
261
  # number_to_human_size(1234567890123, precision: 5) # => "1.1228 TB"
236
262
  # number_to_human_size(524288000, precision: 5) # => "500 MB"
237
263
  def number_to_human_size(number, options = {})
@@ -258,8 +284,8 @@ module ActiveSupport
258
284
  # (defaults to current locale).
259
285
  # * <tt>:precision</tt> - Sets the precision of the number
260
286
  # (defaults to 3).
261
- # * <tt>:significant</tt> - If +true+, precision will be the #
262
- # of significant_digits. If +false+, the # of fractional
287
+ # * <tt>:significant</tt> - If +true+, precision will be the number
288
+ # of significant_digits. If +false+, the number of fractional
263
289
  # digits (defaults to +true+)
264
290
  # * <tt>:separator</tt> - Sets the separator between the
265
291
  # fractional and integer digits (defaults to ".").
@@ -1,4 +1,6 @@
1
- require 'active_support/core_ext/hash/deep_merge'
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/core_ext/hash/deep_merge"
2
4
 
3
5
  module ActiveSupport
4
6
  class OptionMerger #:nodoc:
@@ -1,11 +1,13 @@
1
- require 'yaml'
1
+ # frozen_string_literal: true
2
+
3
+ require "yaml"
2
4
 
3
5
  YAML.add_builtin_type("omap") do |type, val|
4
- ActiveSupport::OrderedHash[val.map{ |v| v.to_a.first }]
6
+ ActiveSupport::OrderedHash[val.map { |v| v.to_a.first }]
5
7
  end
6
8
 
7
9
  module ActiveSupport
8
- # <tt>ActiveSupport::OrderedHash</tt> implements a hash that preserves
10
+ # DEPRECATED: <tt>ActiveSupport::OrderedHash</tt> implements a hash that preserves
9
11
  # insertion order.
10
12
  #
11
13
  # oh = ActiveSupport::OrderedHash.new
@@ -25,7 +27,7 @@ module ActiveSupport
25
27
  end
26
28
 
27
29
  def encode_with(coder)
28
- coder.represent_seq '!omap', map { |k,v| { k => v } }
30
+ coder.represent_seq "!omap", map { |k, v| { k => v } }
29
31
  end
30
32
 
31
33
  def select(*args, &block)
@@ -1,3 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/core_ext/object/blank"
4
+
1
5
  module ActiveSupport
2
6
  # Usually key value pairs are handled something like this:
3
7
  #
@@ -6,6 +10,7 @@ module ActiveSupport
6
10
  # h[:girl] = 'Mary'
7
11
  # h[:boy] # => 'John'
8
12
  # h[:girl] # => 'Mary'
13
+ # h[:dog] # => nil
9
14
  #
10
15
  # Using +OrderedOptions+, the above code could be reduced to:
11
16
  #
@@ -14,6 +19,13 @@ module ActiveSupport
14
19
  # h.girl = 'Mary'
15
20
  # h.boy # => 'John'
16
21
  # h.girl # => 'Mary'
22
+ # h.dog # => nil
23
+ #
24
+ # To raise an exception when the value is blank, append a
25
+ # bang to the key name, like:
26
+ #
27
+ # h.dog! # => raises KeyError: :dog is blank
28
+ #
17
29
  class OrderedOptions < Hash
18
30
  alias_method :_get, :[] # preserve the original #[] method
19
31
  protected :_get # make it protected
@@ -27,11 +39,17 @@ module ActiveSupport
27
39
  end
28
40
 
29
41
  def method_missing(name, *args)
30
- name_string = name.to_s
31
- if name_string.chomp!('=')
42
+ name_string = name.to_s.dup
43
+ if name_string.chomp!("=")
32
44
  self[name_string] = args.first
33
45
  else
34
- self[name]
46
+ bangs = name_string.chomp!("!")
47
+
48
+ if bangs
49
+ self[name_string].presence || raise(KeyError.new(":#{name_string} is blank"))
50
+ else
51
+ self[name_string]
52
+ end
35
53
  end
36
54
  end
37
55
 
@@ -52,9 +70,9 @@ module ActiveSupport
52
70
  def initialize(parent = nil)
53
71
  if parent.kind_of?(OrderedOptions)
54
72
  # use the faster _get when dealing with OrderedOptions
55
- super() { |h,k| parent._get(k) }
73
+ super() { |h, k| parent._get(k) }
56
74
  elsif parent
57
- super() { |h,k| parent[k] }
75
+ super() { |h, k| parent[k] }
58
76
  else
59
77
  super()
60
78
  end
@@ -1,6 +1,11 @@
1
- require 'active_support/core_ext/module/delegation'
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/core_ext/module/delegation"
2
4
 
3
5
  module ActiveSupport
6
+ # NOTE: This approach has been deprecated for end-user code in favor of {thread_mattr_accessor}[rdoc-ref:Module#thread_mattr_accessor] and friends.
7
+ # Please use that approach instead.
8
+ #
4
9
  # This module is used to encapsulate access to thread local variables.
5
10
  #
6
11
  # Instead of polluting the thread locals namespace:
@@ -35,15 +40,15 @@ module ActiveSupport
35
40
  # If the class has an initializer, it must accept no arguments.
36
41
  module PerThreadRegistry
37
42
  def self.extended(object)
38
- object.instance_variable_set '@per_thread_registry_key', object.name.freeze
43
+ object.instance_variable_set "@per_thread_registry_key", object.name.freeze
39
44
  end
40
45
 
41
46
  def instance
42
47
  Thread.current[@per_thread_registry_key] ||= new
43
48
  end
44
49
 
45
- protected
46
- def method_missing(name, *args, &block) # :nodoc:
50
+ private
51
+ def method_missing(name, *args, &block)
47
52
  # Caches the method definition as a singleton method of the receiver.
48
53
  #
49
54
  # By letting #delegate handle it, we avoid an enclosure that'll capture args.
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveSupport
2
4
  # A class with no predefined methods that behaves similarly to Builder's
3
5
  # BlankSlate. Used for proxy classes.
@@ -1,27 +1,35 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # This is private interface.
2
4
  #
3
5
  # Rails components cherry pick from Active Support as needed, but there are a
4
- # few features that are used for sure some way or another and it is not worth
5
- # to put individual requires absolutely everywhere. Think blank? for example.
6
+ # few features that are used for sure in some way or another and it is not worth
7
+ # putting individual requires absolutely everywhere. Think blank? for example.
6
8
  #
7
9
  # This file is loaded by every Rails component except Active Support itself,
8
10
  # but it does not belong to the Rails public interface. It is internal to
9
11
  # Rails and can change anytime.
10
12
 
11
13
  # Defines Object#blank? and Object#present?.
12
- require 'active_support/core_ext/object/blank'
14
+ require "active_support/core_ext/object/blank"
13
15
 
14
16
  # Rails own autoload, eager_load, etc.
15
- require 'active_support/dependencies/autoload'
17
+ require "active_support/dependencies/autoload"
16
18
 
17
19
  # Support for ClassMethods and the included macro.
18
- require 'active_support/concern'
20
+ require "active_support/concern"
19
21
 
20
22
  # Defines Class#class_attribute.
21
- require 'active_support/core_ext/class/attribute'
23
+ require "active_support/core_ext/class/attribute"
22
24
 
23
25
  # Defines Module#delegate.
24
- require 'active_support/core_ext/module/delegation'
26
+ require "active_support/core_ext/module/delegation"
25
27
 
26
28
  # Defines ActiveSupport::Deprecation.
27
- require 'active_support/deprecation'
29
+ require "active_support/deprecation"
30
+
31
+ # Defines Regexp#match?.
32
+ #
33
+ # This should be removed when Rails needs Ruby 2.4 or later, and the require
34
+ # added where other Regexp extensions are being used (easy to grep).
35
+ require "active_support/core_ext/regexp"
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "active_support"
2
4
  require "active_support/i18n_railtie"
3
5
 
@@ -7,6 +9,21 @@ module ActiveSupport
7
9
 
8
10
  config.eager_load_namespaces << ActiveSupport
9
11
 
12
+ initializer "active_support.set_authenticated_message_encryption" do |app|
13
+ config.after_initialize do
14
+ unless app.config.active_support.use_authenticated_message_encryption.nil?
15
+ ActiveSupport::MessageEncryptor.use_authenticated_message_encryption =
16
+ app.config.active_support.use_authenticated_message_encryption
17
+ end
18
+ end
19
+ end
20
+
21
+ initializer "active_support.reset_all_current_attributes_instances" do |app|
22
+ app.reloader.before_class_unload { ActiveSupport::CurrentAttributes.clear_all }
23
+ app.executor.to_run { ActiveSupport::CurrentAttributes.reset_all }
24
+ app.executor.to_complete { ActiveSupport::CurrentAttributes.reset_all }
25
+ end
26
+
10
27
  initializer "active_support.deprecation_behavior" do |app|
11
28
  if deprecation = app.config.active_support.deprecation
12
29
  ActiveSupport::Deprecation.behavior = deprecation
@@ -16,31 +33,48 @@ module ActiveSupport
16
33
  # Sets the default value for Time.zone
17
34
  # If assigned value cannot be matched to a TimeZone, an exception will be raised.
18
35
  initializer "active_support.initialize_time_zone" do |app|
19
- require 'active_support/core_ext/time/zones'
20
- zone_default = Time.find_zone!(app.config.time_zone)
21
-
22
- unless zone_default
23
- raise 'Value assigned to config.time_zone not recognized. ' \
24
- 'Run "rake -D time" for a list of tasks for finding appropriate time zone names.'
36
+ begin
37
+ TZInfo::DataSource.get
38
+ rescue TZInfo::DataSourceNotFound => e
39
+ raise e.exception "tzinfo-data is not present. Please add gem 'tzinfo-data' to your Gemfile and run bundle install"
25
40
  end
26
-
27
- Time.zone_default = zone_default
41
+ require "active_support/core_ext/time/zones"
42
+ Time.zone_default = Time.find_zone!(app.config.time_zone)
28
43
  end
29
44
 
30
45
  # Sets the default week start
31
46
  # If assigned value is not a valid day symbol (e.g. :sunday, :monday, ...), an exception will be raised.
32
47
  initializer "active_support.initialize_beginning_of_week" do |app|
33
- require 'active_support/core_ext/date/calculations'
48
+ require "active_support/core_ext/date/calculations"
34
49
  beginning_of_week_default = Date.find_beginning_of_week!(app.config.beginning_of_week)
35
50
 
36
51
  Date.beginning_of_week_default = beginning_of_week_default
37
52
  end
38
53
 
54
+ initializer "active_support.require_master_key" do |app|
55
+ if app.config.respond_to?(:require_master_key) && app.config.require_master_key
56
+ begin
57
+ app.credentials.key
58
+ rescue ActiveSupport::EncryptedFile::MissingKeyError => error
59
+ $stderr.puts error.message
60
+ exit 1
61
+ end
62
+ end
63
+ end
64
+
39
65
  initializer "active_support.set_configs" do |app|
40
66
  app.config.active_support.each do |k, v|
41
67
  k = "#{k}="
42
68
  ActiveSupport.send(k, v) if ActiveSupport.respond_to? k
43
69
  end
44
70
  end
71
+
72
+ initializer "active_support.set_hash_digest_class" do |app|
73
+ config.after_initialize do
74
+ if app.config.active_support.use_sha1_digests
75
+ ActiveSupport::Digest.hash_digest_class = ::Digest::SHA1
76
+ end
77
+ end
78
+ end
45
79
  end
46
80
  end