Skip to content

Instantly share code, notes, and snippets.

@dammitBrandon
Last active December 19, 2015 12:19
Show Gist options
  • Save dammitBrandon/5953855 to your computer and use it in GitHub Desktop.
Save dammitBrandon/5953855 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'
require 'pry'
class View
def self.initialize
@@lists = {}
self.start
end
def self.start
if ARGV.any?
self.do_action(ARGV)
else
self.get_input
end
end
def self.get_input
puts "Welcome to your todo list, here you can create a list and mark them off as you complete them"
puts "What is it that you would like to do? or type help for a list of things that you can do."
input = gets.chomp
while input == ""
puts "Please enter a valid choice"
input = gets.chomp
end
self.do_action(input)
end
def self.do_action(action)
action = action.downcase
#puts "you said #{action}"
#exit if action == "exit"
#self.get_input
while action != "exit"
if action == "list"
# puts "Please enter the list that you want to read from:\nif no file is choosen then we will read the base list"
# list_name = gets.chomp
# if list_name == ""
# list_name = "default"
# #list_path = "./" + list_name + ".csv"
# else
# list_path = "./" + list_name + ".csv"
# end
# #binding.pry
# args = { list_name: list_name, list_path: list_path }
# self.get_list(args) unless @@lists.has_key?(list_name.to_sym)
self.get_list_name(action)
puts "Please enter a new action, if you want to quit, enter 'exit'"
action = gets.chomp
elsif action == "add"
# puts "You want to add"
self.add_list
puts "Please enter a new action, if you want to quit, enter 'exit'"
action = gets.chomp
elsif action == "save"
self.save_list
puts "Please enter a new action, if you want to quit, enter 'exit'"
elsif action == "delete"
puts "You want to delete"
puts "Please enter a new action, if you want to quit, enter 'exit'"
action = gets.chomp
elsif action == "complete"
puts "you have completed a task"
puts "Please enter a new action, if you want to quit, enter 'exit'"
action = gets.chomp
else
puts "Please enter a valid action"
action = gets.chomp
end
end
exit
end
def self.get_list_name(action)
puts "Please enter the list that you want to #{action} from:\nif no file is choosen then we will read the base list"
list_name = gets.chomp
if list_name == ""
list_name = "default"
end
#list_path = "./" + list_name + ".csv"
list_path = "./todo.csv"
args = {list_name: list_name, list_path: list_path}
self.get_list(args) unless @@lists.has_key?(list_name.to_sym)
return list_name
end
def self.add_list
list_name = self.get_list_name("add")
key = list_name.to_sym
puts "Enter the task that you want to add to the list"
task = gets.chomp
@@lists[key].add_to_list(task)
binding.pry
end
def self.get_list(args)
#args = { list_name: list_name, list_path: list_path }
key = args[:list_name].to_sym
#binding.pry
@@lists[key] = List.new(args)
puts @@lists[key].tasks
end
def self.save_list
list_name = self.get_list_name("save")
key = list_name.to_sym
@@lists[key].save_list
puts "The list has been saved"
puts @@lists[key].tasks
end
end
class Parser
#reads and writes to a file
def self.read_file(filename)
i = 1
tasks = []
CSV.foreach(filename) do |row|
args = { :id => i.to_s.to_sym,
:description => row.join
}
tasks << Task.new(args)
i += 1
end
return tasks
end
def self.write_file(filename="./test.csv", list)
list.each do |row|
CSV.open(filename, "a") do |csv|
array = [row.task]
csv << array
end
end
end
end
class Task
attr_reader :id, :task
def initialize(args)
@id = args[:id]
@task = args[:description]
end
# def self.add_task(args)
# Task.new(args)
# end
def to_s
<<-PRINT
id: #{self.id}
#{self.task}
PRINT
end
end
class List
attr_accessor :name, :tasks
def initialize(args)
@name = args[:list_name]
@tasks = Parser.read_file(args.fetch(:list_path, "./todo.csv"))
# @tasks = []
# if @name == "default"
# @tasks = Parser.read_file
# end
end
def get_list_from_file
@tasks = Parser.read_file
end
def add_to_list(task)
id = (self.tasks[-1].id.to_s.to_i + 1).to_s.to_sym
args = { :id => id,
:description => task
}
self.tasks << Task.new(args)
end
def save_list
Parser.write_file(self.tasks)
end
end
View.initialize
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment