-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtask.rb
More file actions
189 lines (165 loc) · 3.99 KB
/
Copy pathtask.rb
File metadata and controls
189 lines (165 loc) · 3.99 KB
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
# require 'faker' #if you want to test code with random-generated tasks
require 'sqlite3'
require 'date'
require_relative 'service/priority_algorithm'
require 'fileutils'
FileUtils::mkdir_p(Dir.home + '/.taskki')
DB = SQLite3::Database.new(Dir.home + '/.taskki/tasks.db')
class Task
attr_reader :title, :takes, :reoccur, :interval
attr_accessor :id, :due, :score, :numbering, :done, :top_priority
def initialize(infos = {})
Task.create_db
@id = infos['id']
@title = infos['title'] || infos[:title]
# default should be today
@due = infos['due']
@takes = infos['takes'] || 0
@top_priority = (infos['top_priority'] == 1 ? true : false) || false
@reoccur = (infos['reoccur'] == 1 ? true : false) || false
@interval = infos['interval'] || 0
@done = (infos['done'] == 1 ? true : false) || false
end
def self.today
return Task.all.today
end
def self.week
return Task.all.week
end
def self.longterm
return Task.all.longterm
end
def self.done_list
return Task.all.done
end
def self.all
DB.results_as_hash = true
result = DB.execute("SELECT * FROM tasks")
instance_arr = []
result.each do |infos|
instance_arr << Task.new(infos)
end
algo = PriorityAlgorithm.new(instance_arr)
return algo
end
def add
if @due
@due = Date.parse(@due).to_s
else
due_date = Date.today
if @takes
@due = (due_date + @takes).to_s
else
@due = due_date.to_s
end
end
save
end
def self.find(id)
DB.results_as_hash = true
result = DB.execute("SELECT * from tasks WHERE id = ?", id).first
return Task.new(result)
end
def edit_title(value)
@title = value
save
end
def edit_due(value)
@due = Date.parse(value).to_s
save
end
def edit_takes(value)
@takes = value
save
end
def edit_top_priority
@top_priority = !@top_priority
puts "Top priority toggled"
puts ""
save
end
def edit_repeat(value)
@reoccur = true
@interval = value
save
end
def self.done(task)
if task.reoccur
intv = task.interval
new_due = (Date.today + intv).to_s
task.due = new_due
puts "Reoccurring task: every #{intv} days, NEW due: #{new_due}"
if task.top_priority
puts "keep top_priority? [y/n]"
task.top_priority = STDIN.gets.chomp == 'y'
end
else
task.done = true
task.top_priority = false
end
task.save
end
def self.revive(id)
task = Task.find(id)
task.done = false
task.save
end
def save
@id ? update : insert
end
def destroy
DB.execute("DELETE FROM tasks WHERE id = #{@id}")
end
def self.create_db
st = "
CREATE TABLE IF NOT EXISTS`tasks` (
`id` INTEGER PRIMARY KEY AUTOINCREMENT,
`title` TEXT,
`due` TEXT,
`takes` INTEGER,
`top_priority` BOOLEAN,
`reoccur` BOOLEAN,
`interval` INTEGER,
`done` BOOLEAN
);"
DB.execute(st)
end
private
def insert
pr = @top_priority ? 1 : 0
ro = @reoccur ? 1 : 0
dn = @done ? 1 : 0
st = "
INSERT INTO tasks (title, due, takes, top_priority, reoccur, interval, done)
VALUES (?, ?, ?, ?, ?, ?, ?)
"
DB.execute(st, self.title, self.due, self.takes, pr, ro, self.interval, dn)
self.id = DB.last_insert_row_id
end
def update
pr = @top_priority ? 1 : 0
ro = @reoccur ? 1 : 0
dn = @done ? 1 : 0
st = "
UPDATE tasks
SET title = ?, due = ?, takes = ?, top_priority = ?, reoccur =?,
interval = ?, done = ?
WHERE id = #{@id}
"
DB.execute(st, self.title, self.due, self.takes, pr, ro, self.interval, dn)
end
# def self.seed
# puts 'Creating 30 fake tasks...'
# 30.times do
# task = Task.new(
# title: Faker::Lorem.sentence,
# due: Faker::Date.forward(100).to_s,
# takes: (0..5).to_a.sample,
# top_priority: [true, false].sample,
# reoccur: [true, false].sample
# )
# task.save
# end
# puts 'Finished!'
# end
end