require_relative 'router'
require_relative 'tasks_controller'
require_relative 'tasks_repository'
repo = TasksRepository.new
controller = TasksController.new(repo)
Router.new(controller).run
require_relative 'tasks_repository'
# require_relative 'tasks_view'
require_relative 'tasks_controller'
repo = TasksRepository.new
repo.add_task('eat')
repo.add_task('pray')
repo.add_task('love')
repo.mark_task_as_done_by_index(1)
# view = TasksView.new(repo.all)
# view.list
# view.ask_for_index_to_mark
controller = TasksController.new(repo)
controller.list
puts
controller.add_task
# controller.list
puts
controller.mark_as_done
puts
controller.list
class Router
def initialize(controller)
@controller = controller
end
def run
loop do
print_actions
action = gets.chomp.to_i
dispatch(action)
end
end
private
def print_actions
puts "\n---"
puts 'What do you want to do?'
puts '1 - Display tasks'
puts '2 - Add a new task'
puts '3 - Mark a task as done'
puts '4 - Remove a task'
puts '---'
print '> '
end
def dispatch(action)
case action
when 1 then @controller.list
when 2 then @controller.add_task
when 3 then @controller.mark_as_done
when 4 then @controller.destroy
else
puts "Please type 1, 2, 3 or 4 :)"
end
end
end
class Task
attr_reader :description
def initialize(description)
@description = description
@done = false
end
def done?
@done
end
def mark_as_done
@done = true
end
end
require_relative 'tasks_view'
class TasksController
def initialize(repo)
@repo = repo
end
def list
view = TasksView.new(@repo.all)
view.list
end
def add_task
view = TasksView.new(@repo.all)
description = view.ask_for_new_task_description
@repo.add_task(description)
# task = Task.new(description)
# @repo.add_task(task)
end
def mark_as_done
view = TasksView.new(@repo.all)
index = view.ask_for_index_to_mark
@repo.mark_task_as_done_by_index(index - 1)
end
end
require_relative 'task'
class TasksRepository
def initialize
# TODO: load from CSV
@tasks = []
end
def all
@tasks
end
# def add_task(task)
# @tasks << task
# save_tasks
# end
def add_task(description)
@tasks << Task.new(description)
save_tasks
end
def remove_task_by_index(index)
@tasks.delete_at(index)
save_tasks
end
def mark_task_as_done_by_index(index)
@tasks[index].mark_as_done
save_tasks
end
private
def save_tasks
# TODO: save to CSV file
end
end
class TasksView
def initialize(tasks)
@tasks = tasks
end
def list
display_tasks
end
def ask_for_new_task_description
puts "enter description for new task:"
print '> '
gets.chomp
end
def ask_for_index_to_mark
display_tasks
puts "enter index to mark as done:"
print '> '
gets.chomp.to_i
end
private
def display_tasks
@tasks.each_with_index do |task, index|
puts "#{index+1} : [#{ task.done? ? 'X' : ' ' }] #{task.description}"
end
end
end