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.
上级 b50ce8d6
* 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
......
# frozen_string_literal: true
require "active_support/time_with_zone"
require "active_support/deprecation"
module ActiveSupport
module IncludeTimeWithZone #:nodoc:
......@@ -9,9 +10,13 @@ module IncludeTimeWithZone #:nodoc:
# (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
......
......@@ -191,11 +191,23 @@ def test_step_on_time_with_zone
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
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册