Skip to content

Instantly share code, notes, and snippets.

@caioertai
Created February 4, 2022 17:43
Show Gist options
  • Save caioertai/b0fabb6ddb8cd11af70e2d7c0a73adc3 to your computer and use it in GitHub Desktop.
Save caioertai/b0fabb6ddb8cd11af70e2d7c0a73adc3 to your computer and use it in GitHub Desktop.
require_relative "router"
require_relative "task_repository"
require_relative "tasks_controller"
# Start a task repo and add a task to it
task_repository = TaskRepository.new
tasks_controller = TasksController.new(task_repository)
router = Router.new(tasks_controller)
router.run
class Router
def initialize(tasks_controller)
@tasks_controller = tasks_controller
end
def run
puts "Welcome to Taskmaster"
loop do
puts "What do you want to do?"
puts "1. Create a task"
puts "2. List tasks"
puts "3. Mark a task as done"
puts "0. Leave"
user_input = gets.chomp.to_i
case user_input
when 1 then @tasks_controller.create
when 2 then @tasks_controller.list
when 3 then @tasks_controller.mark
when 0 then break
end
end
end
end
class Task # model
attr_reader :description
def initialize(description)
@description = description
@done = false
end
def done!
@done = true
end
def done?
@done
end
end
class TaskRepository
def initialize
@tasks = []
end
def add(task_instance)
@tasks << task_instance
end
def all
@tasks
end
end
class TaskView
def ask_for_description # instance method
puts "Describe the task:"
gets.chomp
end
def display(tasks)
tasks.each_with_index do |task, index|
# task -> Task instance
box = task.done? ? "[X]" : "[ ]"
puts "#{index + 1}. #{box} - #{task.description}"
end
end
def ask_for_index
puts "Which number?"
gets.chomp.to_i - 1
end
end
require_relative "task_view"
require_relative "task"
class TasksController
# DATA
# - Task view
def initialize(task_repository)
@task_view = TaskView.new
@task_repository = task_repository
end
# User Action -> Create
def create
description = @task_view.ask_for_description
task = Task.new(description)
@task_repository.add(task)
end
# User Action -> List
def list
display_tasks
end
def mark
display_tasks
# Ask VIEW to ask user for a task number
index = @task_view.ask_for_index
# Get the task by index
task = @task_repository.find(index)
# Ask TASK instance to mark itself as done
task.done!
end
private
def display_tasks
tasks = @task_repository.all
@task_view.display(tasks)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment