Skip to content

Instantly share code, notes, and snippets.

@avalon9000
Forked from dbc-challenges/todo.csv
Last active December 16, 2015 03:09
Show Gist options
  • Save avalon9000/f41a4ca83efaa876993c to your computer and use it in GitHub Desktop.
Save avalon9000/f41a4ca83efaa876993c 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 Task
attr_accessor :box, :id, :description, :date_added
def initialize(box,id,description,date_added)
@box = "[_]"
@id = id.to_s
@description = description
@date_added = date_added
end
def print_task
"#{@box}" + " " + "#{@id} " + "#{@description} " + "created on " + "#{@date_added}"
end
def complete
gsub!(/[\[\_\]]/, "[X]")
end
def undo
gsub!(/[\[\X\]]/, "[_]")
end
def to_s
self.print_task
end
end
class ToDoList
attr_reader :file, :list
def initialize(file)
@file = file
@list = []
import_list
system "clear"
command_line
end
def import_list
return @list unless @list.empty?
task_num = 1
CSV.foreach(@file) do |row|
@list << Task.new("[_]", task_num, row[0], Time.now)
task_num += 1 # incrementally adds one to the task number
end
@list
end
def add_task(description)
last_task_id = @list.last.id
@list << Task.new("[_]", last_task_id.to_i + 1, description, Time.now)
end
def export_list
CSV.open(@file, "w") do |csv|
@list.each {|task| csv << [task.box, task.id, task.description, task.date]}
end
end
def print_list
@list.map { |task| task.print_task }
end
def print_incomplete
@list.select { |task| task if task.box == "[_]" }
end
def print_completed
@list.select { |task| task if task.box == "[X]" }
end
def complete_task(id)
@list.select { |task| task.complete if task.id == id}
end
def undo_task(id)
@list.select { |task| task.undo if task.id == id}
end
def command_line
puts "\n\n"
print "-------------------------------------"
puts"\n""======== U GOT THANGS 2 DO ========""\n"
puts "-------------------------------------"
puts " "
puts "all"
puts "completed"
puts "incomplete"
puts "add"
puts "check"
puts "uncheck"
puts "-------------------------------------"
choice = gets.chomp
while choice != "exit"
while choice == "all"
system "clear"
puts self.print_list
command_line
end
system "clear"
while choice == "incomplete"
puts self.print_incomplete
command_line
end
system "clear"
while choice == "completed"
puts self.print_completed
command_line
end
system "clear"
while choice == "add"
puts "type description of task"
description = gets.chomp
add_task(description)
system "clear"
puts self.print_list
command_line
end
system "clear"
while choice == "check"
puts self.print_list
puts "type the id of the task you finished"
description = gets.chomp.to_s
self.complete_task(description)
system "clear"
puts self.print_list
command_line
end
system "clear"
while choice == "uncheck"
puts self.print_list
puts "type the id of the task you want to uncheck"
description = gets.chomp.to_s
self.list.undo_task(description)
system "clear"
puts self.print_list
command_line
end
system "clear"
end
end
end
rockyslist = ToDoList.new('todo.csv')
# if $ARGV.any?
# if $ARGV[0] == "all"
# puts rockyslist.print_list
# elsif $ARGV[0] == "incomplete"
# puts rockyslist.print_incomplete
# elsif $ARGV[0] == "complete"
# rockyslist.complete_task($ARGV[1])
# elsif $ARGV[0] == "complete"
# rockyslist.undo_task($ARGV[1])
# end
# end
@macb
Copy link

macb commented Apr 13, 2013

Nice job trying to keeping most of your methods down to 1 responsibility. My comment would be to try and refactor the command_line method down by pulling all the puts statements out and making a UI class that will handle those and pulling the logic out into a controller class that will route the commands to and from UI/List.

Ooh, also your regex could be made simpler. Just gsub out the "_" for an X.

@adamsartell
Copy link

I like how you used gsub for checking off tasks! I want to use that now! I agree with Mac about the command line "mega moth" method! Think about how you could refactor to minimize the repetition. I like this - good job!

@avalon9000
Copy link
Author

@SeanBr @acsart totally refactoring this insanity tree! Got a little carried away w/ simply making it work and experimenting with the front end. @SeanBr thanks about the gsub!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment