Module: Todo::Tasks

Extended by:
Tasks
Included in:
Tasks
Defined in:
lib/to-do/tasks.rb

Overview

The module that contains methods for manipulating the database

Instance Method Summary collapse

Instance Method Details

#add(task, priority = 1) ⇒ Object

Adds the tast to the list

Parameters:

  • task (String)

    the task to add to the list

  • priority (Integer) (defaults to: 1)

    the priority of the task



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/to-do/tasks.rb', line 11

def add task, priority=1
	list = Helpers::DATABASE[:Lists].select(:Total, :Id)[:Name=>Todo::Config[:working_list_name]]
	if !list
		Helpers::DATABASE[:Lists] << {:Name => Config[:working_list_name], :Total => 0}
		list = Helpers::DATABASE[:Lists].select(:Total, :Id)[:Name=>Todo::Config[:working_list_name]]
	end
	count = list[:Total]+1
	Helpers::DATABASE[:Tasks] << {:Task_number => count, :Name => task, :Completed => 0, :Priority => priority}
	list_id = list[:Id]
	task_id = Helpers::DATABASE[:Tasks].with_sql("SELECT last_insert_rowid() FROM Tasks")
	Helpers::DATABASE[:Task_list] << {:Task_id => task_id, :List_id => list_id}
	Helpers::DATABASE[:Lists].filter(:Id => list_id).update(:Total => count)
end

#clear(clear_all, list_name = Config[:working_list_name]) ⇒ Object

clears all the tasks in the list

and resets the count. if false, just clears the completed tasks

Parameters:

  • clear_all (Bool)

    if true, clears all completed and uncompleted tasks



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/to-do/tasks.rb', line 65

def clear clear_all, list_name = Config[:working_list_name]
	clear_each 1, list_name
	if clear_all
		clear_each 0, list_name
		#Helpers::DATABASE[:Lists].filter(:Name => list_name).update(:Total => 0)
		Helpers::DATABASE[:Lists].filter(:Name => list_name).delete
		puts "Cleared all tasks in #{list_name}"
	else
		puts "Cleared completed tasks in #{Config[:working_list_name]}"
	end
end

#clear_each(completed, list_name) ⇒ Object

clears either just the completed or the uncompleted tasks

Uncompleted tasks

Parameters:

  • completed (Integer)

    1 if clearing completed tasks, 0 if clearing



51
52
53
54
55
56
57
58
59
# File 'lib/to-do/tasks.rb', line 51

def clear_each completed ,list_name
	tasks = Helpers::DATABASE[:Tasks].join(:Task_list, :Tasks__id => :Task_list__Task_id).join(
		:Lists, :Lists__id => :Task_list__List_id).select(:Tasks__Id).filter(
		:Lists__Name => Config[:working_list_name]).filter(:Tasks__Completed => completed)
	tasks.each do |task|
		Helpers::DATABASE[:Task_list].filter(:Task_id => task[:Id]).delete
		Helpers::DATABASE[:Tasks].filter(:Id => task[:Id]).delete
	end
end

#finish(task, is_num) ⇒ Object

finish the task. task is either a case insensitive task on the list or the task number. Prints out either the task is not in the list or that i succesfully finished the task

false if it is the task name

Parameters:

  • task

    either a task number or task name to finish

  • is_num (Bool)

    if the task param represents the task number, true.



32
33
34
# File 'lib/to-do/tasks.rb', line 32

def finish task, is_num
	finish_undo task, is_num, 0, 1
end

#finish_undo(task, is_num, initial, final) ⇒ Object

Helper method for finishing and undoing a task

false if it is the task name

Parameters:

  • task

    either a task number or task name to finish

  • is_num (Bool)

    if the task param represents the task number, true.

  • initial (Integer)

    0 if you are finishing a task, 1 if you are undoing a task

  • final (Integer)

    1 if you are finishing a task, 0 if you ara undoing a task



84
85
86
87
# File 'lib/to-do/tasks.rb', line 84

def finish_undo task , is_num, initial, final
	names =Helpers::task_names.filter(:Tasks__Completed => initial)
	Helpers::Tasks::update_task is_num, names, task, :Completed, final
end

#set_priority(priority, task, is_num) ⇒ Object

Sets the priority of a task

false if it is the task name

Parameters:

  • priority (Integer)

    the new priority

  • task

    either a task number or a task name to change the priority of

  • is_num (Bool)

    if the task param represents the task number, true.



95
96
97
98
# File 'lib/to-do/tasks.rb', line 95

def set_priority priority, task, is_num
	names =Helpers::Tasks::task_names
	Helpers::Tasks::update_task is_num, names, task, :Priority, priority
end

#undo(task, is_num) ⇒ Object

undos finishing a task. task is either a case insensitive task on the list or the task number. Prints out either the task is not in the list or that i succesfully undoed finished the task

false if it is the task name

Parameters:

  • task

    either a task number or task name to finish

  • is_num (Bool)

    if the task param represents the task number, true.



43
44
45
# File 'lib/to-do/tasks.rb', line 43

def undo task, is_num
	finish_undo task, is_num, 1, 0
end