diff options
author | Andrew White <andrew.white@unboxed.co> | 2017-02-27 18:31:35 +0000 |
---|---|---|
committer | Andrew White <andrew.white@unboxed.co> | 2017-03-02 08:00:22 +0000 |
commit | 75924c4517c8f87712d3f59c11f10152ed57b9d8 (patch) | |
tree | 5ea1e0dd022c66f96662b5745df591ae5714c6f7 /activesupport/lib/active_support | |
parent | a815c7c7214d0c45194bc9b097e20f50a2b0aab1 (diff) | |
download | rails-75924c4517c8f87712d3f59c11f10152ed57b9d8.tar.gz rails-75924c4517c8f87712d3f59c11f10152ed57b9d8.tar.bz2 rails-75924c4517c8f87712d3f59c11f10152ed57b9d8.zip |
Deprecate implicit coercion of `ActiveSupport::Duration`
Currently `ActiveSupport::Duration` implicitly converts to a seconds
value when used in a calculation except for the explicit examples of
addition and subtraction where the duration is the receiver, e.g:
>> 2 * 1.day
=> 172800
This results in lots of confusion especially when using durations
with dates because adding/subtracting a value from a date treats
integers as a day and not a second, e.g:
>> Date.today
=> Wed, 01 Mar 2017
>> Date.today + 2 * 1.day
=> Mon, 10 Apr 2490
To fix this we're implementing `coerce` so that we can provide a
deprecation warning with the intent of removing the implicit coercion
in Rails 5.2, e.g:
>> 2 * 1.day
DEPRECATION WARNING: Implicit coercion of ActiveSupport::Duration
to a Numeric is deprecated and will raise a TypeError in Rails 5.2.
=> 172800
In Rails 5.2 it will raise `TypeError`, e.g:
>> 2 * 1.day
TypeError: ActiveSupport::Duration can't be coerced into Integer
This is the same behavior as with other types in Ruby, e.g:
>> 2 * "foo"
TypeError: String can't be coerced into Integer
>> "foo" * 2
=> "foofoo"
As part of this deprecation add `*` and `/` methods to `AS::Duration`
so that calculations that keep the duration as the receiver work
correctly whether the final receiver is a `Date` or `Time`, e.g:
>> Date.today
=> Wed, 01 Mar 2017
>> Date.today + 1.day * 2
=> Fri, 03 Mar 2017
Fixes #27457.
Diffstat (limited to 'activesupport/lib/active_support')
-rw-r--r-- | activesupport/lib/active_support/cache/mem_cache_store.rb | 2 | ||||
-rw-r--r-- | activesupport/lib/active_support/duration.rb | 41 |
2 files changed, 40 insertions, 3 deletions
diff --git a/activesupport/lib/active_support/cache/mem_cache_store.rb b/activesupport/lib/active_support/cache/mem_cache_store.rb index e09cee3335..5eee04a34e 100644 --- a/activesupport/lib/active_support/cache/mem_cache_store.rb +++ b/activesupport/lib/active_support/cache/mem_cache_store.rb @@ -156,7 +156,7 @@ module ActiveSupport expires_in = options[:expires_in].to_i if expires_in > 0 && !options[:raw] # Set the memcache expire a few minutes in the future to support race condition ttls on read - expires_in += 5.minutes + expires_in += 300 end rescue_error_with false do @data.send(method, key, value, expires_in, options) diff --git a/activesupport/lib/active_support/duration.rb b/activesupport/lib/active_support/duration.rb index 70cf78519d..b92891c511 100644 --- a/activesupport/lib/active_support/duration.rb +++ b/activesupport/lib/active_support/duration.rb @@ -1,5 +1,7 @@ require "active_support/core_ext/array/conversions" require "active_support/core_ext/object/acts_like" +require "active_support/core_ext/string/filters" +require "active_support/deprecation" module ActiveSupport # Provides accurate date and time measurements using Date#advance and @@ -88,6 +90,25 @@ module ActiveSupport @parts.default = 0 end + def coerce(other) #:nodoc: + ActiveSupport::Deprecation.warn(<<-MSG.squish) + Implicit coercion of ActiveSupport::Duration to a Numeric + is deprecated and will raise a TypeError in Rails 5.2. + MSG + + [other, value] + end + + # Compares one Duration with another or a Numeric to this Duration. + # Numeric values are treated as seconds. + def <=>(other) + if Duration === other + value <=> other.value + elsif Numeric === other + value <=> other + end + end + # Adds another Duration or a Numeric to this Duration. Numeric values # are treated as seconds. def +(other) @@ -109,6 +130,24 @@ module ActiveSupport self + (-other) end + # Multiplies this Duration by a Numeric and returns a new Duration. + def *(other) + if Numeric === other + Duration.new(value * other, parts.map { |type, number| [type, number * other] }) + else + value * other + end + end + + # Devides this Duration by a Numeric and returns a new Duration. + def /(other) + if Numeric === other + Duration.new(value / other, parts.map { |type, number| [type, number / other] }) + else + value / other + end + end + def -@ #:nodoc: Duration.new(-value, parts.map { |type, number| [type, -number] }) end @@ -212,8 +251,6 @@ module ActiveSupport ISO8601Serializer.new(self, precision: precision).serialize end - delegate :<=>, to: :value - private def sum(sign, time = ::Time.current) |