Skip to content

Instantly share code, notes, and snippets.

@steven-ferguson
Created September 4, 2013 00:54
Show Gist options
  • Save steven-ferguson/6431541 to your computer and use it in GitHub Desktop.
Save steven-ferguson/6431541 to your computer and use it in GitHub Desktop.
require 'date'
class Task
def initialize(description)
@description = description
@status = "incomplete"
@priority = 5
@due_date = Date.today.to_s
end
def description
@description
end
def mark_as_complete
@status = "complete"
end
def status
@status
end
def priority
@priority
end
def edit_priority(level)
@priority = level
end
def due_date
@due_date
end
def edit_due_date(year, month, day)
@due_date = "#{year}-#{month}-#{day}"
end
def complete?
@status == 'complete'
end
end
class List
def initialize(name)
@name = name
@tasks = []
end
def name
@name
end
def list_tasks
@tasks
end
def add_task(task)
@tasks << task
end
def delete(task)
@tasks.delete_if {|x| x == task}
end
def sort_by_priority
@tasks.sort! do |task, next_task|
task.priority <=> next_task.priority
end
end
def sort_by_due_date
@tasks.sort! do |task, next_task|
task.due_date <=> next_task.due_date
end
end
end
require './lib/to_do'
require 'date'
@lists = []
def main_menu
puts "Enter 'cl' to create a new list"
puts "Enter 'v' to view the names for each of your lists"
puts "Enter 's' to select a list to edit"
puts "Enter 'x' to exit"
main_choice = gets.chomp
if main_choice == 'cl'
new_list
elsif main_choice == 'v'
display_list_names
main_menu
elsif main_choice == 's'
select_list
main_menu
elsif main_choice == 'x'
puts "Good-bye!"
else
puts "That is not a valid input."
main_menu
end
end
def new_list
puts "Please enter a name for your list"
list_name = gets.chomp
@lists << List.new(list_name)
puts "New list created."
main_menu
end
def display_list_names
@lists.each_with_index do |list, index|
puts "#{index + 1}. #{list.name}"
end
end
def select_list
display_list_names
puts "Enter the number of the list you would like to edit"
index_of_list_to_edit = gets.to_i - 1
@list = @lists[index_of_list_to_edit]
puts "\n\n#{@list.name} has been selected"
list_menu
end
def list_menu
puts "Enter 'a' to add a task or 'l' to list all of your tasks."
puts "Enter 'c' to mark task as complete"
puts "Enter 'p' to set priority for the task"
puts "Enter 'd' to set the due date for the task"
puts "Press 'x' return to the main menu."
main_choice = gets.chomp
if main_choice == 'a'
add_task
list_menu
elsif main_choice == 'l'
list_tasks
list_menu
elsif main_choice == 'x'
main_menu
elsif main_choice == 'c'
complete_task
list_menu
elsif main_choice == 'p'
task_priority
list_menu
elsif main_choice == 'd'
task_due_date
list_menu
else
puts "That is not a valid input."
list_menu
end
end
def add_task
puts "Enter a description"
description = gets.chomp
@list.add_task(Task.new(description))
end
def list_tasks
@list.sort_by_priority
@list.sort_by_due_date
@list.list_tasks.each_with_index do |task, index|
puts "#{index + 1}. #{task.description} - Status: #{task.status} - Priority: #{task.priority} - Due: #{task.due_date}"
end
puts "\n\n"
end
def complete_task
list_tasks
puts "Please enter the number of the task you wish to set to complete"
task_choice_index = gets.to_i - 1
tasks = @list.list_tasks
if task_choice_index < 0 || task_choice_index > tasks.length
puts "That is not a task number."
else
selected_task = tasks[task_choice_index]
selected_task.mark_as_complete
puts "#{selected_task.description} has been marked complete \n\n"
end
end
def task_priority
list_tasks
puts "Please enter the number of the task you wish set priority"
task_choice = gets.to_i - 1
tasks = @list.list_tasks
if task_choice < 0 || task_choice > tasks.length
puts "That is not a task number."
else
selected_task = tasks[task_choice]
puts "#{selected_task.description} has been selected"
puts "Please enter the priority level from 1-5 (5 being the lowest)"
priority = gets.to_i
selected_task.edit_priority(priority)
puts "#{selected_task.description} priority has been set at #{priority} \n\n"
end
end
def task_due_date
list_tasks
puts "Please enter the number of the task you wish set the due date"
task_choice = gets.to_i - 1
tasks = @list.list_tasks
if task_choice < 0 || task_choice > tasks.length
puts "That is not a task number."
else
selected_task = tasks[task_choice]
puts "Please enter the year"
year = gets.to_i
puts "Please enter the month"
month = gets.to_i
puts "Please enter the day"
day = gets.to_i
selected_task.edit_due_date(year, month, day)
puts "Task due date set to #{selected_task.due_date}\n\n"
end
end
main_menu
require 'rspec'
require 'to_do'
require 'date'
describe Task do
it 'is initialized with a description' do
test_task = Task.new('walk the dog')
test_task.should be_an_instance_of Task
end
it 'lets you read the description out' do
test_task = Task.new('walk the dog')
test_task.description.should eq 'walk the dog'
end
it 'lets you mark a task as complete' do
test_task = Task.new('walk the dog')
test_task.mark_as_complete # complete!
test_task.status.should eq 'complete'
end
describe 'complete?' do
it "returns true if the task is completed" do
test_task = Task.new("walk the dog")
test_task.mark_as_complete
test_task.complete?.should eq true
end
end
describe 'priority' do
it 'lets you read the task current priority level' do
test_task = Task.new("walk the dog")
test_task.priority.should eq 5
end
end
describe 'edit_priority' do
it 'sets the priority number for task' do
test_task = Task.new("walk the dog")
test_task.edit_priority(1)
test_task.priority.should eq 1
end
end
describe 'due_date' do
it 'lets you read the due date for the task' do
test_task = Task.new("Walk the dog")
test_task.due_date.should eq Date.today.to_s
end
it 'lets you change the due date for the task' do
test_task = Task.new("Walk the dog")
test_task.edit_due_date(2013, 10, 4)
test_task.due_date.should eq "2013-10-4"
end
end
end
describe List do
it 'is intialized with a name' do
test_list = List.new("list1")
test_list.should be_an_instance_of List
end
it "lets you read the list of tasks" do
test_list = List.new("list")
test_list.list_tasks.should eq [] # tasks
end
it 'add a task to the list' do
test_list = List.new("List1")
task = Task.new("Walk the dog")
test_list.add_task(task)
list = test_list.list_tasks.should eq [task]
end
it "lets you sort by priority level" do
list = List.new("list")
task = Task.new("Walk the dog")
task2 = Task.new("Wash the car")
task2.edit_priority(1)
list.add_task(task)
list.add_task(task2)
list.sort_by_priority
list.list_tasks.should eq [task2, task]
end
it "lets you sort by due date" do
list = List.new("list")
task = Task.new("Walk the dog")
task2 = Task.new("Wash the car")
task.edit_due_date(2013, 11, 04)
list.add_task(task)
list.add_task(task2)
list.sort_by_due_date
list.list_tasks.should eq [task2, task]
end
it 'lets you delete a task' do
list = List.new("List")
task = Task.new("walk the dog")
list.add_task(task)
list.delete(task)
list.list_tasks.should eq []
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment