diff --git a/lib/booking/day_schedule.rb b/lib/booking/day_schedule.rb index d991f8a..0bf21f8 100644 --- a/lib/booking/day_schedule.rb +++ b/lib/booking/day_schedule.rb @@ -2,11 +2,13 @@ module Booking class DaySchedule CannotReserve = Class.new(StandardError) - def initialize + def initialize(allowed_time_range) @reserved_slots = Set.new + @allowed_time_range = allowed_time_range end def reserve(time_range) + cannot_reserve unless allowed_time_range.cover?(time_range) cannot_reserve if reserved_slots.any?(covers?(time_range)) reserved_slots << time_range visit_scheduled(time_range) @@ -14,13 +16,15 @@ def reserve(time_range) private - attr_reader :reserved_slots + attr_reader :reserved_slots, :allowed_time_range def visit_scheduled(time_range) - VisitScheduled.new(data: { - scheduled_at: time_range.first, - duration: time_range.last - time_range.first - }) + VisitScheduled.new( + data: { + scheduled_at: time_range.first, + duration: time_range.last - time_range.first + } + ) end def covers?(time_range) @@ -31,4 +35,4 @@ def cannot_reserve raise CannotReserve end end -end \ No newline at end of file +end diff --git a/test/lib/booking/day_schedule_test.rb b/test/lib/booking/day_schedule_test.rb index 4a4b9f6..5bcf1ae 100644 --- a/test/lib/booking/day_schedule_test.rb +++ b/test/lib/booking/day_schedule_test.rb @@ -5,14 +5,14 @@ class DayScheduleTest < ActiveSupport::TestCase cover DaySchedule test "happy path" do - day_schedule = DaySchedule.new + day_schedule = DaySchedule.new(open_hours) assert_event visit_scheduled(at_12_00..at_13_00), day_schedule.reserve(at_12_00..at_13_00) end test "cannot reserve same slot twice" do - day_schedule = DaySchedule.new + day_schedule = DaySchedule.new(open_hours) day_schedule.reserve(at_12_00..at_13_00) assert_raises DaySchedule::CannotReserve do @@ -21,7 +21,7 @@ class DayScheduleTest < ActiveSupport::TestCase end test "cannot reserve partially covered slot" do - day_schedule = DaySchedule.new + day_schedule = DaySchedule.new(open_hours) day_schedule.reserve(at_12_00..at_13_00) assert_raises DaySchedule::CannotReserve do @@ -30,7 +30,7 @@ class DayScheduleTest < ActiveSupport::TestCase end test "cannot reserve partially covered slot, again" do - day_schedule = DaySchedule.new + day_schedule = DaySchedule.new(open_hours) day_schedule.reserve(at_12_00..at_13_00) assert_raises DaySchedule::CannotReserve do @@ -39,15 +39,26 @@ class DayScheduleTest < ActiveSupport::TestCase end test "can reserve non-intersecting slots" do - day_schedule = DaySchedule.new + day_schedule = DaySchedule.new(open_hours) day_schedule.reserve(at_12_00..at_12_45) assert_event visit_scheduled(at_13_00..at_13_45), day_schedule.reserve(at_13_00..at_13_45) end + test "cannot reserve outside of open hours" do + day_schedule = DaySchedule.new(open_hours) + open_at, close_at = open_hours.first, open_hours.last + + assert_raises { day_schedule.reserve(open_at..(close_at + 1.hour)) } + end + private + def open_hours + at_11_00..at_19_00 + end + def visit_scheduled(time_range) VisitScheduled.new( data: { @@ -57,6 +68,10 @@ def visit_scheduled(time_range) ) end + def at_11_00 + Time.new(2023, 10, 10, 11, 00) + end + def at_11_45 Time.new(2023, 10, 10, 11, 45) end @@ -76,5 +91,9 @@ def at_13_00 def at_13_45 Time.new(2023, 10, 10, 13, 45) end + + def at_19_00 + Time.new(2023, 10, 10, 19, 00) + end end -end \ No newline at end of file +end