Skip to content

Instantly share code, notes, and snippets.

@ayoformayo
Forked from dbc-challenges/todo.csv
Last active December 18, 2015 11:19
Show Gist options
  • Save ayoformayo/1e07e51a69567a7dc4ba to your computer and use it in GitHub Desktop.
Save ayoformayo/1e07e51a69567a7dc4ba to your computer and use it in GitHub Desktop.
id task_text completeness
1 Meet Gandalf Incomplete
2 Get the ring from Bilbo Incomplete
3 Go on an adventure Incomplete
4 Realize this is some real, real shit going down Incomplete
5 Avoid killing Gollum Incomplete
6 Escape the Balrog Incomplete
7 Spoiler Alert: WIN Incomplete
require 'csv'
class List
attr_reader :tasks
def initialize
@tasks = []
end
def add(task)
@tasks << task
end
def delete(id_integer)
@tasks.each do |x|
if id_integer == x.id
@tasks.slice!(id_integer.to_i - 1)
end
end
@tasks.each_with_index {|x, i| x.id = i + 1 }
end
def complete(id_integer)
tasks.each do |x|
if id_integer == x.id
x.completeness = "Completed!"
end
end
end
end
class Task
attr_accessor :id, :completeness
attr_reader :task
def initialize(hash)
@id = hash[:id]
@task = hash[:task_text]
@completeness = hash[:completeness]
end
end
class TaskMaster
attr_reader :command_line_input, :command, :task, :my_list
def initialize(file)
@file = File.new(file)
@parsed_file = CSV.parse(@file, :headers => true)
@my_list = List.new
read_file
@command_line_input = ARGV
@command = ARGV[0]
@command_line_task = ARGV[1..-1].join(" ") unless ARGV[1] == nil
parse_command
p @my_list
end
def parse_command
if @command == "add"
@my_list.add(Task.new(task_text: @command_line_task, id:(@parsed_file.length+1), completeness: "Incomplete"))
write_file
elsif @command == "delete"
@my_list.delete(@command_line_task)
write_file
elsif @command == "complete"
@my_list.complete(@command_line_task)
write_file
elsif @command == 'list'
list
end
end
def list
@my_list.tasks.each do |each_task|
puts "#{each_task.id} - #{each_task.task} - #{each_task.completeness}"
end
end
def write_file
new_csv = CSV.open('to_do_list.csv', "w")
new_csv <<['id', 'task_text', 'completeness']
@my_list.tasks.each {|task_object| new_csv << [task_object.id,task_object.task, task_object.completeness]}
end
def read_file
@parsed_file.each {|task_array| @my_list.add(Task.new(id: task_array['id'], task_text: task_array['task_text'], completeness: task_array['completeness']))}
end
end
tasks = TaskMaster.new('to_do_list.csv')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment