Class: Calamity::DataAccess

Inherits:
Object
  • Object
show all
Defined in:
lib/calamity_data_access.rb

Instance Method Summary collapse

Constructor Details

#initialize(db_location) ⇒ DataAccess

Returns a new instance of DataAccess.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/calamity_data_access.rb', line 6

def initialize db_location
  @db = SQLite3::Database.new db_location
  begin
    @db.execute <<-SQL
      create table tasks (
        name varchar(20),
        context varchar(20),
        project varchar(20),
        status varchar(10)
      );
    SQL
  rescue
  end
end

Instance Method Details

#add_task(task) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/calamity_data_access.rb', line 21

def add_task task
  @db.execute("select * from tasks where name = ?", [task.name]) do |row|
    @db.execute "update tasks set context = ?, project = ? where name = ?", [((task.context) ? task.context : row[1]), (task.project ? task.project : row[2]), task.name]
    return
  end
  @db.execute "insert into tasks values (?,?,?,?)", [task.name, task.context, task.project, 'created']  
end

#list_tasksObject



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/calamity_data_access.rb', line 29

def list_tasks
  tasks = []
  @db.execute("select * from tasks where status != 'finished'") do |row|
    task = Task.new
    task.name = row[0]  
    task.context = row[1]
    task.project = row[2]
    task.status = row[3]
    tasks << task
  end
  tasks
end

#mark_finished(task) ⇒ Object



42
43
44
# File 'lib/calamity_data_access.rb', line 42

def mark_finished task
  @db.execute "update tasks set status = ? where name = ?", ['finished', task.name]
end