Skip to content

Instantly share code, notes, and snippets.

@barcher

barcher/todo.csv Secret

Forked from dbc-challenges/todo.csv
Last active December 17, 2015 16:29
Show Gist options
  • Save barcher/96e3ea4dc3995bd9a3f6 to your computer and use it in GitHub Desktop.
Save barcher/96e3ea4dc3995bd9a3f6 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'
class List
attr_accessor :id_numbers, :list
def initialize(filename)
@filename = filename
@list = []
@id_numbers = (1..1000).to_a
import_tasks
end
def import_tasks
CSV.foreach(@filename){|row| @list << row}
@list.map! {|item| Tasks.new({id: @id_numbers.shift, text: item[0]})}
save_task
end
def add_task(text)
@list << Tasks.new({id: @id_numbers.shift, text: text})
save_task
end
def save_task
CSV.open(@filename, "wb") do |csv|
@list.each {|list_item| csv << [list_item.text]}
end
end
def to_human!
CSV.open(@filename, "wb") do |csv|
@list.each do |list_item|
csv << ["#{list_item.id}. [ ] #{list_item.text}"] unless list_item.complete
csv << ["#{list_item.id}. [X] #{list_item.text}"] if list_item.complete
end
end
end
def delete_task(id)
@list.delete_if { |task| task.id == id }
save_task
end
def display_all_tasks
@list.each do |task|
puts "#{task.id} #{task.text} -- #{task.complete}"
end
end
def complete_task(id)
@list.each {|item| item.complete = true if item.id == id}
end
end
class Tasks
attr_accessor :complete
attr_reader :id, :text
def initialize(args)
@text = args[:text]
@id = args[:id]
@complete = false
end
end
list_son = List.new('todo2.csv')
if ARGV.any?
list_son.add_task(ARGV[1..-1][0]) if ARGV[0] == "add"
list_son.delete_task(ARGV[1].to_i) if ARGV[0] == "delete"
list_son.complete_task(ARGV[1].to_i) if ARGV[0] == "complete"
list_son.to_human! if ARGV[0] == "convert!"
list_son.display_all_tasks
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment