Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@rserna2010
Forked from dbc-challenges/todo.csv
Last active December 21, 2015 04:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rserna2010/98d48073d92a7d6f0845 to your computer and use it in GitHub Desktop.
Save rserna2010/98d48073d92a7d6f0845 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
require 'csv'
# 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).
class List
attr_reader :id, :task
def initialize(file)
@file_location = file
@file = CSV.open(file, "r")
@temp_holder = []
@display_holder = []
current_file
end
def current_file
@file.each { |row| @temp_holder << row}#.join(". ") }
@file.rewind
end
def list
puts "calling list"
@temp_holder.each { |row| @display_holder << "#{@temp_holder.index(row)}. #{row[0]} #{row[1]}" }
@display_holder.shift
@display_holder.each { |row| print row }
end
# Not human-readable
def export(filename)
File.open(filename, "w").write(list)
end
def add(task)
@temp_holder << ['[ ]'] + [task.content]#[task.content]
save_to_csv
puts "Appended \"#{task.content}\" to your TODO list..."
end
def save_to_csv
CSV.open(@file_location, "wb") do |csv|
@temp_holder.each { |row| csv << row }
end
end
def delete(task_number)
puts "Deleted \"#{@temp_holder[task_number].last}\" from your TODO list..."
@temp_holder.delete_at(task_number)
save_to_csv
end
def completed(task_number)
puts "You've completed \"#{@temp_holder[task_number].last}\" from your TODO list..."
@temp_holder[task_number][0].to_s.gsub!(/\s/, 'x')
save_to_csv
end
end
class Task
attr_accessor :content
def initialize(content)
@content = content
end
end
my_list = List.new('todo.csv')
#This runs the program
case ARGV[0]
when "list"
my_list.list
when "add"
my_list.add(Task.new(ARGV[1..-1].join(" ")))
when "delete"
my_list.delete(ARGV[1].to_i)
when "completed"
my_list.completed(ARGV[1].to_i)
when "export"
my_list.export(ARGV[1..-1].join(" "))
end
@audibleblink
Copy link

love the use of case

@leeacto
Copy link

leeacto commented Aug 19, 2013

I agree. The case is really good. I'll try to implement it in my refactor.

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