Skip to content

Commit 73b402a

Browse files
committed
Add shift.overlaps? and shift.contains?
1 parent 21e5d81 commit 73b402a

3 files changed

Lines changed: 74 additions & 0 deletions

File tree

README.markdown

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,29 @@ Include?
137137
Shift.new(TimeOfDay.new(20), TimeOfDay.new(4)).include?(TimeOfDay.new(2)) # => true
138138
Shift.new(TimeOfDay.new(20), TimeOfDay.new(4)).include?(TimeOfDay.new(18)) # => false
139139

140+
141+
Overlap?
142+
--------------------
143+
breakfast = Shift.new(TimeOfDay.new(8), TimeOfDay.new(11))
144+
lunch = Shift.new(TimeOfDay.new(10), TimeOfDay.new(14))
145+
breakfast.overlaps?(lunch) # => true
146+
lunch.overlaps?(breakfast) # => true
147+
148+
dinner = Shift.new(TimeOfDay.new(18), TimeOfDay.new(20))
149+
dinner.overlaps?(lunch) # => false
150+
151+
152+
Contains?
153+
--------------------
154+
workday = Shift.new(TimeOfDay.new(9), TimeOfDay.new(17))
155+
lunch = Shift.new(TimeOfDay.new(10), TimeOfDay.new(14))
156+
workday.contains?(lunch) # => true
157+
lunch.contains?(workday) # => false
158+
159+
dinner = Shift.new(TimeOfDay.new(18), TimeOfDay.new(20))
160+
dinner.overlaps?(lunch) # => false
161+
162+
140163
Rails Time Zone Support
141164
=======================
142165

lib/tod/shift.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,17 @@ def include?(tod)
2222
end
2323
end
2424

25+
def overlaps?(shift)
26+
self.include?(shift.beginning) ||
27+
self.include?(shift.ending) ||
28+
shift.include?(self.beginning) ||
29+
shift.include?(self.ending)
30+
end
31+
32+
def contains?(shift)
33+
self.include?(shift.beginning) && self.include?(shift.ending)
34+
end
35+
2536
# Return shift duration in seconds.
2637
# if ending is lower than beginning this method will calculate the duration as the ending time is from the following day
2738
def duration

test/tod/shift_test.rb

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,46 @@ class ShiftTest < Test::Unit::TestCase
2727
end
2828
end
2929

30+
context "overlaps?" do
31+
should "be true when shifts overlap" do
32+
shift1 = Shift.new(TimeOfDay.new(12), TimeOfDay.new(18))
33+
shift2 = Shift.new(TimeOfDay.new(13), TimeOfDay.new(15))
34+
assert_true shift1.overlaps?(shift2)
35+
end
36+
37+
should "be false when shifts don't overlap" do
38+
shift1 = Shift.new(TimeOfDay.new(1), TimeOfDay.new(5))
39+
shift2 = Shift.new(TimeOfDay.new(9), TimeOfDay.new(12))
40+
assert_false shift1.overlaps?(shift2)
41+
end
42+
43+
should "be true when shifts touch" do
44+
shift1 = Shift.new(TimeOfDay.new(1), TimeOfDay.new(5))
45+
shift2 = Shift.new(TimeOfDay.new(5), TimeOfDay.new(12))
46+
assert_true shift1.overlaps?(shift2)
47+
end
48+
end
49+
50+
context "contains?" do
51+
should "be true when one shift contains another" do
52+
outside = Shift.new(TimeOfDay.new(12), TimeOfDay.new(18))
53+
inside = Shift.new(TimeOfDay.new(13), TimeOfDay.new(15))
54+
assert_true outside.contains?(inside)
55+
end
56+
57+
should "be false when a shift is contained by another" do
58+
outside = Shift.new(TimeOfDay.new(12), TimeOfDay.new(18))
59+
inside = Shift.new(TimeOfDay.new(13), TimeOfDay.new(15))
60+
assert_false inside.contains?(outside)
61+
end
62+
63+
should "be false when shifts don't even overlap" do
64+
shift1 = Shift.new(TimeOfDay.new(12), TimeOfDay.new(15))
65+
shift2 = Shift.new(TimeOfDay.new(18), TimeOfDay.new(19))
66+
assert_false shift1.contains?(shift2)
67+
end
68+
end
69+
3070
context "include?" do
3171
# |------------------------|--------T1----V----T2----|------------------------|
3272
should "be true when value is between ToDs and boths tods are in the same day" do

0 commit comments

Comments
 (0)