Skip to content

Instantly share code, notes, and snippets.

@codesliced
Forked from dbc-challenges/todo.csv
Last active December 17, 2015 17:59
Show Gist options
  • Save codesliced/32f4baf08efc8c69cb15 to your computer and use it in GitHub Desktop.
Save codesliced/32f4baf08efc8c69cb15 to your computer and use it in GitHub Desktop.
class DisplayTodos
def print_task_list(list)
list.tasks.each do |list_item|
puts "#{list_item.id}. #{list_item.description} #{list_item.is_complete ? "Completed: Yes" : "Completed: No"}."
end
end
def start_program
puts "Type the filename of the list you want to load."
end
def option_list
puts "Options: Type 'list' to see your to do list,
'add' to add a task,
'delete' to delete a task,
'complete' to mark a task completed,
'q' to exit. "
end
def add_item_prompt
puts "What do you want to add?"
end
def delete_item_prompt
puts "What numbered item do you want to delete?"
end
def mark_complete_prompt
puts "What numbered item did you complete today?"
end
end
We can make this file beautiful and searchable if this error is corrected: No commas found in this CSV file in line 0.
Move with Lil to the black mountain hills of Dakota
Lose Lil to Danny
Get hit in the eye by Danny
Walk into town seeking revenge
Book room at local saloon
Check into room and read Gideon's bible
Drink too much gin
Overhear Lil and Danny in neighboring room
Burst into neighboring room and declare a showdown
Get shot by Danny and collapse in corner
Visit doctor
Return to room and read Gideon's bible
Sing along! D'do d'do d'do do do d'do d'do d'do
require_relative 'todo_model'
require_relative 'display'
class ToDoController
def initialize
@list = ToDoList.new
@list_view = DisplayTodos.new
load_list
task_manager
end
def load_list
@list_view.start_program
load_list = gets.chomp
if load_list.end_with? ".txt"
@list.convert_to_csv(load_list)
elsif load_list.end_with? ".csv"
@list.load_csv('new_todo.csv')
end
@list_view.print_task_list(@list)
end
def task_manager
begin
@list_view.option_list
response = gets.chomp
if response == 'add'
@list_view.add_item_prompt
add_task = gets.chomp
@list.add_new(add_task)
elsif response == 'delete'
@list_view.delete_item_prompt
delete_task_id = gets.chomp
@list.delete_task(delete_task_id.to_i)
elsif response == 'complete'
@list_view.mark_complete_prompt
mark_id_complete = gets.chomp
@list.complete_task(mark_id_complete.to_i)
end
@list_view.print_task_list(@list)
end until response == "q"
end
end
ToDoController.new
require 'pry'
require 'csv'
class Task
attr_accessor :id, :description, :is_complete
def initialize(id, description, is_complete = false)
@id = id
@description = description
@is_complete = is_complete
end
def is_complete?
@is_complete
end
end
class ToDoList
def initialize
@all_tasks = []
end
def add_new(task_name, complete = false)
@all_tasks << Task.new(@all_tasks.length + 1, task_name, complete)
end
def tasks
@all_tasks
end
def delete_task(task_id)
@all_tasks.delete_if { |task| task.id == task_id }
end
def complete_task(task_id)
@all_tasks.each { |task| task.is_complete = true if task.id == task_id}
end
def load_txt(filename)
File.open(filename).each_line { |line| add_new(line.chomp)}
end
def save_as_csv(filename)
CSV.open(filename, "wb") do |csv|
@all_tasks.each do |task|
csv << [task.id, task.description, task.is_complete]
end
end
end
def convert_to_csv(filename)
self.load_txt(filename)
self.save_as_csv('new_todo.csv')
self.load_csv('new_todo.csv')
end
def load_csv(filename)
CSV.foreach(filename) do |row|
add_new(row[1], (row[2] == "true") )
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment