Skip to content

Instantly share code, notes, and snippets.

@angiebui
Forked from dbc-challenges/todo.csv
Last active December 16, 2015 02:49
Show Gist options
  • Save angiebui/a479c5c08a64e69cbb10 to your computer and use it in GitHub Desktop.
Save angiebui/a479c5c08a64e69cbb10 to your computer and use it in GitHub Desktop.
We can make this file beautiful and searchable if this error is corrected: No commas found in this CSV file in line 0.
Cook dinner
Work out in the morning
Grocery shopping at Trader Joes
Eat macarons
Watch Game of Thrones
Sleep more tonight
Read POODR
Call parents
# Will refactor later!
require 'csv'
require 'english'
class Task
attr_reader :text, :row_number
attr_accessor :complete
def initialize(row_number, text)
@text = text[0]
@row_number = row_number
@complete = text[1] || "" # Thanks, Robby! :)
end
def to_s
"#{row_number}. #{text} [#{complete}]"
end
end
class ToDo
attr_accessor :list, :file, :add
def initialize(file)
@file = file
@list = []
@current_num_of_tasks = @list.length
load!
end
def load!
CSV.foreach(file) do |task|
@list << Task.new($INPUT_LINE_NUMBER, task)
end
end
def list
@list.map {|task| task.to_s}.join("\n")
end
def add(task)
new_task = task.join(' ').split(',')
@list << Task.new(@current_num_of_tasks + 1, new_task)
puts "Appended \"#{new_task.join}\" to your TODO list"
save!
end
def delete(task_number)
@list.delete_if {|task| task.row_number.to_s == task_number}
puts "Deleted #{task_number} from your TODO list"
save!
end
def save!
CSV.open(file, "wb") do |csv|
@list.each do |task|
csv << [task.text,task.complete]
end
end
end
def complete(task_number)
@list.each do |task|
if task.row_number.to_s == task_number
task.complete = "X"
puts "Awesome. Marked #{task_number} as complete. Good job!"
end
end
save!
end
end
## Driver Code
if $ARGV.any?
todo = ToDo.new('todo.csv')
if $ARGV[0] == "list"
puts todo.list
elsif $ARGV[0] == "add"
todo.add($ARGV[1..-1])
elsif $ARGV[0] == "delete"
todo.delete($ARGV[1])
elsif $ARGV[0] == "complete"
todo.complete($ARGV[1])
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment