-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathruby_notes
executable file
·581 lines (551 loc) · 26.4 KB
/
ruby_notes
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
#!/usr/bin/env ruby
$LOAD_PATH << "lib"
$LOAD_PATH << "models"
require 'environment'
Environment.environment = ENV["ENVIRONMENT"] || "production"
$stderr = $stdout
require 'user'
require 'note'
require 'print_notes'
require 'super_user'
#---------------menus--------------#
def menu
<<EOS
====================================================================================================================================================
| .__ __ ___. _______ __ |
|__ _ __ ____ | | ____ ____ _____ ____ _/ |_ ____ _______ __ _\\_ |__ ___.__.\\ \\ _____/ |_ ____ ______ |
|\\ \\/ \\/ // __ \\| | _/ ___\\/ _ \\ / \\_/ __ \\ \\ __\\/ _ \\ \\_ __ \\ | \\ __ < | |/ | \\ / _ \\ __\\/ __ \\ / ___/ |
| \\ /\\ ___/| |_\\ \\__( <_> ) Y Y \\ ___/ | | ( <_> ) | | \\/ | / \\_\\ \\___ / | ( <_> ) | \\ ___/ \\___ \\ |
| \\/\\_/ \\___ >____/\\___ >____/|__|_| /\\___ > |__| \\____/ |__| |____/|___ / ____\\____|__ /\\____/|__| \\___ >____ > |
| \\/ \\/ \\/ \\/ \\/\\/ \\/ \\/ \\/ |
| |
| |
|Welcome to Ruby_Notes! |
|What do you want to do? |
| |
|1.==> Register or Login |
| |
|2.==> Create a Note |
| |
|3.==> Modify a Note |
| |
|4.==> Search |
| |
|5.==> Count all my Notes |
| Ruby_Notes_1.1 |
|=================================================================================================================================================/
EOS
end
def search_menu
<<EOS
What kind of search you want to do? _,aggdd888bbgg,,_
,ad88888YYYYYYYYYYY8888ba,
,d888P""' ``""Y88b,
,d888"' "Y888,
d88P' `Y88b,
,d88' `Y88,
,888' `Y88,
,d88' 1.==> By Title `Y8b,
d88' 2.==> By Tag `88I
,88P 3.==> Show All I88
I88I I88
I88I I8I
`888, d8I
`888, d88'
`888, d8PI
,dP"8b, ,8P'd'
,dP' "Yb, _,d8" P'
,dP' ,db, "Yb,_ ,ad8P" ,P'
,dP' ,d8888b, `"Yba,,__ __,ad88P" ,d"
,dP' ,d88888888b, "88Y8888888888PP"" _,d"
,dP' ,d888888888888P ,d"8 _,gd"'
,dP' ,d888888888888P' ,d" ,8bbaagggggaaddP""'
,dP' ,d888888888888P' ,d" ,d"'
,dP' ,d888888888888P' ,d" ,d"
,dP' ,d888888888888P' ,d" ,d"
,dP' ,d888888888888P' ,d" ,d"
,dP' ,d888888888888P' ,d" ,d"
,dP' ,d888888888888P' ,d" ,d"
,dP' ,d888888888888P' ,d" ,d"
dP' d888888888888P' ,d" ,d"
8"Ya, `888888888P' ,d" ,d"
8 "Ya, `88888P' ,d" ,d"
8a, "Ya, `8P' ,d" ,d"
"Ya, "Ya, ,d" ,d"
"Ya, "Y8P" ,d"
"Ya, 8 ,d"
"Ya,8d"
"YP
EOS
end
def what_now_menu(user)
<<EOS
====================================================================================================================================================
| __ __.__ __ _______ _________ |
|/ \\ / \\ |__ _____ _/ |_ \\ \\ ______ _ _\\_____ \\ |
|\\ \\/\\/ / | \\\\__ \\\\ __\\ / | \\ / _ \\ \\/ \\/ / / __/ |
| \\ /| Y \\/ __ \\| | / | ( <_> ) / | | |
| \\__/\\ / |___| (____ /__| \\____|__ /\\____/ \\/\\_/ |___| |
| \\/ \\/ \\/ \\/ <___> |
| |
|1.==> Create a Note |
| |
|2.==> Modify a Note |
| |
|3.==> Search |
| |
|4.==> Let's play! |
| USER: #{user} |
|5.==> Super_User |
| Ruby_Notes_1.1 |
|6.==> Quit |
|=================================================================================================================================================/
EOS
end
def create_menu(user)
<<EOS
=============================================================================
|What is the title of the note you want to create? |
| (`/\\ |
| `=\\/\\ |
| `=\\/\\ |
| `=\\/ |
| _\\___ |
| ) ( USER: #{user} |
| ( INK ) |
| \\___/ Ruby_Notes_1.1|
=============================================================================
EOS
end
def modify_menu(user)
<<EOS
=============================================================================
|What do you want to do? |
| (`/\\ |
| `=\\/\\ |
| `=\\/\\ 1.==> Write |
| `=\\/ 2.==> Delete Note |
| _\\___ 3.==> Back |
| ) ( USER: #{user} |
| ( INK ) |
| \\___/ Ruby_Notes_1.1|
=============================================================================
EOS
end
def quit_menu
<<EOS
=============================================================================
| ''~`` |
| ( o o ) |
| +--------------------.oooO--(_)--Oooo.------------------+ |
| | | |
| | Are you sure you want to quit this awesome app?(y/n) | |
| | .oooO | |
| | ( ) Oooo. | |
| +-----------------------\\ (----( )--------------------+ |
| \\_) ) / |
| (_/ |
| |
| Ruby_Notes_1.1|
=============================================================================
EOS
end
def who_are_you
<<EOS
=============================================================================
| ________ |
| _jgN########Ngg_ |
| _N##N@@"" ""9NN##Np_ |
| d###P N####p |
| "^^" T#### |
| d###P |
| _g####F |
| _gN###P |
| gN###F" Not so fast!!! |
| d###F |
| 0###F Who are you? |
| 0###F |
| 0###F |
| "NN@' |
| |
| ___ |
| q###r |
| "" |
| |
| Ruby_Notes_1.1|
=============================================================================
EOS
end
def troll_face
<<EOS
=============================================================================================================================
.....'',;;::cccllllllllllllcccc:::;;,,,''...'',,'.. |
..';cldkO00KXNNNNXXXKK000OOkkkkkxxxxxddoooddddddxxxxkkkkOO0XXKx:. |
.':ok0KXXXNXK0kxolc:;;,,,,,,,,,,,;;,,,''''''',,''.. .'lOXKd' |
.,lx00Oxl:,'............''''''................... ...,;;'. .oKXd. |
.ckKKkc'...'',:::;,'.........'',;;::::;,'..........'',;;;,'.. .';;'. 'kNKc. |
.:kXXk:. .. .................. .............,:c:'...;:'. .dNNx. |
:0NKd, .....''',,,,''.. ',...........',,,'',,::,...,,. .dNNx. |
.xXd. .:;'.. ..,' .;,. ...,,'';;'. ... .oNNo |
.0K. .;. ;' '; .'...'. .oXX: |
.oNO. . ,. . ..',::ccc:;,.. .. lXX: |
.dNX: ...... ;. 'cxOKK0OXWWWWWWWNX0kc. :KXd. |
.l0N0; ;d0KKKKKXK0ko:... .l0X0xc,...lXWWWWWWWWKO0Kx' ,ONKo. |
.lKNKl...'......'. .dXWN0kkk0NWWWWWN0o. :KN0;. .,cokXWWNNNNWNKkxONK: .,:c:. .';;;;:lk0XXx; |
:KN0l';ll:'. .,:lodxxkO00KXNWWWX000k. oXNx;:okKX0kdl:::;'',;coxkkd, ...'. ...'''.......',:lxKO:. |
oNNk,;c,'',. ...;xNNOc,. ,d0X0xc,. .dOd, ..;dOKXK00000Ox:. ..''dKO, |
'KW0,:,.,:..,oxkkkdl;'. 'KK' .. .dXX0o:'....,:oOXNN0d;.'. ..,lOKd. .. ;KXl. |
;XNd,; ;. l00kxoooxKXKx:..ld: ;KK' .:dkO000000Okxl;. c0; :KK; . ;XXc |
'XXdc. :. .. '' 'kNNNKKKk, .,dKNO. .... .'c0NO' :X0. ,. xN0. |
.kNOc' ,. .00. ..''... .l0X0d;. 'dOkxo;... .;okKXK0KNXx;. .0X: ,. lNX' |
,KKdl .c, .dNK, .;xXWKc. .;:coOXO,,'....... .,lx0XXOo;...oNWNXKk:.'KX; ' dNX. |
:XXkc'.... .dNWXl .';l0NXNKl. ,lxkkkxo' .cK0. ..;lx0XNX0xc. ,0Nx'.','.kXo ., ,KNx. |
cXXd,,;:, .oXWNNKo' .'.. .'.'dKk; .cooollox;.xXXl ..,cdOKXXX00NXc. 'oKWK' ;k: .l. ,0Nk. |
cXNx. . ,KWX0NNNXOl'. .o0Ooldk; .:c;.':lxOKKK0xo:,.. ;XX: .,lOXWWXd. . .':,.lKXd. |
lXNo cXWWWXooNWNXKko;'.. .lk0x; ...,:ldk0KXNNOo:,.. ,OWNOxO0KXXNWNO, ....'l0Xk, |
.dNK. oNWWNo.cXK;;oOXNNXK0kxdolllllooooddxk00KKKK0kdoc:c0No .'ckXWWWNXkc,;kNKl. .,kXXk, |
'KXc .dNWWX;.xNk. .kNO::lodxkOXWN0OkxdlcxNKl,.. oN0'..,:ox0XNWWNNWXo. ,ONO' .o0Xk; |
.ONo oNWWN0xXWK, .oNKc .ONx. ;X0. .:XNKKNNWWWWNKkl;kNk. .cKXo. .ON0; |
.xNd cNWWWWWWWWKOkKNXxl:,'...;0Xo'.....'lXK;...',:lxk0KNWWWWNNKOd:.. lXKclON0: .xNk. |
.dXd ;XWWWWWWWWWWWWWWWWWWNNNNNWWNNNNNNNNNWWNNNNNNWWWWWNXKNNk;.. .dNWWXd. cXO. |
.xXo .ONWNWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWNNK0ko:'..OXo 'l0NXx, :KK, |
.OXc :XNk0NWXKNWWWWWWWWWWWWWWWWWWWWWNNNX00NNx:'.. lXKc. 'lONN0l. .oXK: |
.KX; .dNKoON0;lXNkcld0NXo::cd0NNO:;,,'.. .0Xc lXXo..'l0NNKd,. .c0Nk, |
:XK. .xNX0NKc.cXXl ;KXl .dN0. .0No .xNXOKNXOo,. .l0Xk;. |
.dXk. .lKWN0d::OWK; lXXc .OX: .ONx. . .,cdk0XNXOd;. .'''....;c:'..;xKXx, |
.0No .:dOKNNNWNKOxkXWXo:,,;ONk;,,,,,;c0NXOxxkO0XXNXKOdc,. ..;::,...;lol;..:xKXOl. |
,XX: ..';cldxkOO0KKKXXXXXXXXXXKKKKK00Okxdol:;'.. .';::,..':llc,..'lkKXkc. |
:NX' . '' .................. .,;:;,',;ccc;'..'lkKX0d;. |
lNK. .; ,lc,. ................ ..,,;;;;;;:::,....,lkKX0d:. |
.oN0. .'. .;ccc;,'.... ....'',;;;;;;;;;;'.. .;oOXX0d:. |
.dN0. .;;,.. .... ..''''''''.... .:dOKKko;. |
lNK' ..,;::;;,'......................... .;d0X0kc'. |
.xXO' .;oOK0x:. |
.cKKo. .,:oxkkkxk0K0xc'. JUST KIDDING!!! |
.oKKkc,. .';cok0XNNNX0Oxoc,. |
.;d0XX0kdlc:;,,,',,,;;:clodkO0KK0Okdl:,'.. Give me a smile and press enter to go back |
.,coxO0KXXXXXXXKK0OOxdoc:,.. |
| ... Ruby_Notes_1.1 |
=============================================================================================================================
EOS
end
#----------methods--------------#
def add_user
puts "Please enter a User-Name:"
user_name = gets
return unless user_name
user_name.chomp!
user = User.new(user_name)
if user.save
puts "#{user_name} has been added as a User."
what_now(user_name)
else
puts "Welcome back #{user_name}!"
what_now(user_name)
add_user
end
end
def create_note
puts who_are_you
user = gets.chomp
return unless user
x = User.find_by_name(user)
if x
puts "Welcome back #{x.name}!"
puts create_menu(user)
note_title = gets.chomp
return unless note_title
puts "Please write a tag for your note:"
note_tag = gets.chomp
return unless note_tag
body = "empty"
note = Note.new(note_title, note_tag, user, body)
if note.save
puts "#{note_title} has been created and saved in the database!"
what_now(user)
else
puts note.errors
end
else
puts "I don't know you #{user}!"
puts "To create a new note you have first to register as a user"
get_menu_selection
end
end
def create_note_user(user)
puts "Ok #{user},"
puts create_menu(user)
note_title = gets.chomp
return unless note_title
puts "Please write a tag for your note:"
note_tag = gets.chomp
return unless note_tag
body = "empty"
note = Note.new(note_title, note_tag, user, body)
if note.save
puts "#{note_title} has been created and saved in the database!"
what_now(user)
else
puts note.errors
end
end
def modify_note_user(user)
puts modify_menu(user)
input = gets.chomp
return unless input
if input == "1"
write_note(user)
elsif input == "2"
delete_note(user)
elsif input == "3"
what_now(user)
else
puts "Invalid selection"
modify_note_user(user)
end
end
def modify_note
puts who_are_you
user = gets.chomp
return unless user
x = User.find_by_name(user)
if x
puts "Welcome back #{x.name}!"
puts modify_menu(user)
input = gets.chomp
return unless input
if input == "1"
write_note(user)
elsif input == "2"
delete_note(user)
elsif input == "3"
what_now(user)
else
puts "Invalid selection"
modify_note_user(user)
end
else
puts "I don't know you #{user}!"
puts "To access the modify section you have first to register as a user"
get_menu_selection
end
end
def write_note(user)
puts "What is the title of the Note?"
title = gets.chomp
return unless title
note = Note.find_by_title_user(title, user)
if note
array = [note.title, note.tag, note.user_name, note.body]
print_body(array)
puts "..."
stuff = gets.chomp
return unless stuff
if array[3] == "empty"
new_body = stuff
else
new_body = array[3]+" "+stuff
end
updated_note = Note.update(user, title, new_body)
puts "Your Note '#{title}', has been updated"
what_now(user)
else
puts "Wrong title, try again"
write_note(user)
end
end
def delete_note(user)
puts "What is the title of the note you want to delete?"
note_title = gets.chomp
return unless note_title
note = Note.find_by_title_user(note_title, user)
if note
puts "Warning; if you really want to delete this note write again the title:"
title = gets.chomp
return unless title
if title == note_title
Note.destroy_by_title(user, title)
puts "Note destroyed!"
what_now(user)
else
puts "Wrong title, try again"
what_now(user)
end
else
puts "I didn't find this note; try again"
what_now(user)
end
end
def count
puts who_are_you
user = gets.chomp
x = Note.count_user(user)
puts "There are #{x} notes in the database"
if x < 6
puts "You are not much of a writer, aren't you?"
else
puts "Wow, are you writing a poem or something?"
end
what_now(user)
end
def search_note
puts who_are_you
user = gets.chomp
x = User.find_by_name(user)
if x
puts "Welcome back #{user}!"
puts search_menu
input = gets
return unless input
input.chomp!
if input == "1"
search_by_title(user)
elsif input == "2"
search_by_tag(user)
elsif input == "3"
show_all(user)
else
puts "sorry buddy,'#{input}' is not a valid selection"
search_note_user(user)
end
else
puts "Sorry #{user}, but I don't know you..."
get_menu_selection
end
end
def search_note_user(user)
puts search_menu
input = gets
return unless input
input.chomp!
if input == "1"
search_by_title(user)
elsif input == "2"
search_by_tag(user)
elsif input == "3"
show_all(user)
else
puts "sorry buddy,'#{input}' is not a valid selection"
search_note_user(user)
end
end
def search_by_title(user)
puts "What is the title of the Note you want to search?"
note_title = gets.chomp
return unless note_title
x = Note.find_by_title_user(note_title, user)
if x
puts "Ok #{x.user_name}, here is your note:" +"\n"+ "'#{x.title.capitalize!}'"
array = [x.title, x.tag, x.user_name, x.body]
print_note(array)
what_now(user)
else
puts "Sorry but there is not such Note in the database." +"\n"+ "Try again"
search_by_title(user)
end
end
def search_by_tag(user)
puts "What is the tag you are searching for?"
note_tag = gets.chomp
return unless note_tag
x = Note.find_by_tag(note_tag, user)
if x
puts "Ok #{x[0].user_name}, I found your notes with the tag:#{x[0].tag}"
print_notes(x)
puts "press enter to continue"
input = gets.chomp
return unless input
if input
what_now(user)
end
what_now(user)
else
puts "Sorry but there is not such Note in the database." +"\n"+ "Try again"
search_by_tag(user)
end
end
def show_all(user)
x = Note.find_all(user)
if x != []
puts "I found all your notes"
print_notes(x)
puts "press enter to continue"
input = gets.chomp
return unless input
if input
what_now(user)
end
what_now(user)
else
puts "You have no notes in the database"
puts "press enter to continue"
input = gets.chomp
return unless input
what_now(user)
end
end
def get_menu_selection
puts menu
input = gets
return unless input
input.chomp!
if input == "1"
add_user
elsif input == "2"
create_note
elsif input == "3"
modify_note
elsif input == "4"
search_note
elsif input == "5"
count
else
puts "Sorry buddy, '#{input}' is not a valid selection."
get_menu_selection
end
end
def what_now(user)
puts what_now_menu(user)
input = gets.chomp
return unless input
if input == "1"
create_note_user(user)
elsif input == "2"
modify_note_user(user)
elsif input == "3"
search_note_user(user)
elsif input == "4"
troll(user)
elsif input == "5"
password(user)
elsif input == "6"
quit(user)
else
puts "'#{input}' is not a valid selection."
what_now(user)
end
end
def troll(user)
puts troll_face
input = gets.chomp
return unless input
if input
what_now(user)
end
end
def quit(user)
puts quit_menu
input = gets.chomp
return unless input
if input == "y"
puts "Ok, you are the boss....bye!"
exit
elsif input == "n"
puts "Good choice #{user}:-)"
what_now(user)
else
puts "Yes or no buddy..."
quit(user)
end
end
get_menu_selection