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

Deprecate using Range#include? to check the inclusion of a value in a date time range

Use `Range#cover?` instead of `Range#include?` to check the inclusion of a value in a date time range.
This commit is contained in:
Vishal Telangre 2020-01-08 17:15:10 +05:30
parent b50ce8d6cf
commit 740bb178d9
No known key found for this signature in database
GPG key ID: 16BA3EBD701E48D8
3 changed files with 27 additions and 3 deletions

View file

@ -1,3 +1,10 @@
* Deprecate using `Range#include?` method to check the inclusion of a value
in a date time range. It is recommended to use `Range#cover?` method
instead of `Range#include?` to check the inclusion of a value
in a date time range.
*Vishal Telangre*
* Support added for a `round_mode` parameter, in all number helpers. (See: `BigDecimal::mode`.)
```ruby

View file

@ -1,6 +1,7 @@
# frozen_string_literal: true
require "active_support/time_with_zone"
require "active_support/deprecation"
module ActiveSupport
module IncludeTimeWithZone #:nodoc:
@ -9,9 +10,13 @@ module ActiveSupport
# (1.hour.ago..1.hour.from_now).include?(Time.current) # => true
#
def include?(value)
if self.begin.is_a?(TimeWithZone)
cover?(value)
elsif self.end.is_a?(TimeWithZone)
if self.begin.is_a?(TimeWithZone) || self.end.is_a?(TimeWithZone)
ActiveSupport::Deprecation.warn(<<-MSG.squish)
Using `Range#include?` to check the inclusion of a value in
a date time range is deprecated.
It is recommended to use `Range#cover?` instead of `Range#include?` to
check the inclusion of a value in a date time range.
MSG
cover?(value)
else
super

View file

@ -191,11 +191,23 @@ class RangeTest < ActiveSupport::TestCase
end
end
def test_cover_on_time_with_zone
twz = ActiveSupport::TimeWithZone.new(nil, ActiveSupport::TimeZone["Eastern Time (US & Canada)"], Time.utc(2006, 11, 28, 10, 30))
assert ((twz - 1.hour)..twz).cover?(twz)
end
def test_include_on_time_with_zone
twz = ActiveSupport::TimeWithZone.new(nil, ActiveSupport::TimeZone["Eastern Time (US & Canada)"], Time.utc(2006, 11, 28, 10, 30))
assert ((twz - 1.hour)..twz).include?(twz)
end
def test_include_on_time_with_zone_deprecation
twz = ActiveSupport::TimeWithZone.new(nil, ActiveSupport::TimeZone["Eastern Time (US & Canada)"], Time.utc(2006, 11, 28, 10, 30))
assert_deprecated do
((twz - 1.hour)..twz).include?(twz)
end
end
def test_case_equals_on_time_with_zone
twz = ActiveSupport::TimeWithZone.new(nil, ActiveSupport::TimeZone["Eastern Time (US & Canada)"], Time.utc(2006, 11, 28, 10, 30))
assert ((twz - 1.hour)..twz) === twz