activesupport 5.0.7.2 → 5.1.7

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 (211) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +464 -694
  3. data/MIT-LICENSE +1 -1
  4. data/README.rdoc +1 -1
  5. data/lib/active_support.rb +8 -4
  6. data/lib/active_support/all.rb +3 -3
  7. data/lib/active_support/array_inquirer.rb +7 -5
  8. data/lib/active_support/backtrace_cleaner.rb +4 -4
  9. data/lib/active_support/benchmarkable.rb +3 -3
  10. data/lib/active_support/builder.rb +1 -1
  11. data/lib/active_support/cache.rb +41 -48
  12. data/lib/active_support/cache/file_store.rb +11 -20
  13. data/lib/active_support/cache/mem_cache_store.rb +30 -40
  14. data/lib/active_support/cache/memory_store.rb +13 -13
  15. data/lib/active_support/cache/null_store.rb +4 -4
  16. data/lib/active_support/cache/strategy/local_cache.rb +13 -22
  17. data/lib/active_support/cache/strategy/local_cache_middleware.rb +4 -5
  18. data/lib/active_support/callbacks.rb +649 -584
  19. data/lib/active_support/concurrency/load_interlock_aware_monitor.rb +17 -0
  20. data/lib/active_support/concurrency/share_lock.rb +20 -21
  21. data/lib/active_support/configurable.rb +5 -5
  22. data/lib/active_support/core_ext.rb +1 -2
  23. data/lib/active_support/core_ext/array.rb +7 -7
  24. data/lib/active_support/core_ext/array/access.rb +1 -1
  25. data/lib/active_support/core_ext/array/conversions.rb +15 -15
  26. data/lib/active_support/core_ext/array/grouping.rb +1 -1
  27. data/lib/active_support/core_ext/array/inquiry.rb +1 -1
  28. data/lib/active_support/core_ext/array/prepend_and_append.rb +1 -1
  29. data/lib/active_support/core_ext/benchmark.rb +1 -1
  30. data/lib/active_support/core_ext/big_decimal.rb +1 -1
  31. data/lib/active_support/core_ext/big_decimal/conversions.rb +4 -6
  32. data/lib/active_support/core_ext/class.rb +2 -2
  33. data/lib/active_support/core_ext/class/attribute.rb +5 -5
  34. data/lib/active_support/core_ext/class/attribute_accessors.rb +1 -1
  35. data/lib/active_support/core_ext/class/subclasses.rb +18 -4
  36. data/lib/active_support/core_ext/date.rb +5 -5
  37. data/lib/active_support/core_ext/date/acts_like.rb +1 -1
  38. data/lib/active_support/core_ext/date/blank.rb +1 -1
  39. data/lib/active_support/core_ext/date/calculations.rb +8 -8
  40. data/lib/active_support/core_ext/date/conversions.rb +12 -12
  41. data/lib/active_support/core_ext/date/zones.rb +2 -2
  42. data/lib/active_support/core_ext/date_and_time/calculations.rb +27 -22
  43. data/lib/active_support/core_ext/date_and_time/compatibility.rb +1 -1
  44. data/lib/active_support/core_ext/date_and_time/zones.rb +7 -8
  45. data/lib/active_support/core_ext/date_time.rb +5 -5
  46. data/lib/active_support/core_ext/date_time/acts_like.rb +2 -2
  47. data/lib/active_support/core_ext/date_time/blank.rb +1 -1
  48. data/lib/active_support/core_ext/date_time/calculations.rb +20 -10
  49. data/lib/active_support/core_ext/date_time/compatibility.rb +2 -2
  50. data/lib/active_support/core_ext/date_time/conversions.rb +12 -12
  51. data/lib/active_support/core_ext/digest/uuid.rb +4 -4
  52. data/lib/active_support/core_ext/enumerable.rb +23 -12
  53. data/lib/active_support/core_ext/file.rb +1 -1
  54. data/lib/active_support/core_ext/file/atomic.rb +4 -4
  55. data/lib/active_support/core_ext/hash.rb +9 -9
  56. data/lib/active_support/core_ext/hash/compact.rb +12 -9
  57. data/lib/active_support/core_ext/hash/conversions.rb +36 -37
  58. data/lib/active_support/core_ext/hash/indifferent_access.rb +1 -2
  59. data/lib/active_support/core_ext/hash/keys.rb +6 -6
  60. data/lib/active_support/core_ext/hash/reverse_merge.rb +1 -1
  61. data/lib/active_support/core_ext/hash/slice.rb +4 -4
  62. data/lib/active_support/core_ext/hash/transform_values.rb +1 -0
  63. data/lib/active_support/core_ext/integer.rb +3 -3
  64. data/lib/active_support/core_ext/integer/inflections.rb +1 -1
  65. data/lib/active_support/core_ext/integer/time.rb +2 -2
  66. data/lib/active_support/core_ext/kernel.rb +4 -4
  67. data/lib/active_support/core_ext/kernel/concern.rb +1 -1
  68. data/lib/active_support/core_ext/kernel/reporting.rb +1 -1
  69. data/lib/active_support/core_ext/load_error.rb +1 -18
  70. data/lib/active_support/core_ext/module.rb +11 -12
  71. data/lib/active_support/core_ext/module/aliasing.rb +3 -48
  72. data/lib/active_support/core_ext/module/attr_internal.rb +4 -4
  73. data/lib/active_support/core_ext/module/attribute_accessors.rb +11 -5
  74. data/lib/active_support/core_ext/module/attribute_accessors_per_thread.rb +20 -13
  75. data/lib/active_support/core_ext/module/concerning.rb +1 -1
  76. data/lib/active_support/core_ext/module/delegation.rb +85 -16
  77. data/lib/active_support/core_ext/module/introspection.rb +3 -11
  78. data/lib/active_support/core_ext/module/reachable.rb +2 -2
  79. data/lib/active_support/core_ext/numeric.rb +4 -4
  80. data/lib/active_support/core_ext/numeric/conversions.rb +3 -9
  81. data/lib/active_support/core_ext/numeric/inquiry.rb +21 -21
  82. data/lib/active_support/core_ext/numeric/time.rb +5 -5
  83. data/lib/active_support/core_ext/object.rb +12 -12
  84. data/lib/active_support/core_ext/object/blank.rb +3 -1
  85. data/lib/active_support/core_ext/object/conversions.rb +4 -4
  86. data/lib/active_support/core_ext/object/deep_dup.rb +1 -1
  87. data/lib/active_support/core_ext/object/duplicable.rb +34 -4
  88. data/lib/active_support/core_ext/object/inclusion.rb +1 -1
  89. data/lib/active_support/core_ext/object/json.rb +26 -12
  90. data/lib/active_support/core_ext/object/to_param.rb +1 -1
  91. data/lib/active_support/core_ext/object/to_query.rb +8 -5
  92. data/lib/active_support/core_ext/object/try.rb +1 -1
  93. data/lib/active_support/core_ext/object/with_options.rb +12 -1
  94. data/lib/active_support/core_ext/range.rb +4 -4
  95. data/lib/active_support/core_ext/range/conversions.rb +1 -1
  96. data/lib/active_support/core_ext/regexp.rb +4 -0
  97. data/lib/active_support/core_ext/securerandom.rb +3 -3
  98. data/lib/active_support/core_ext/string.rb +13 -13
  99. data/lib/active_support/core_ext/string/access.rb +6 -6
  100. data/lib/active_support/core_ext/string/conversions.rb +2 -2
  101. data/lib/active_support/core_ext/string/filters.rb +3 -3
  102. data/lib/active_support/core_ext/string/indent.rb +4 -4
  103. data/lib/active_support/core_ext/string/inflections.rb +10 -14
  104. data/lib/active_support/core_ext/string/inquiry.rb +1 -1
  105. data/lib/active_support/core_ext/string/multibyte.rb +1 -1
  106. data/lib/active_support/core_ext/string/output_safety.rb +19 -20
  107. data/lib/active_support/core_ext/string/strip.rb +1 -1
  108. data/lib/active_support/core_ext/string/zones.rb +2 -2
  109. data/lib/active_support/core_ext/time.rb +5 -5
  110. data/lib/active_support/core_ext/time/acts_like.rb +1 -1
  111. data/lib/active_support/core_ext/time/calculations.rb +46 -29
  112. data/lib/active_support/core_ext/time/conversions.rb +15 -12
  113. data/lib/active_support/core_ext/time/zones.rb +3 -3
  114. data/lib/active_support/core_ext/uri.rb +2 -2
  115. data/lib/active_support/dependencies.rb +45 -46
  116. data/lib/active_support/dependencies/interlock.rb +1 -1
  117. data/lib/active_support/deprecation.rb +9 -8
  118. data/lib/active_support/deprecation/behaviors.rb +3 -3
  119. data/lib/active_support/deprecation/constant_accessor.rb +50 -0
  120. data/lib/active_support/deprecation/instance_delegator.rb +2 -2
  121. data/lib/active_support/deprecation/method_wrappers.rb +10 -3
  122. data/lib/active_support/deprecation/proxy_wrappers.rb +6 -4
  123. data/lib/active_support/deprecation/reporting.rb +7 -7
  124. data/lib/active_support/duration.rb +221 -28
  125. data/lib/active_support/duration/iso8601_parser.rb +66 -65
  126. data/lib/active_support/duration/iso8601_serializer.rb +11 -9
  127. data/lib/active_support/evented_file_update_checker.rb +59 -55
  128. data/lib/active_support/execution_wrapper.rb +3 -3
  129. data/lib/active_support/executor.rb +1 -1
  130. data/lib/active_support/file_update_checker.rb +54 -50
  131. data/lib/active_support/gem_version.rb +2 -2
  132. data/lib/active_support/gzip.rb +4 -4
  133. data/lib/active_support/hash_with_indifferent_access.rb +40 -28
  134. data/lib/active_support/i18n.rb +5 -5
  135. data/lib/active_support/i18n_railtie.rb +14 -9
  136. data/lib/active_support/inflections.rb +11 -11
  137. data/lib/active_support/inflector.rb +5 -5
  138. data/lib/active_support/inflector/inflections.rb +11 -9
  139. data/lib/active_support/inflector/methods.rb +52 -51
  140. data/lib/active_support/inflector/transliterate.rb +8 -11
  141. data/lib/active_support/json.rb +2 -2
  142. data/lib/active_support/json/decoding.rb +3 -3
  143. data/lib/active_support/json/encoding.rb +8 -7
  144. data/lib/active_support/key_generator.rb +17 -17
  145. data/lib/active_support/lazy_load_hooks.rb +2 -2
  146. data/lib/active_support/log_subscriber.rb +9 -7
  147. data/lib/active_support/log_subscriber/test_helper.rb +9 -9
  148. data/lib/active_support/logger.rb +3 -3
  149. data/lib/active_support/logger_silence.rb +3 -3
  150. data/lib/active_support/logger_thread_safe_level.rb +1 -1
  151. data/lib/active_support/message_encryptor.rb +77 -35
  152. data/lib/active_support/message_verifier.rb +7 -7
  153. data/lib/active_support/multibyte.rb +2 -2
  154. data/lib/active_support/multibyte/chars.rb +23 -21
  155. data/lib/active_support/multibyte/unicode.rb +68 -89
  156. data/lib/active_support/notifications.rb +7 -5
  157. data/lib/active_support/notifications/fanout.rb +3 -3
  158. data/lib/active_support/notifications/instrumenter.rb +5 -5
  159. data/lib/active_support/number_helper.rb +5 -4
  160. data/lib/active_support/number_helper/number_converter.rb +11 -11
  161. data/lib/active_support/number_helper/number_to_currency_converter.rb +3 -3
  162. data/lib/active_support/number_helper/number_to_delimited_converter.rb +1 -2
  163. data/lib/active_support/number_helper/number_to_human_converter.rb +8 -10
  164. data/lib/active_support/number_helper/number_to_human_size_converter.rb +6 -11
  165. data/lib/active_support/number_helper/number_to_percentage_converter.rb +1 -1
  166. data/lib/active_support/number_helper/number_to_phone_converter.rb +1 -3
  167. data/lib/active_support/number_helper/number_to_rounded_converter.rb +12 -32
  168. data/lib/active_support/number_helper/rounding_helper.rb +64 -0
  169. data/lib/active_support/option_merger.rb +1 -1
  170. data/lib/active_support/ordered_hash.rb +3 -3
  171. data/lib/active_support/ordered_options.rb +6 -4
  172. data/lib/active_support/per_thread_registry.rb +5 -5
  173. data/lib/active_support/rails.rb +12 -6
  174. data/lib/active_support/railtie.rb +3 -3
  175. data/lib/active_support/reloader.rb +1 -1
  176. data/lib/active_support/rescuable.rb +6 -6
  177. data/lib/active_support/security_utils.rb +1 -1
  178. data/lib/active_support/string_inquirer.rb +8 -2
  179. data/lib/active_support/subscriber.rb +9 -5
  180. data/lib/active_support/tagged_logging.rb +4 -4
  181. data/lib/active_support/test_case.rb +12 -29
  182. data/lib/active_support/testing/assertions.rb +100 -2
  183. data/lib/active_support/testing/autorun.rb +2 -2
  184. data/lib/active_support/testing/constant_lookup.rb +0 -1
  185. data/lib/active_support/testing/declarative.rb +1 -1
  186. data/lib/active_support/testing/deprecation.rb +3 -2
  187. data/lib/active_support/testing/isolation.rb +15 -22
  188. data/lib/active_support/testing/method_call_assertions.rb +1 -1
  189. data/lib/active_support/testing/setup_and_teardown.rb +2 -2
  190. data/lib/active_support/testing/stream.rb +28 -28
  191. data/lib/active_support/testing/tagged_logging.rb +1 -1
  192. data/lib/active_support/testing/time_helpers.rb +45 -11
  193. data/lib/active_support/time.rb +12 -12
  194. data/lib/active_support/time_with_zone.rb +16 -14
  195. data/lib/active_support/values/time_zone.rb +100 -31
  196. data/lib/active_support/values/unicode_tables.dat +0 -0
  197. data/lib/active_support/version.rb +1 -1
  198. data/lib/active_support/xml_mini.rb +34 -36
  199. data/lib/active_support/xml_mini/jdom.rb +112 -112
  200. data/lib/active_support/xml_mini/libxml.rb +12 -11
  201. data/lib/active_support/xml_mini/libxmlsax.rb +13 -14
  202. data/lib/active_support/xml_mini/nokogiri.rb +10 -10
  203. data/lib/active_support/xml_mini/nokogirisax.rb +12 -13
  204. data/lib/active_support/xml_mini/rexml.rb +9 -9
  205. metadata +8 -9
  206. data/lib/active_support/concurrency/latch.rb +0 -26
  207. data/lib/active_support/core_ext/kernel/debugger.rb +0 -3
  208. data/lib/active_support/core_ext/module/method_transplanting.rb +0 -3
  209. data/lib/active_support/core_ext/module/qualified_const.rb +0 -70
  210. data/lib/active_support/core_ext/struct.rb +0 -3
  211. data/lib/active_support/core_ext/time/marshal.rb +0 -3
@@ -1,5 +1,5 @@
1
- require 'mutex_m'
2
- require 'concurrent/map'
1
+ require "mutex_m"
2
+ require "concurrent/map"
3
3
 
4
4
  module ActiveSupport
5
5
  module Notifications
@@ -68,7 +68,7 @@ module ActiveSupport
68
68
 
69
69
  module Subscribers # :nodoc:
70
70
  def self.new(pattern, listener)
71
- if listener.respond_to?(:start) and listener.respond_to?(:finish)
71
+ if listener.respond_to?(:start) && listener.respond_to?(:finish)
72
72
  subscriber = Evented.new pattern, listener
73
73
  else
74
74
  subscriber = Timed.new pattern, listener
@@ -1,4 +1,4 @@
1
- require 'securerandom'
1
+ require "securerandom"
2
2
 
3
3
  module ActiveSupport
4
4
  module Notifications
@@ -14,7 +14,7 @@ module ActiveSupport
14
14
  # Instrument the given block by measuring the time taken to execute it
15
15
  # and publish it. Notice that events get sent even if an error occurs
16
16
  # in the passed-in block.
17
- def instrument(name, payload={})
17
+ def instrument(name, payload = {})
18
18
  # some of the listeners might have state
19
19
  listeners_state = start name, payload
