Skip to content

Instantly share code, notes, and snippets.

@steven-ferguson
Created September 10, 2013 00:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save steven-ferguson/6503617 to your computer and use it in GitHub Desktop.
Save steven-ferguson/6503617 to your computer and use it in GitHub Desktop.
class List
attr_reader :name, :id
def initialize(name)
@name = name
end
def ==(another_list)
self.name == another_list.name
end
def self.all
results = DB.exec("SELECT * FROM lists;")
lists = []
results.each do |result|
name = result['name']
id = result['id'].to_i
lists << List.new(name)
lists.last.set_id(id)
end
lists
end
def set_id(number)
@id = number
end
def save
results = DB.exec("INSERT INTO lists (name) VALUES ('#{@name}') RETURNING id;")
@id = results.first['id'].to_i
end
def delete
DB.exec("DELETE FROM lists WHERE name = '#{@name}' AND id = #{@id};")
end
end
require 'spec_helper'
describe List do
it 'is initialized with a name' do
list = List.new('Epicodus Stuff')
list.should be_an_instance_of List
end
it 'tells you its name' do
list = List.new('Epicodus Stuff')
list.name.should eq 'Epicodus Stuff'
end
it 'is the same list if it has the same name' do
list1 = List.new("list")
list2 = List.new("list")
list1.should eq list2
end
it 'starts off with no lists' do
List.all.should eq []
end
it 'lets you save a list to the database' do
list = List.new('learn SQL')
list.save
List.all.should eq [list]
end
it 'sets its ID when you save it' do
list = List.new('learn SQL')
list.save
list.id.should be_an_instance_of Fixnum
end
it 'lets you delete a list' do
list = List.new('learn SQL')
list.save
list.delete
List.all.should eq []
end
it 'lists you set its id' do
list = List.new('learn SQL')
list.set_id(1)
list.id.should eq 1
end
end
require 'rspec'
require 'pg'
require 'task'
require 'list'
DB = PG.connect(:dbname => 'to_do_test')
RSpec.configure do |config|
config.after(:each) do
DB.exec("DELETE FROM tasks *;")
DB.exec("DELETE FROM lists *;")
end
end
class Task
attr_reader :name, :list_id, :status, :due_date
def initialize(name, list_id, due_date='0001-01-01')
@name = name
@list_id = list_id
@status = 'incomplete'
@due_date = due_date
end
def mark_complete
@status = 'complete'
DB.exec("UPDATE tasks SET status = '#{@status}' WHERE name = '#{@name}' AND list_id = '#{@list_id}';")
end
def completed?
@status == 'complete'
end
def set_status(current_status)
@status = current_status
end
def self.all
results = DB.exec("SELECT * FROM tasks;")
tasks = []
results.each do |result|
name = result['name']
list_id = result['list_id'].to_i
status = result['status']
due_date = result['due_date']
tasks << Task.new(name, list_id, due_date)
tasks.last.set_status(status)
tasks.last.set_due_date(due_date)
end
tasks
end
def self.all_from_list(list_id)
results = DB.exec("SELECT * FROM tasks WHERE list_id = #{list_id};")
tasks = []
results.each do |result|
name = result['name']
status = result['status']
list_id = result['list_id'].to_i
due_date = result['due_date']
tasks << Task.new(name, list_id)
tasks.last.set_status(status)
tasks.last.set_due_date(due_date)
end
tasks
end
def self.all_from_list_sorted(list_id)
results = DB.exec("SELECT * FROM tasks WHERE list_id = #{list_id} ORDER BY due_date;")
tasks = []
results.each do |result|
name = result['name']
status = result['status']
list_id = result['list_id'].to_i
due_date = result['due_date']
tasks << Task.new(name, list_id)
tasks.last.set_status(status)
tasks.last.set_due_date(due_date)
end
tasks
end
def save
DB.exec("INSERT INTO tasks (name, list_id, status, due_date) VALUES ('#{@name}', #{@list_id}, '#{@status}', '#{@due_date}');")
end
def delete
DB.exec("DELETE FROM tasks WHERE name = '#{@name}' AND list_id = #{@list_id};")
end
def ==(another_task)
self.name == another_task.name && self.list_id == another_task.list_id
end
def set_due_date(date)
@due_date = date
end
def edit_due_date(due_date)
DB.exec("UPDATE tasks SET due_date = '#{due_date}' WHERE name = '#{@name}' AND list_id = '#{@list_id}';")
end
def edit_name(new_name)
DB.exec("UPDATE tasks SET name = '#{new_name}' WHERE name = '#{@name}' AND list_id = '#{@list_id}';")
end
def edit_status(new_status)
DB.exec("UPDATE tasks SET status = '#{new_status}' WHERE name = '#{@name}' AND list_id = '#{@list_id}';")
end
end
require 'spec_helper'
describe Task do
it 'is intialized with a name and a list ID' do
task = Task.new('learn SQL', 1)
task.should be_an_instance_of Task
end
it 'can be initialized with a due date' do
task = Task.new('learn SQL', 1, '2013-12-12')
task.should be_an_instance_of Task
end
it 'tells you its name' do
task = Task.new('learn SQL', 1)
task.name.should eq 'learn SQL'
end
it 'tells you its list ID' do
task = Task.new('learn SQL', 1)
task.list_id.should eq 1
end
it 'starts off with no tasks' do
Task.all.should eq []
end
it 'lets you give it a due date' do
task = Task.new('learn SQL', 1)
task.save
task.edit_due_date('2013-09-09')
Task.all.first.due_date.should eq ('2013-09-09')
end
it 'saves the due date to the database' do
task = Task.new('learn SQL', 1)
task.save
task.edit_due_date('2013-09-09')
Task.all.first.due_date.should eq '2013-09-09'
end
it 'saves the due date if it is initialized with a due date' do
task = Task.new('learn SQL', 1, '2013-09-09')
task.save
Task.all.first.due_date.should eq '2013-09-09'
end
it 'saves even without a due date' do
task = Task.new('stuff', 1)
expect {task.save}.not_to raise_error
end
it 'lets you save tasks to the database' do
task = Task.new('learn SQL', 1)
task.save
Task.all.should eq [task]
end
it 'lets you delete tasks from the database' do
task = Task.new('learn SQL', 1)
task.save
task.delete
Task.all.should eq []
end
it 'is the same task if it has the same name and list_id' do
task1 = Task.new('learn SQL', 1)
task2 = Task.new('learn SQL', 2)
task1.should_not eq task2
end
it 'lets you get all tasks in a list' do
task1 = Task.new('learn SQL', 1)
task1.save
task2 = Task.new('learn Ruby', 2)
task3 = Task.new('learn Rails', 1)
task2.save
task3.save
Task.all_from_list(1).should eq [task1, task3]
end
it 'lets you mark a task completed' do
task = Task.new('to do in pSQL', 1)
task.mark_complete
task.save
results = Task.all
results.first.status.should eq 'complete'
end
it 'lets you set its status' do
task = Task.new('to do in pSQL', 1)
task.set_status('complete')
task.completed?.should eq true
end
it 'lets you sort all the tasks in a list by their due date' do
task = Task.new('SQL', 1, '2013-09-09')
task2 = Task.new('Ruby', 1, '2013-08-01')
task.save
task2.save
Task.all_from_list_sorted(1).should eq [task2, task]
end
it 'lets you update the task name' do
task = Task.new('Learn Ruby', 1)
task.save
task.edit_name('Learn Ruby on Rails')
Task.all.first.name.should eq 'Learn Ruby on Rails'
end
it 'lets you update the status' do
task = Task.new('Learn Ruby', 1)
task.save
task.mark_complete
task.edit_status('incomplete')
Task.all.first.status.should eq 'incomplete'
end
end
require 'pg'
require './lib/task'
require './lib/list'
DB = PG.connect(:dbname => 'to_do')
def welcome
puts "Welcome to the To Do List!"
list_menu
end
def list_menu
puts "Press 'l' to see all lists"
puts "Press 'a' to create a new list"
puts "Press 'v' to view a list"
puts "Press 'd' to delete a list"
puts "Press 'e' to edit a list"
puts "Press 'x' to exit"
user_choice = gets.chomp
if user_choice == 'l'
view_lists
list_menu
elsif user_choice == 'a'
add_list
elsif user_choice == 'v'
view_a_list
elsif user_choice == 'd'
delete_list
elsif user_choice == 'e'
edit_list
elsif user_choice == 'x'
puts "Good Bye"
else
puts "I did not understand that."
list_menu
end
end
def add_list
puts "Name you new list:"
list = List.new(gets.chomp)
list.save
puts "List added.\n\n"
task_menu(list.id)
end
def view_lists
lists = List.all
puts
lists.each_with_index do |list, index|
puts "#{index + 1}. #{list.name}"
end
puts
end
def delete_list
view_lists
puts "Enter the number of the list you would like to delete"
lists = List.all
lists[gets.to_i - 1].delete
puts "List has been deleted.\n\n"
list_menu
end
def edit_list
view_lists
puts "Enter the number of the list you would like to edit"
lists = List.all
list_id = gets.to_i - 1
puts "Selected list, #{lists[list_id].name}\n\n"
task_menu(list_id)
end
def task_menu(list_id)
puts "Press 'a' add a new task"
puts "Press 'c' to view all the completed tasks"
puts "Press 'm' to mark a task as complete"
puts "Press 'e' to edit the task"
puts "Press 'v' to view all tasks in the list"
puts "Press 'd' to delete a task"
puts "Press 'x' to go back to the main menu"
user_choice = gets.chomp
if user_choice == 'a'
add_task(list_id)
elsif user_choice == 'c'
view_completed_tasks(list_id)
elsif user_choice == 'v'
view_tasks(list_id)
task_menu(list_id)
elsif user_choice == 'd'
delete_task(list_id)
elsif user_choice == 'e'
edit_task(list_id)
elsif user_choice == 'm'
complete_task(list_id)
elsif user_choice == 'x'
puts "Good Bye!"
list_menu
else
puts "I did not understand that."
task_menu(list_id)
end
end
def add_task(list_id)
puts "Describe the task:"
task_name = gets.chomp
puts "\nDo you want to add a due date? (y/n):"
add_due = gets.chomp
if add_due.downcase == 'y'
puts "Insert due date (yyyy-mm-dd):"
due_date = gets.chomp
task = Task.new(task_name, list_id, due_date)
else
task = Task.new(task_name, list_id)
end
task.save
puts "Task added.\n"
task_menu(list_id)
end
def view_completed_tasks(list_id)
puts "Here are your completed tasks:"
tasks = Task.all_from_list(list_id)
counter = 1
tasks.each do |task|
if task.completed?
puts "#{counter}. #{task.name}"
counter += 1
end
end
puts
task_menu(list_id)
end
def complete_task(list_id)
view_tasks(list_id)
puts "Enter the number of the task you would like to mark as complete"
tasks = Task.all_from_list(list_id)
tasks[gets.to_i - 1].mark_complete
puts 'Task has been marked as complete.'
puts
task_menu(list_id)
end
def view_tasks(list_id)
puts "Enter 'd' to sort list by due date"
user_choice = gets.chomp.downcase
if user_choice == 'd'
tasks = Task.all_from_list_sorted(list_id)
else
tasks = Task.all_from_list(list_id)
end
puts "\n"
tasks.each_with_index do |task, index|
puts "#{index + 1}. #{task.name} - #{task.status} - #{task.due_date}"
end
puts "\n"
end
def delete_task(list_id)
view_tasks(list_id)
puts "Enter the number of the task you would like to delete"
tasks = Task.all_from_list(list_id)
tasks[gets.to_i - 1].delete
puts "Task has been deleted.\n\n"
task_menu(list_id)
end
def edit_task(list_id)
view_tasks(list_id)
puts "Enter the number of the task you would like to edit"
tasks = Task.all_from_list(list_id)
selected_task = tasks[gets.to_i - 1]
puts "#{selected_task.name} - #{selected_task.status} - #{selected_task.due_date}"
puts "Enter 1 if you want to change the name/description"
puts "Enter 2 if you want to change the status"
puts "Enter 3 if you want to change the due date"
puts "Enter 4 to go back"
user_choice = gets.to_i
if user_choice == 1
puts "Enter a new name/description:"
selected_task.edit_name(gets.chomp)
puts "Update Complete."
elsif user_choice == 2
puts "Enter the new status of the task (complete/incomplete):"
selected_task.edit_status(gets.chomp)
puts "Update Complete."
elsif user_choice == 3
puts "Enter the new due date (YYYY-MM-DD)"
selected_task.edit_due_date(gets.chomp)
puts "Update Complete."
elsif user_choice == 4
task_menu(list_id)
else
puts "I did not understand that choice."
end
task_menu(list_id)
end
welcome
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment