Skip to content

Instantly share code, notes, and snippets.

@yclim95
Forked from nextacademy-private/todo.csv
Last active January 25, 2016 16:27
Show Gist options
  • Save yclim95/929f0de7fd560b60ff26 to your computer and use it in GitHub Desktop.
Save yclim95/929f0de7fd560b60ff26 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
# 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) - waiter(front), task
# 2. Displaying information to the user (view) - interface
# 3. Reading and writing from the todo.txt file (model) - chef(background) + list
# 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 'csv'
class Task # Controller
attr_accessor :id, :text, :completed
def initialize(option = {})
@id = option.fetch(:id){nil}
@text = option.fetch(:text)
@completed = option.fetch(:completed){false}
end
end
class List # Model
attr_reader :file, :list
def initialize(file)
@file = file
@list = nil
end
def parse_file # initial_scan // scan from start
return @list if @list
@list = []
CSV.foreach(@file) { |row|
if row.size == 3 # Check if there is 3 row
if row[2] == "true"
row[2] = true # Completed
else
row[2] = false # Not completed
end
row = Task.new(id: @list.size, text: row[1], completed: row[2])
add(row)
else
row = Task.new(id: @list.size, text: row[0])
add(row)
end
}
end # END of Parse_file
def display #View
puts "There are #{@list.size} tasks in the file '#{@file}'."
@list.each_with_index { |task, index|
if (index+1) > 9 # More than 10 and above
mark = task.completed ? "[X]" : "[ ]"
else
mark = task.completed ? " [X]" : " [ ]"
end
# puts "##{task.id+1}. #{'[X]' if task.completed} #{task.text}"
puts "##{task.id+1}. #{mark} #{task.text}"
}
puts ""
end
def add(task)
if task.id == nil
task.id = @list.size
end
@list << task
end
def update_list(list)
list.each_with_index { |row, index|
row.id = index
}
end
def delete(task_number)
@list.delete_at(task_number-1)
update_list(@list)
end
def complete(task_number)
@list[task_number-1].completed = true
update_list(@list)
end
def save
CSV.open(@file, "w+") { |csv|
@list.each { |task|
csv << [task.id, task.text, task.completed]
}
} # END of CSVs
end
end # END of Class
class Driver
def start!
if ARGV.any?
# puts "The command line arguments are:"
first_arg, *the_rest = ARGV
# p first_arg
if the_rest.empty?
else
the_rest = the_rest.join(" ")
# p the_rest
end
end
list = List.new('todo.csv')
list.parse_file
list.save
case first_arg
when "list"
clear_screen!
move_to_home!
list.display # Display the text
when "add"
new_task = Task.new(text: the_rest)
list.add(new_task)
list.save
clear_screen!
move_to_home!
puts "Appended '#{the_rest}'' to your TODO list..."
when "delete"
clear_screen!
move_to_home!
if ARGV[1].nil?
puts "\nInvalid input. Need Help ??\nType : ruby testing.rb help"
else
the_rest = the_rest.to_i
list.delete(the_rest)
list.save
puts "Deleted '#{the_rest}' from your TODO list..."
end
when "complete"
clear_screen!
move_to_home!
if ARGV[1].nil?
puts "\nInvalid input. Need Help ??\nType : ruby testing.rb help"
else
the_rest = the_rest.to_i
list.complete(the_rest)
list.save
puts "Have mark '#{the_rest}' completed from your TODO list..."
end
when "help"
clear_screen!
move_to_home!
puts "ruby testing.rb (add,delete,complete,list) value/text"
puts "EG. 1: ruby testing.rb add Hello"
puts "EG. 2: ruby testing.rb delete 1, [NOTE: 1 means the position!]"
puts "EG. 3: ruby testing.rb complete 2, [NOTE: 2 means the position!]"
puts "EG. 4: ruby testing.rb list"
else
puts "Need Help ??\nType : ruby testing.rb help"
end
end # END of start Method
private
# Clear the screen
def clear_screen!
print "\e[2J"
end
# Moves cursor to the top left of the terminal
def move_to_home!
print "\e[H"
end
end # ENDof Driver class
driver = Driver.new
driver.start!
0 Move with Lil to the black mountain hills of Dakota false
1 Lose Lil to Danny true
2 Get hit in the eye by Danny false
3 Walk into town seeking revenge false
4 Book room at local saloon true
5 Check into room and read Gideon's bible false
6 Drink too much gin false
7 Overhear Lil and Danny in neighboring room false
8 Burst into neighboring room and declare a showdown false
9 Get shot by Danny and collapse in corner false
10 Visit doctor false
11 Return to room and read Gideon's bible false
12 bye false
13 Bye false
14 testing false
15 [] false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment