diff --git a/lib/booking.rb b/lib/booking.rb index 2821d29..d011b65 100644 --- a/lib/booking.rb +++ b/lib/booking.rb @@ -1,15 +1,9 @@ module Booking - Event = Data.define(:data, :event_type) do - def initialize(data: Hash.new) - super(data: data, event_type: self.class.name) - end - end + ScheduleReleased = Data.define(:scheduled_at, :duration) + ScheduleReserved = Data.define(:scheduled_at, :duration) - ScheduleReleased = Class.new(Event) - ScheduleReserved = Class.new(Event) - - AppointmentProposed = Class.new(Event) - AppointmentAccepted = Class.new(Event) - AppointmentRejected = Class.new(Event) - AppointmentCancelled = Class.new(Event) + AppointmentProposed = Data.define(:id, :proposed_at) + AppointmentAccepted = Data.define(:id, :accepted_at) + AppointmentRejected = Data.define(:id, :rejected_at) + AppointmentCancelled = Data.define(:id, :cancelled_at) end diff --git a/lib/booking/appointment.rb b/lib/booking/appointment.rb index fe79988..d9c0f8e 100644 --- a/lib/booking/appointment.rb +++ b/lib/booking/appointment.rb @@ -11,24 +11,24 @@ def initialize(id, clock:) def propose invalid_state unless @state.initial? @state.to_proposed - AppointmentProposed.new(data: { id: @id, proposed_at: @clock.call }) + AppointmentProposed.new(id: @id, proposed_at: @clock.call) end def accept invalid_state unless @state.proposed? @state.to_accepted - AppointmentAccepted.new(data: { id: @id, accepted_at: @clock.call }) + AppointmentAccepted.new(id: @id, accepted_at: @clock.call) end def reject invalid_state unless @state.proposed? @state.to_rejected - AppointmentRejected.new(data: { id: @id, rejected_at: @clock.call }) + AppointmentRejected.new(id: @id, rejected_at: @clock.call) end def cancel invalid_state unless @state.accepted? || @state.proposed? - AppointmentCancelled.new(data: { id: @id, cancelled_at: @clock.call }) + AppointmentCancelled.new(id: @id, cancelled_at: @clock.call) end private diff --git a/lib/booking/day_schedule.rb b/lib/booking/day_schedule.rb index 60af62a..0814ed3 100644 --- a/lib/booking/day_schedule.rb +++ b/lib/booking/day_schedule.rb @@ -26,19 +26,15 @@ def release(time_range) def visit_scheduled(time_range) ScheduleReserved.new( - data: { - scheduled_at: time_range.first, - duration: time_range.last - time_range.first - } + scheduled_at: time_range.first, + duration: time_range.last - time_range.first ) end def visit_released(time_range) ScheduleReleased.new( - data: { - scheduled_at: time_range.first, - duration: time_range.last - time_range.first - } + scheduled_at: time_range.first, + duration: time_range.last - time_range.first ) end diff --git a/test/lib/booking/appointment_test.rb b/test/lib/booking/appointment_test.rb index 5886b01..05bd19c 100644 --- a/test/lib/booking/appointment_test.rb +++ b/test/lib/booking/appointment_test.rb @@ -6,7 +6,7 @@ class AppointmentTest < ActiveSupport::TestCase test "propose" do appointment = new_appointment - assert_event appointment_proposed, appointment.propose + assert_domain_event appointment_proposed, appointment.propose end test "cannot propose twice" do @@ -20,7 +20,7 @@ class AppointmentTest < ActiveSupport::TestCase appointment = new_appointment appointment.propose - assert_event appointment_accepted, appointment.accept + assert_domain_event appointment_accepted, appointment.accept end test "cannot accept without proposal" do @@ -40,7 +40,7 @@ class AppointmentTest < ActiveSupport::TestCase appointment = new_appointment appointment.propose - assert_event appointment_rejected, appointment.reject + assert_domain_event appointment_rejected, appointment.reject end test "cannot reject without proposal" do @@ -92,7 +92,7 @@ class AppointmentTest < ActiveSupport::TestCase appointment = new_appointment appointment.propose - assert_event appointment_cancelled, appointment.cancel + assert_domain_event appointment_cancelled, appointment.cancel end test "cancel accepted appointment" do @@ -100,7 +100,7 @@ class AppointmentTest < ActiveSupport::TestCase appointment.propose appointment.accept - assert_event appointment_cancelled, appointment.cancel + assert_domain_event appointment_cancelled, appointment.cancel end test "cannot cancel rejected appointment" do @@ -136,37 +136,29 @@ def new_appointment def appointment_proposed AppointmentProposed.new( - data: { - id: appointment_id, - proposed_at: current_time - } + id: appointment_id, + proposed_at: current_time ) end def appointment_accepted AppointmentAccepted.new( - data: { - id: appointment_id, - accepted_at: current_time - } + id: appointment_id, + accepted_at: current_time ) end def appointment_rejected AppointmentRejected.new( - data: { - id: appointment_id, - rejected_at: current_time - } + id: appointment_id, + rejected_at: current_time ) end def appointment_cancelled AppointmentCancelled.new( - data: { - id: appointment_id, - cancelled_at: current_time - } + id: appointment_id, + cancelled_at: current_time ) end end diff --git a/test/lib/booking/day_schedule_test.rb b/test/lib/booking/day_schedule_test.rb index c1a4d75..8521b2c 100644 --- a/test/lib/booking/day_schedule_test.rb +++ b/test/lib/booking/day_schedule_test.rb @@ -7,7 +7,7 @@ class DayScheduleTest < ActiveSupport::TestCase test "happy path" do day_schedule = DaySchedule.new(open_hours) - assert_event visit_scheduled(slot), day_schedule.reserve(slot) + assert_domain_event visit_scheduled(slot), day_schedule.reserve(slot) end test "cannot reserve same slot twice" do @@ -41,7 +41,7 @@ class DayScheduleTest < ActiveSupport::TestCase day_schedule = DaySchedule.new(open_hours) day_schedule.reserve(rush_by(1.hour, slot)) - assert_event visit_scheduled(slot), day_schedule.reserve(slot) + assert_domain_event visit_scheduled(slot), day_schedule.reserve(slot) end test "cannot reserve outside of open hours" do @@ -54,7 +54,7 @@ class DayScheduleTest < ActiveSupport::TestCase day_schedule = DaySchedule.new(open_hours) day_schedule.reserve(slot) - assert_event visit_released(slot), day_schedule.release(slot) + assert_domain_event visit_released(slot), day_schedule.release(slot) end test "released slot can be taken again" do @@ -91,19 +91,15 @@ def rush_by(seconds, slot) def visit_scheduled(time_range) ScheduleReserved.new( - data: { - scheduled_at: time_range.first, - duration: time_range.last - time_range.first - } + scheduled_at: time_range.first, + duration: time_range.last - time_range.first ) end def visit_released(time_range) ScheduleReleased.new( - data: { - scheduled_at: time_range.first, - duration: time_range.last - time_range.first - } + scheduled_at: time_range.first, + duration: time_range.last - time_range.first ) end end diff --git a/test/test_helper.rb b/test/test_helper.rb index f7846e4..cd151d7 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -13,9 +13,9 @@ class TestCase # Add more helper methods to be used by all tests here... - def assert_event(expected, actual) - assert_equal expected.event_type, actual.event_type - assert_equal expected.data, actual.data + def assert_domain_event(expected, actual) + assert_equal expected.class, actual.class + assert_equal expected.deconstruct_keys(nil), actual.deconstruct_keys(nil) end end end