Skip to content

Instantly share code, notes, and snippets.

@seanot
Forked from dbc-challenges/todo.csv
Last active December 20, 2015 06:19
Show Gist options
  • Save seanot/94bd00d5d68b424d5d8e to your computer and use it in GitHub Desktop.
Save seanot/94bd00d5d68b424d5d8e to your computer and use it in GitHub Desktop.
# test_todo.rb
require './todo.rb'
new_list = ListItemParser.new('todo.csv')
new_list.parse_list
new_list.add_list_item('add list item')
We can make this file beautiful and searchable if this error is corrected: It looks like row 2 should actually have 4 columns, instead of 3 in line 1.
id,todo_text,completed,false
1,Move with Lil to the black mountain hills of Dakota,false
2,Lose Lil to Danny,false
3,Get hit in the eye by Danny,false
4,Walk into town seeking revenge,false
5,Book room at local saloon,false
6,Check into room and read Gideon's bible,false
7,Drink too much gin,false
8,Overhear Lil and Danny in neighboring room,false
9,Burst into neighboring room and declare a showdown,false
10,Get shot by Danny and collapse in corner,false
11,Visit doctor,false
12,Return to room and read Gideon's bible,false
13,Sing along! D'do d'do d'do do do d'do d'do d'do,false
require 'csv'
# What classes do you need?
# Remember, there are four high-level responsibilities, each of which have multiple sub-responsibilities:
# 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 ListItem
attr_accessor :id, :todo_text, :completed
def initialize(args = {})
@id = args[:id]
@todo_text = args[:todo_text]
@completed = args[:completed]
end
def join
[:id, :todo_text, :completed]
end
end
class TodoList
# Displaying information to the user (view)
def self.display_list(display_input)
display_input.each { |i| puts "#{i.id}.\t #{i.todo_text}" }
end
end
class InputController
end
class ListItemParser
# Gathering user input and taking the appropriate action (controller)
# Reading and writing from the todo.txt file (model)
OPTIONS = {:headers => true, :header_converters => :symbol}
attr_accessor :todos
attr_reader :file
def initialize(file)
@file = file
@todos ||= []
end
def parse_list
CSV.foreach(file, OPTIONS) { |row| todos << ListItem.new(row) }
return todos if todos
end
def add_list_item(item)
todos << ListItem.new({ :id => next_item_number,
:todo_text => item,
:completed => false})
TodoList.display_list(todos)
end
def next_item_number
todos.last.id.to_i + 1
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment