mirror of https://github.com/rails/rails
Merge pull request #4985 from shigeya/range_overlaps_to_use_cover
make Range#overlaps? accept Range of Time
This commit is contained in:
commit
fc3fad8d85
|
@ -3,6 +3,6 @@ class Range
|
|||
# (1..5).overlaps?(4..6) # => true
|
||||
# (1..5).overlaps?(7..9) # => false
|
||||
def overlaps?(other)
|
||||
include?(other.first) || other.include?(first)
|
||||
cover?(other.first) || other.cover?(first)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -71,4 +71,16 @@ class RangeTest < ActiveSupport::TestCase
|
|||
range = (1..3)
|
||||
assert range.method(:include?) != range.method(:cover?)
|
||||
end
|
||||
|
||||
def test_overlaps_on_time
|
||||
time_range_1 = Time.utc(2005, 12, 10, 15, 30)..Time.utc(2005, 12, 10, 17, 30)
|
||||
time_range_2 = Time.utc(2005, 12, 10, 17, 00)..Time.utc(2005, 12, 10, 18, 00)
|
||||
assert time_range_1.overlaps?(time_range_2)
|
||||
end
|
||||
|
||||
def test_no_overlaps_on_time
|
||||
time_range_1 = Time.utc(2005, 12, 10, 15, 30)..Time.utc(2005, 12, 10, 17, 30)
|
||||
time_range_2 = Time.utc(2005, 12, 10, 17, 31)..Time.utc(2005, 12, 10, 18, 00)
|
||||
assert !time_range_1.overlaps?(time_range_2)
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue