-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathappointment_book.rb
71 lines (55 loc) · 1.09 KB
/
appointment_book.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
require "date"
require "pstore"
class Schedule
include Enumerable
def initialize(file="schedule.store")
@store = PStore.new(file)
end
def [](date)
@store.transaction do
events_for(date)
end
end
def event(date, text)
@store.transaction do
events_for(date) << text
end
end
def event_update(date, index, text)
@store.transaction do
events_for(date)[index].replace(text)
end
end
def empty?
@store.roots.empty?
end
#remove entire date
def clear_date(date)
@store.transaction do
@store.delete(to_date(date))
end
end
#remove a single event
def remove(date, index)
@store.transaction do
events_for(date).delete_at(index)
end
end
def each
@store.transaction do
@store.roots.sort.each { |date| yield([date, @store[date]]) }
end
end
def to_s
map do |(date, appointments)|
date.strftime("%m/%d/%Y") + " (#{appointments.length})"
end.join("\n")
end
private
def events_for(date)
@store[to_date(date)] ||= []
end
def to_date(str)
Date.strptime(str, "%m/%d/%Y")
end
end