20
20
  begin
@@ -44,9 +44,9 @@ module ActiveSupport
44
44
 
45
45
  private
46
46
 
47
- def unique_id
48
- SecureRandom.hex(10)
49
- end
47
+ def unique_id
48
+ SecureRandom.hex(10)
49
+ end
50
50
  end
51
51
 
52
52
  class Event
@@ -4,6 +4,7 @@ module ActiveSupport
4
4
 
5
5
  eager_autoload do
6
6
  autoload :NumberConverter
7
+ autoload :RoundingHelper
7
8
  autoload :NumberToRoundedConverter
8
9
  autoload :NumberToDelimitedConverter
9
10
  autoload :NumberToHumanConverter
@@ -45,7 +46,7 @@ module ActiveSupport
45
46
  #
46
47
  # number_to_phone(75561234567, pattern: /(\d{1,4})(\d{4})(\d{4})$/, area_code: true)
47
48
  # # => "(755) 6123-4567"
48
- # number_to_phone(13312345678, pattern: /(\d{3})(\d{4})(\d{4})$/))
49
+ # number_to_phone(13312345678, pattern: /(\d{3})(\d{4})(\d{4})$/)
49
50
  # # => "133-1234-5678"
50
51
  def number_to_phone(number, options = {})
51
52
  NumberToPhoneConverter.convert(number, options)
@@ -78,7 +79,7 @@ module ActiveSupport
78
79
  # (defaults to "%u%n"). Fields are <tt>%u</tt> for the
79
80
  # currency, and <tt>%n</tt> for the number.
80
81
  # * <tt>:negative_format</tt> - Sets the format for negative
81
- # numbers (defaults to prepending an hyphen to the formatted
82
+ # numbers (defaults to prepending a hyphen to the formatted
82
83
  # number given by <tt>:format</tt>). Accepts the same fields
83
84
  # than <tt>:format</tt>, except <tt>%n</tt> is here the
84
85
  # absolute value of the number.
@@ -109,7 +110,7 @@ module ActiveSupport
109
110
  # * <tt>:locale</tt> - Sets the locale to be used for formatting
110
111
  # (defaults to current locale).
111
112
  # * <tt>:precision</tt> - Sets the precision of the number
112
- # (defaults to 3). Keeps the number's precision if nil.
113
+ # (defaults to 3). Keeps the number's precision if +nil+.
113
114
  # * <tt>:significant</tt> - If +true+, precision will be the number
114
115
  # of significant_digits. If +false+, the number of fractional
115
116
  # digits (defaults to +false+).
@@ -183,7 +184,7 @@ module ActiveSupport
183
184
  # * <tt>:locale</tt> - Sets the locale to be used for formatting
