Skip to content

Instantly share code, notes, and snippets.

@garrethdev
Forked from dbc-challenges/todo.csv
Last active December 25, 2015 19:39
Show Gist options
  • Save garrethdev/572f20e4e7618e39aa8f to your computer and use it in GitHub Desktop.
Save garrethdev/572f20e4e7618e39aa8f 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.
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
# What classes do you need?
# Remember, there are four high-level responsibilities, each of which have multiple sub-responsibilities:
# 1. Gathering user input and taking the appropriate action (controller)
# 2. Displaying information to the user (view)
# 3. Reading and writing from the todo.txt file (model)
# 4. Manipulating the in-memory objects that model a real-life TODO list (domain-specific model)
# Note that (4) is where the essence of your application lives.
# Pretty much every application in the universe has some version of responsibilities (1), (2), and (3).
require 'csv'
class Parser
attr_reader :file
attr_accessor :list_of_items
def initialize(file)
@file = file
@list_of_items = []
@line_at_a_time
end
def line_at_a_time
CSV.foreach(file) do |row|
list_of_items << row
end
list_of_items
end
end
class To_do_list
attr_accessor :document
def initialize (to_do_list_doc ,new_item = nil )
@document = to_do_list_doc
end
def delete_item(todo_item)
document.delete_at(todo_item)
document
end
def add_item(new_item)
@document = @document << new_item
@document
end
def complete_task(todo_item)
p "hi"
p document
p todo_item
document.map! {|x| if document[todo_item] == x
x = "#{document[todo_item].join()}"
x = "#{x} completed."
x = x.split(".")
else
x
end }
p document
end
end
to_do_list_doc = Parser.new('todo.csv').line_at_a_time
list = To_do_list.new(to_do_list_doc)
command = ARGV.shift
case command
when "add"
list.add_item(ARGV[0..-1])
when "list"
p list.document
when "delete"
item = ARGV[0]
list.delete_item(item.to_i)
when "complete"
item = ARGV[0]
list.complete_task(item.to_i)
puts "you completed this task"
when "new"
p To_do_list.new(to_do_list_doc)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment