-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathappointment_book_ui.rb
195 lines (137 loc) · 2.86 KB
/
appointment_book_ui.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
require "rubygems"
require "highline/import"
$terminal = HighLine.new($stdin, $stdout, :auto)
require "appointment_book"
def main
choose do |menu|
menu.index = :letter
menu.choice :new_entry do
create_new
end
menu.choice :view do
view
end
menu.choice :edit do
edit
end
menu.choice :quit do
exit
end
end
end
def ask_date
date = ask("Date? (m(m)/d(d)/yyyy)")
if date =~ /\A[0-1][0-9]\/[0-3][0-9]\/\d{4}\Z/
date
else
say "Something's wrong."
ask_date
end
end
def display_date(date)
@sch[date].each_with_index {|event, index| say "#{index}: \n#{event}"}
end
def create_new
date = ask_date
event = ask("What? ")
time = ask("When? ")
location = ask("Where? ")
details = ask("Details? ")
@sch.event(date, T(:event, binding))
say "\n\nYour entry: #{date}\n"
display_date(date)
if agree("Edit? ", true)
edit(date)
end
main
end
def edit(date = nil)
unless date
date = ask_date
end
choose do |menu|
menu.choice :delete do
date
choose do |menu|
menu.choice :all do
@sch.clear_date(date)
end
menu.choice :single_event do
display_date(date)
index = ask("Which event? ", Integer) { |q|
q.in = 0..@sch[date].length.to_i }
@sch.remove(date, index)
end
menu.choice :back do
edit
end
end
end
menu.choice :change do
choose do |menu|
menu.choice :replace do
date
display_date(date)
index = ask("Which event? ", Integer) { |q|
q.in = 0..@sch[date].length.to_i - 1 }
#don't need to_i
say "#{@sch[date][index]}"
#select date/time/details etc.
#Options: Add or replace.
say "Please enter new details:\n "
event = ask("What? ")
time = ask("When? ")
location = ask("Where? ")
details = ask("Details? ")
@sch.event_update(date, index, T(:event, binding))
say "\n\nYour entry: #{date}\n"
display_date(date)
if agree("Edit? ", true)
edit(date)
end
end
end
main
end
menu.choice :back do
main
end
end
main
end
def view
choose do |menu|
menu.prompt = "Please select: "
menu.choice :all_appointments do
puts @sch
end
menu.choice :single_date do
date = ask_date
display_date(date)
end
menu.choice :today do
d = Time.now
date = d.strftime("%m/%d/%Y")
display_date(date)
end
menu.choice :back do
main
end
end
choose do |menu|
menu.choice :edit do
edit
end
menu.choice :back do
main
end
end
end
def T(name, bound_to)
file = File.read(File.dirname(__FILE__) + "/templates/#{name}.erb")
ERB.new(file).result(bound_to)
end
say "\nWelcome To Chenoa's Appointment Book"
@sch = Schedule.new(ARGV[0] || "schedule.store")
say "\nYour schedule: \n\n#{@sch}\n\n"
main