184
185
  # (defaults to current locale).
185
186
  # * <tt>:precision</tt> - Sets the precision of the number
186
- # (defaults to 3). Keeps the number's precision if nil.
187
+ # (defaults to 3). Keeps the number's precision if +nil+.
187
188
  # * <tt>:significant</tt> - If +true+, precision will be the number
188
189
  # of significant_digits. If +false+, the number of fractional
189
190
  # digits (defaults to +false+).
@@ -1,8 +1,8 @@
1
- require 'active_support/core_ext/big_decimal/conversions'
2
- require 'active_support/core_ext/object/blank'
3
- require 'active_support/core_ext/hash/keys'
4
- require 'active_support/i18n'
5
- require 'active_support/core_ext/class/attribute'
1
+ require "active_support/core_ext/big_decimal/conversions"
2
+ require "active_support/core_ext/object/blank"
3
+ require "active_support/core_ext/hash/keys"
4
+ require "active_support/i18n"
5
+ require "active_support/core_ext/class/attribute"
6
6
 
7
7
  module ActiveSupport
8
8
  module NumberHelper
@@ -139,17 +139,17 @@ module ActiveSupport
139
139
  @options ||= format_options.merge(opts)
140
140
  end
141
141
 
142
- def format_options #:nodoc:
142
+ def format_options
143
143
  default_format_options.merge!(i18n_format_options)
144
144
  end
145
145
 
146
- def default_format_options #:nodoc:
146
+ def default_format_options
147
147
  options = DEFAULTS[:format].dup
148
148
  options.merge!(DEFAULTS[namespace][:format]) if namespace
149
149
  options
150
150
  end
151
151
 
152
- def i18n_format_options #:nodoc:
152
+ def i18n_format_options
153
153
  locale = opts[:locale]
154
154
  options = I18n.translate(:'number.format', locale: locale, default: {}).dup
155
155
 
@@ -160,7 +160,7 @@ module ActiveSupport
160
160
  options
161
161
  end
162
162
 
163
- def translate_number_value_with_default(key, i18n_options = {}) #:nodoc:
163
+ def translate_number_value_with_default(key, i18n_options = {})
164
164
  I18n.translate(key, { default: default_value(key), scope: :number }.merge!(i18n_options))
165
165
  end
166
166
 
@@ -169,10 +169,10 @@ module ActiveSupport
169
169
  end
170
170
 
