Skip to content

Instantly share code, notes, and snippets.

@Databates
Forked from dbc-challenges/todo.csv
Last active August 29, 2015 13:56
Show Gist options
  • Save Databates/69ae7f776aea8b8f202f to your computer and use it in GitHub Desktop.
Save Databates/69ae7f776aea8b8f202f to your computer and use it in GitHub Desktop.
Move with Lil to the black mountain hills of Dakota
Lose Lil to Danny
Get hit in the eye by Danny
true
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
learn rails false
learn rails true
# 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 'pry'; require 'pry-nav';binding.pry
require 'csv'
module HandleCSV
def load_list(file)
self.file = file
CSV.parse(File.read(file), :headers => false) do |row|
@goal_tracker << NewTask.new({todo: row[0..-2].join(','), complete: row[-1] })
end
end
def save
CSV.open(file, "w") do |x|
goal_tracker.each do |item|
x << [item.todo, item.complete]
end
end
end
end
class NewTask
attr_accessor :complete
attr_reader :id, :todo
def initialize(args)
@todo = args[:todo]
@complete = args.fetch(:complete, false) # instead of || falsecon
end
end
class TodoList
include HandleCSV
attr_accessor :file, :goal_tracker #:todos,
def initialize
@goal_tracker = []
@file = "" #instance method file set to an empty string
end
def list_item
counter = 1
goal_tracker.each do |x|
print "#{counter}: #{x.todo}"
if todo.complete == "true"
puts ": Terminat! (finished)"
else
puts
end
counter += 1
end
end
def add_item(add)
goal_tracker << NewTask.new(todo: (add), complete: false)
save
end
def delete(number)
puts "Removing: #{goal_tracker[number.to_i - 1].todo}"
goal_tracker.delete_at(number.to_i - 1)
save
end
def complete(number)
goal_tracker[number.to_i - 1].complete = true
save
end
end
my_list = TodoList.new
my_list.load_list('todo.csv')
p my_list.add_item('learn rails')
p my_list.complete("learn rails")
@oconn
Copy link

oconn commented Feb 8, 2014

Great job separating out the functionality to handle the csv file. This makes TodoList handle only what a
todo list should handle.

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