From 6f68e632746f72469cbd1c1b6cb9d3f1ed1730a0 Mon Sep 17 00:00:00 2001 From: Shigeya Suzuki Date: Thu, 9 Feb 2012 22:08:28 -0800 Subject: make Range#overlaps? accept Range of Time --- activesupport/lib/active_support/core_ext/range/overlaps.rb | 2 +- activesupport/test/core_ext/range_ext_test.rb | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/activesupport/lib/active_support/core_ext/range/overlaps.rb b/activesupport/lib/active_support/core_ext/range/overlaps.rb index 7df653b53f..603657c180 100644 --- a/activesupport/lib/active_support/core_ext/range/overlaps.rb +++ b/activesupport/lib/active_support/core_ext/range/overlaps.rb @@ -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 diff --git a/activesupport/test/core_ext/range_ext_test.rb b/activesupport/test/core_ext/range_ext_test.rb index 8a91f6d69c..cf1ec448c2 100644 --- a/activesupport/test/core_ext/range_ext_test.rb +++ b/activesupport/test/core_ext/range_ext_test.rb @@ -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 -- cgit v1.2.3