171
171
  def default_value(key)
172
- key.split('.').reduce(DEFAULTS) { |defaults, k| defaults[k.to_sym] }
172
+ key.split(".").reduce(DEFAULTS) { |defaults, k| defaults[k.to_sym] }
173
173
  end
174
174
 
175
- def valid_float? #:nodoc:
175
+ def valid_float?
176
176
  Float(number)
177
177
  rescue ArgumentError, TypeError
178
178
  false
@@ -1,4 +1,4 @@
1
- require 'active_support/core_ext/numeric/inquiry'
1
+ require "active_support/core_ext/numeric/inquiry"
2
2
 
3
3
  module ActiveSupport
4
4
  module NumberHelper
@@ -15,13 +15,13 @@ module ActiveSupport
15
15
  end
16
16
 
17
17
  rounded_number = NumberToRoundedConverter.convert(number, options)
18
- format.gsub('%n'.freeze, rounded_number).gsub('%u'.freeze, options[:unit])
18
+ format.gsub("%n".freeze, rounded_number).gsub("%u".freeze, options[:unit])
19
19
  end
20
20
 
21
21
  private
22
22
 
23
23
  def absolute_value(number)
24
- number.respond_to?(:abs) ? number.abs : number.sub(/\A-/, '')
24
+ number.respond_to?(:abs) ? number.abs : number.sub(/\A-/, "")
25
25
  end
26
26
 
27
27
  def options
@@ -12,7 +12,7 @@ module ActiveSupport
12
12
  private
13
13
 
14
14
  def parts
15
- left, right = number.to_s.split('.'.freeze)
15
+ left, right = number.to_s.split(".".freeze)
16
16
  left.gsub!(delimiter_pattern) do |digit_to_delimit|
17
17
  "#{digit_to_delimit}#{options[:delimiter]}"
18
18
  end
@@ -22,7 +22,6 @@ module ActiveSupport
22
22
  def delimiter_pattern
23
23
  options.fetch(:delimiter_pattern, DEFAULT_DELIMITER_REGEX)
24
24
  end
25
-
26
25
  end
27
26
  end
28
27
  end
@@ -9,6 +9,7 @@ module ActiveSupport
9
9
  self.validate_float = true
10
10
 
11
11
  def convert # :nodoc:
12
+ @number = RoundingHelper.new(options).round(number)
12
13
  @number = Float(number)
13
14
 
14
15
  # for backwards compatibility with those that didn't add strip_insignificant_zeros to their locale files
@@ -18,29 +19,26 @@ module ActiveSupport
18
19
 
19
20
  units = opts[:units]
20
21
  exponent = calculate_exponent(units)
21
- @number = number / (10 ** exponent)
22
+ @number = number / (10**exponent)
22
23
 
23
- until (rounded_number = NumberToRoundedConverter.convert(number, options)) != NumberToRoundedConverter.convert(1000, options)
24
- @number = number / 1000.0
25
- exponent += 3
26
- end
24
+ rounded_number = NumberToRoundedConverter.convert(number, options)
27
25
  unit = determine_unit(units, exponent)
28
- format.gsub('%n'.freeze, rounded_number).gsub('%u'.freeze, unit).strip
26
+ format.gsub("%n".freeze, rounded_number).gsub("%u".freeze, unit).strip
29
27
  end
30
28
 
31
29
  private
32
30
 
33
31
  def format
34
- options[:format] || translate_in_locale('human.decimal_units.format')
32
+ options[:format] || translate_in_locale("human.decimal_units.format")
35
33
  end
36
34
 
37
35
  def determine_unit(units, exponent)
38
36
  exp = DECIMAL_UNITS[exponent]
39
37
  case units
40
38
  when Hash
41
- units[exp] || ''
39
+ units[exp] || ""
42
40
  when String, Symbol
43
- I18n.translate("#{units}.#{exp}", :locale => options[:locale], :count => number.to_i)
41
+ I18n.translate("#{units}.#{exp}", locale: options[:locale], count: number.to_i)
44
42
  else
45
43
  translate_in_locale("human.decimal_units.units.#{exp}", count: number.to_i)
46
44
  end
@@ -56,7 +54,7 @@ module ActiveSupport
56
54
  when Hash
57
55
  units
58
56
  when String, Symbol
59
- I18n.translate(units.to_s, :locale => options[:locale], :raise => true)
57
+ I18n.translate(units.to_s, locale: options[:locale], raise: true)
60
58
  when nil
61
59
  translate_in_locale("human.decimal_units.units", raise: true)
62
60
  else
@@ -7,10 +7,6 @@ module ActiveSupport
7
7
  self.validate_float = true
8
8
 
9
9
  def convert
10
- if opts.key?(:prefix)
11
- ActiveSupport::Deprecation.warn('The :prefix option of `number_to_human_size` is deprecated and will be removed in Rails 5.1 with no replacement.')
12
- end
13
-
14
10
  @number = Float(number)
15
11
 
16
12
  # for backwards compatibility with those that didn't add strip_insignificant_zeros to their locale files
@@ -21,24 +17,24 @@ module ActiveSupport
21
17
  if smaller_than_base?
22
18
  number_to_format = number.to_i.to_s
23
19
  else
24
- human_size = number / (base ** exponent)
20
+ human_size = number / (base**exponent)
25
21
  number_to_format = NumberToRoundedConverter.convert(human_size, options)
26
22
  end
27
- conversion_format.gsub('%n'.freeze, number_to_format).gsub('%u'.freeze, unit)
23
+ conversion_format.gsub("%n".freeze, number_to_format).gsub("%u".freeze, unit)
28
24
  end
29
25
 
30
26
  private
31
27
 
32
28
  def conversion_format
33
- translate_number_value_with_default('human.storage_units.format', :locale => options[:locale], :raise => true)
29
+ translate_number_value_with_default("human.storage_units.format", locale: options[:locale], raise: true)
34
30
  end
35
31
 
36
32
  def unit
37
- translate_number_value_with_default(storage_unit_key, :locale => options[:locale], :count => number.to_i, :raise => true)
33
+ translate_number_value_with_default(storage_unit_key, locale: options[:locale], count: number.to_i, raise: true)
38
34
  end
39
35
 
40
36
  def storage_unit_key
41
- key_end = smaller_than_base? ? 'byte' : STORAGE_UNITS[exponent]
37
+ key_end = smaller_than_base? ? "byte" : STORAGE_UNITS[exponent]
42
38
  "human.storage_units.units.#{key_end}"
43
39
  end
44
40
 
@@ -54,9 +50,8 @@ module ActiveSupport
54
50
  end
55
51
 
56
52
  def base
57
- opts[:prefix] == :si ? 1000 : 1024
53
+ 1024
58
54
  end
59
55
  end
60
56
  end
61
57
  end
62
-
@@ -5,7 +5,7 @@ module ActiveSupport
5
5
 
6
6
  def convert
7
7
  rounded_number = NumberToRoundedConverter.convert(number, options)
8
- options[:format].gsub('%n'.freeze, rounded_number)
8
+ options[:format].gsub("%n".freeze, rounded_number)
9
9
  end
10
10
  end
11
11
  end
@@ -2,7 +2,7 @@ module ActiveSupport
2
2
  module NumberHelper
3
3
  class NumberToPhoneConverter < NumberConverter #:nodoc:
4
4
  def convert
5
- str = country_code(opts[:country_code])
5
+ str = country_code(opts[:country_code])
6
6
  str << convert_to_phone_number(number.to_s.strip)
7
7
  str << phone_ext(opts[:extension])
8
8
  end
@@ -51,8 +51,6 @@ module ActiveSupport
51
51
  def regexp_pattern(default_pattern)
52
52
  opts.fetch :pattern, default_pattern
53
53
  end
54
-
55
54
  end
56
55
  end
57
56
  end
58
-
@@ -5,40 +5,28 @@ module ActiveSupport
5
5
  self.validate_float = true
6
6
 
7
7
  def convert
8
- precision = options.delete :precision
8
+ helper = RoundingHelper.new(options)
9
+ rounded_number = helper.round(number)
9
10
 
10
- if precision
11
- case number
12
- when Float, String
13
- @number = BigDecimal(number.to_s)
14
- when Rational
15
- @number = BigDecimal(number, digit_count(number.to_i) + precision)
16
- else
17
- @number = number.to_d
18
- end
19
-
20
- if options.delete(:significant) && precision > 0
21
- digits, rounded_number = digits_and_rounded_number(precision)
11
+ if precision = options[:precision]
12
+ if options[:significant] && precision > 0
13
+ digits = helper.digit_count(rounded_number)
22
14
  precision -= digits
23
15
  precision = 0 if precision < 0 # don't let it be negative
24
- else
25
- rounded_number = number.round(precision)
26
- rounded_number = rounded_number.to_i if precision == 0 && rounded_number.finite?
27
- rounded_number = rounded_number.abs if rounded_number.zero? # prevent showing negative zeros
28
16
  end
29
17
 
30
18
  formatted_string =
31
19
  if BigDecimal === rounded_number && rounded_number.finite?
32
- s = rounded_number.to_s('F')
33
- s << '0'.freeze * precision
34
- a, b = s.split('.'.freeze, 2)
35
- a << '.'.freeze
20
+ s = rounded_number.to_s("F")
21
+ s << "0".freeze * precision
22
+ a, b = s.split(".".freeze, 2)
23
+ a << ".".freeze
36
24
  a << b[0, precision]
37
25
  else
38
26
  "%00.#{precision}f" % rounded_number
39
27
  end
40
28
  else
41
- formatted_string = number
29
+ formatted_string = rounded_number
42
30
  end
43
31
 
44
32
  delimited_number = NumberToDelimitedConverter.convert(formatted_string, options)
@@ -52,7 +40,7 @@ module ActiveSupport
52
40
  [1, 0]
53
41
  else
54
42
  digits = digit_count(number)
55
- multiplier = 10 ** (digits - precision)
43
+ multiplier = 10**(digits - precision)
56
44
  rounded_number = calculate_rounded_number(multiplier)
57
45
  digits = digit_count(rounded_number) # After rounding, the number of digits may have changed
58
46
  [digits, rounded_number]
@@ -74,19 +62,11 @@ module ActiveSupport
74
62
  def format_number(number)
75
63
  if strip_insignificant_zeros
76
64
  escaped_separator = Regexp.escape(options[:separator])
77
- number.sub(/(#{escaped_separator})(\d*[1-9])?0+\z/, '\1\2').sub(/#{escaped_separator}\z/, '')
65
+ number.sub(/(#{escaped_separator})(\d*[1-9])?0+\z/, '\1\2').sub(/#{escaped_separator}\z/, "")
78
66
  else
79
67
  number
80
68
  end
81
69
  end
82
-
83
- def absolute_number(number)
84
- number.respond_to?(:abs) ? number.abs : number.to_d.abs
85
- end
86
-
87
- def zero?
88
- number.respond_to?(:zero?) ? number.zero? : number.to_d.zero?
89
- end
90
70
  end
91
71
  end
92
72
  end
@@ -0,0 +1,64 @@
1
+ module ActiveSupport
2
+ module NumberHelper
3
+ class RoundingHelper # :nodoc:
4
+ attr_reader :options
5
+
6
+ def initialize(options)
7
+ @options = options
8
+ end
9
+
10
+ def round(number)
11
+ return number unless precision
12
+ number = convert_to_decimal(number)
13
+ if significant && precision > 0
14
+ round_significant(number)
15
+ else
16
+ round_without_significant(number)
17
+ end
18
+ end
19
+
20
+ def digit_count(number)
21
+ return 1 if number.zero?
22
+ (Math.log10(absolute_number(number)) + 1).floor
23
+ end
24
+
25
+ private
26
+ def round_without_significant(number)
27
+ number = number.round(precision)
28
+ number = number.to_i if precision == 0 && number.finite?
29
+ number = number.abs if number.zero? # prevent showing negative zeros
30
+ number
31
+ end
32
+
33
+ def round_significant(number)
34
+ return 0 if number.zero?
35
+ digits = digit_count(number)
36
+ multiplier = 10**(digits - precision)
37
+ (number / BigDecimal.new(multiplier.to_f.to_s)).round * multiplier
38
+ end
39
+
40
+ def convert_to_decimal(number)
41
+ case number
42
+ when Float, String
43
+ number = BigDecimal(number.to_s)
44
+ when Rational
45
+ number = BigDecimal(number, digit_count(number.to_i) + precision)
46
+ else
47
+ number = number.to_d
48
+ end
49
+ end
50
+
51
+ def precision
52
+ options[:precision]
53
+ end
54
+
55
+ def significant
56
+ options[:significant]
57
+ end
58
+
59
+ def absolute_number(number)
60
+ number.respond_to?(:abs) ? number.abs : number.to_d.abs
61
+ end
62
+ end
63
+ end
64
+ end