1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Rename prefix to in_* and update CHANGELOG

This commit is contained in:
Jason York 2020-08-26 09:31:31 -05:00
parent 7bd9603778
commit a2535d9fe9
3 changed files with 48 additions and 37 deletions

View file

@ -1,3 +1,9 @@
* Add `ActiveSupport::Duration` conversion methods
`in_seconds`, `in_minutes`, `in_hours`, `in_days`, `in_weeks`, `in_months`, and `in_years` return the respective duration covered.
*Jason York*
* Fixed issue in `ActiveSupport::Cache::RedisCacheStore` not passing options * Fixed issue in `ActiveSupport::Cache::RedisCacheStore` not passing options
to `read_multi` causing `fetch_multi` to not work properly to `read_multi` causing `fetch_multi` to not work properly

View file

@ -349,48 +349,48 @@ module ActiveSupport
def to_i def to_i
@value.to_i @value.to_i
end end
alias :to_seconds :to_i alias :in_seconds :to_i
# Returns the amount of minutes a duration covers as a float # Returns the amount of minutes a duration covers as a float
# #
# 1.day.to_minutes # => 1440.0 # 1.day.in_minutes # => 1440.0
def to_minutes def in_minutes
to_seconds / SECONDS_PER_MINUTE.to_f in_seconds / SECONDS_PER_MINUTE.to_f
end end
# Returns the amount of hours a duration covers as a float # Returns the amount of hours a duration covers as a float
# #
# 1.day.to_hours # => 24.0 # 1.day.in_hours # => 24.0
def to_hours def in_hours
to_seconds / SECONDS_PER_HOUR.to_f in_seconds / SECONDS_PER_HOUR.to_f
end end
# Returns the amount of days a duration covers as a float # Returns the amount of days a duration covers as a float
# #
# 12.hours.to_days # => 0.5 # 12.hours.in_days # => 0.5
def to_days def in_days
to_seconds / SECONDS_PER_DAY.to_f in_seconds / SECONDS_PER_DAY.to_f
end end
# Returns the amount of weeks a duration covers as a float # Returns the amount of weeks a duration covers as a float
# #
# 2.months.to_weeks # => 8.696 # 2.months.in_weeks # => 8.696
def to_weeks def in_weeks
to_seconds / SECONDS_PER_WEEK.to_f in_seconds / SECONDS_PER_WEEK.to_f
end end
# Returns the amount of months a duration covers as a float # Returns the amount of months a duration covers as a float
# #
# 9.weeks.to_months # => 2.07 # 9.weeks.in_months # => 2.07
def to_months def in_months
to_seconds / SECONDS_PER_MONTH.to_f in_seconds / SECONDS_PER_MONTH.to_f
end end
# Returns the amount of years a duration covers as a float # Returns the amount of years a duration covers as a float
# #
# 30.days.to_years # => 0.082 # 30.days.in_years # => 0.082
def to_years def in_years
to_seconds / SECONDS_PER_YEAR.to_f in_seconds / SECONDS_PER_YEAR.to_f
end end
# Returns +true+ if +other+ is also a Duration instance, which has the # Returns +true+ if +other+ is also a Duration instance, which has the

View file

@ -46,34 +46,39 @@ class DurationTest < ActiveSupport::TestCase
assert_equal "1", 1.second.to_s assert_equal "1", 1.second.to_s
end end
def test_to_minutes def test_in_seconds
assert_in_delta 1440.0, 1.day.to_minutes assert_equal 86400.0, 1.day.in_seconds
assert_in_delta 0.5, 30.seconds.to_minutes assert_equal 1.week.to_i, 1.week.in_seconds
end end
def test_to_hours def test_in_minutes
assert_in_delta 24.0, 1.day.to_hours assert_in_delta 1440.0, 1.day.in_minutes
assert_in_delta 336.0, 2.weeks.to_hours assert_in_delta 0.5, 30.seconds.in_minutes
end end
def test_to_days def test_in_hours
assert_in_delta 0.5, 12.hours.to_days assert_in_delta 24.0, 1.day.in_hours
assert_in_delta 30.437, 1.month.to_days assert_in_delta 336.0, 2.weeks.in_hours
end end
def test_to_weeks def test_in_days
assert_in_delta 8.696, 2.months.to_weeks assert_in_delta 0.5, 12.hours.in_days
assert_in_delta 52.178, 1.year.to_weeks assert_in_delta 30.437, 1.month.in_days
end end
def test_to_months def test_in_weeks
assert_in_delta 2.07, 9.weeks.to_months assert_in_delta 8.696, 2.months.in_weeks
assert_in_delta 12.0, 1.year.to_months assert_in_delta 52.178, 1.year.in_weeks
end end
def test_to_years def test_in_months
assert_in_delta 0.082, 30.days.to_years assert_in_delta 2.07, 9.weeks.in_months
assert_in_delta 1.0, 365.days.to_years assert_in_delta 12.0, 1.year.in_months
end
def test_in_years
assert_in_delta 0.082, 30.days.in_years
assert_in_delta 1.0, 365.days.in_years
end end
def test_eql def test_eql