-
Notifications
You must be signed in to change notification settings - Fork 1
/
todo.rb
68 lines (57 loc) · 1.15 KB
/
todo.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
require_relative 'config/application'
require_relative 'app/models/task'
def list
Task.all.each do |x|
puts "#{x.count} #{x.name} #{x.completed == false ? "[ ]" : "[x]"}"
end
end
def delete(count)
Task.find(count).destroy
recount
list
end
def add(task)
Task.create!(name: task)
recount
list
end
def done(count)
Task.find(convert_count_to_id(count)).update(:completed => true)
list
end
def cancel_done(count)
Task.find(convert_count_to_id(count)).update(:completed => false)
list
end
def recount
c = 1
Task.all.each do |x|
x.update(:count => c)
c += 1
end
end
def convert_count_to_id(count)
id_count = 0
Task.select(:id).distinct.where(:count => count).each do |y|
id_count = y.id
end
id_count
end
input = ARGV[0]
input2 = ARGV[1..-1]
if input == "list"
list
elsif input == "delete"
delete(input2[0].to_i)
#puts input
elsif input == "add"
input3 = input2.join(" ")
add(input3)
elsif input == "done"
done(input2[0].to_i)
elsif input == "cancel"
cancel_done(input2[0].to_i)
else
puts "To-do List version 1.0"
puts "commands avaliable are list, done (id), cancel (id), delete (id), add (task) "
end