Created
April 21, 2016 19:52
-
-
Save agnaite/3e89d41b0c17ec922a6b8140037bbcff to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'sinatra' | |
require 'shotgun' | |
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 | |
get('/') do | |
erb :index | |
@tasks = myList.show_tasks | |
end | |
post '/' do | |
@task = params[:task] | |
myList.add(@task) | |
"#{@tasks}" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment