Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add shift.overlaps? and shift.contains? #16

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,29 @@ Include?
Shift.new(TimeOfDay.new(20), TimeOfDay.new(4)).include?(TimeOfDay.new(2)) # => true
Shift.new(TimeOfDay.new(20), TimeOfDay.new(4)).include?(TimeOfDay.new(18)) # => false


Overlap?
--------------------
breakfast = Shift.new(TimeOfDay.new(8), TimeOfDay.new(11))
lunch = Shift.new(TimeOfDay.new(10), TimeOfDay.new(14))
breakfast.overlaps?(lunch) # => true
lunch.overlaps?(breakfast) # => true

dinner = Shift.new(TimeOfDay.new(18), TimeOfDay.new(20))
dinner.overlaps?(lunch) # => false


Contains?
--------------------
workday = Shift.new(TimeOfDay.new(9), TimeOfDay.new(17))
lunch = Shift.new(TimeOfDay.new(10), TimeOfDay.new(14))
workday.contains?(lunch) # => true
lunch.contains?(workday) # => false

dinner = Shift.new(TimeOfDay.new(18), TimeOfDay.new(20))
dinner.overlaps?(lunch) # => false


Rails Time Zone Support
=======================

Expand Down
11 changes: 11 additions & 0 deletions lib/tod/shift.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@ def include?(tod)
end
end

def overlaps?(shift)
self.include?(shift.beginning) ||
self.include?(shift.ending) ||
shift.include?(self.beginning) ||
shift.include?(self.ending)
end

def contains?(shift)
self.include?(shift.beginning) && self.include?(shift.ending)
end

# Return shift duration in seconds.
# if ending is lower than beginning this method will calculate the duration as the ending time is from the following day
def duration
Expand Down
40 changes: 40 additions & 0 deletions test/tod/shift_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,46 @@ class ShiftTest < Test::Unit::TestCase
end
end

context "overlaps?" do
should "be true when shifts overlap" do
shift1 = Shift.new(TimeOfDay.new(12), TimeOfDay.new(18))
shift2 = Shift.new(TimeOfDay.new(13), TimeOfDay.new(15))
assert_true shift1.overlaps?(shift2)
end

should "be false when shifts don't overlap" do
shift1 = Shift.new(TimeOfDay.new(1), TimeOfDay.new(5))
shift2 = Shift.new(TimeOfDay.new(9), TimeOfDay.new(12))
assert_false shift1.overlaps?(shift2)
end

should "be true when shifts touch" do
shift1 = Shift.new(TimeOfDay.new(1), TimeOfDay.new(5))
shift2 = Shift.new(TimeOfDay.new(5), TimeOfDay.new(12))
assert_true shift1.overlaps?(shift2)
end
end

context "contains?" do
should "be true when one shift contains another" do
outside = Shift.new(TimeOfDay.new(12), TimeOfDay.new(18))
inside = Shift.new(TimeOfDay.new(13), TimeOfDay.new(15))
assert_true outside.contains?(inside)
end

should "be false when a shift is contained by another" do
outside = Shift.new(TimeOfDay.new(12), TimeOfDay.new(18))
inside = Shift.new(TimeOfDay.new(13), TimeOfDay.new(15))
assert_false inside.contains?(outside)
end

should "be false when shifts don't even overlap" do
shift1 = Shift.new(TimeOfDay.new(12), TimeOfDay.new(15))
shift2 = Shift.new(TimeOfDay.new(18), TimeOfDay.new(19))
assert_false shift1.contains?(shift2)
end
end

context "include?" do
# |------------------------|--------T1----V----T2----|------------------------|
should "be true when value is between ToDs and boths tods are in the same day" do
Expand Down