-
Notifications
You must be signed in to change notification settings - Fork 0
/
calendar.rb
85 lines (71 loc) · 2.25 KB
/
calendar.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
module Heroes
module Calendar
module Calendar
State = Data.define(:current_day, :current_week, :current_month)
class << self
def decide(command, state)
case command
when StartDay
handle_start_day(command, state)
when FinishDay
handle_finish_day(command, state)
else
raise "Unknown command"
end
end
def evolve(state, event)
case event
when DayStarted
State.new(
current_day: event.day,
current_week: event.week,
current_month: event.month
)
when DayFinished
state
else
raise "Unknown event"
end
end
def initial_state
State.new(current_day: 0, current_week: 0, current_month: 0)
end
private
def handle_start_day(command, state)
validate_start_day(command, state)
[ DayStarted.new(month: command.month, week: command.week, day: command.day) ]
end
def handle_finish_day(command, state)
validate_finish_day(command, state)
[ DayFinished.new(month: command.month, week: command.week, day: command.day) ]
end
def validate_start_day(command, state)
raise CannotSkipDays unless next_day?(command, state)
end
def validate_finish_day(command, state)
raise CanOnlyFinishCurrentDay unless current_day?(command, state)
end
def next_day?(command, state)
return true if state.current_day == 0 # First day
next_day = state.current_day + 1
next_week = state.current_week
next_month = state.current_month
if next_day > 7
next_day = 1
next_week += 1
end
if next_week > 4
next_week = 1
next_month += 1
end
command.day == next_day && command.week == next_week && command.month == next_month
end
def current_day?(command, state)
command.day == state.current_day &&
command.week == state.current_week &&
command.month == state.current_month
end
end
end
end
end