Skip to content

Instantly share code, notes, and snippets.

@agnaite
Created April 19, 2016 23:19
Show Gist options
  • Save agnaite/d133596bf569a097770089384cccac23 to your computer and use it in GitHub Desktop.
Save agnaite/d133596bf569a097770089384cccac23 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
class List
attr_reader :all_tasks
def initialize
@all_tasks = []
puts 'You have created a new list'
end
def add(task, status=false)
all_tasks << Task.new(task, status)
puts "Task '#{task}' added."
end
def remove(task_id)
task_to_remove = @all_tasks[task_id-1]
if task_to_remove != nil
@all_tasks.delete_at(task_id-1)
puts "Task '#{task_to_remove.description}' removed."
else
puts "Task not found."
end
end
def show_tasks
seperator
puts "Your list: "
@all_tasks.each do |task|
puts "#{get_task_status(task)} #{get_task_id(task)}. #{task.description}."
end
seperator
end
def change_status(task_id)
task = @all_tasks[task_id-1]
if task.status
task.status = false
puts "Task '#{task.description}' needs to be done."
else
task.status = true
puts "Task '#{task.description}' marked completed."
end
end
def export_todo
date = Time.now
date = date.strftime("%m-%d-%y_%H-%M-%S")
File.open("TODO_#{date}.txt", 'w') do |file|
@all_tasks.each do |task|
file.write ("#{get_task_status(task)} #{get_task_id(task)}. #{task.description}.\n")
end
end
puts "To-do was exported."
end
private
def get_task_id(task_id)
@all_tasks.index(task_id) + 1
end
private
def get_task_status(task)
if task.status
"👍"
else
"🖕"
end
end
private
def seperator
str = ""
40.times { str << "*" }
print str << "\n"
end
end
class Task
attr_reader :description, :status
attr_writer :description, :status
def initialize(description, status)
@description = description
@status = status
end
end
myList = List.new
choice = ""
loop do
puts "\nPlease make a selection."
puts "[A] to add task"
puts "[T] to toggle status"
puts "[R] to remove task"
puts "[S] to show tasks"
puts "[E] to export to-do"
puts "[Q] to quit:"
choice = gets.chomp.downcase
case choice
when "a"
puts "Enter task: "
task = gets.chomp
myList.add(task)
when "t"
puts "Enter task ID to toggle status: "
task_id = gets.chomp.to_i
myList.change_status(task_id)
when "r"
puts "Enter task ID to remove: "
task_id = gets.chomp.to_i
myList.remove(task_id)
when "s"
myList.show_tasks
when "e"
myList.export_todo
else
break
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment