Created
October 16, 2020 10:11
-
-
Save Haumer/ebfea863067cbbb74c588dabe8eabc37 to your computer and use it in GitHub Desktop.
This file contains hidden or 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_relative "task" | |
require_relative "view" | |
class Controller | |
def initialize(repository) | |
@repository = repository | |
@view = View.new | |
end | |
def add_task | |
# 1. ask the user for task name | |
name = @view.ask_user_for_name | |
# 2. create the task | |
task = Task.new(name) | |
# 3. save the task to the repo | |
@repository.add(task) | |
end | |
def list_all_tasks | |
# 1. retrieve all tasks from repo | |
tasks = @repository.all | |
# 2. hand the tasks over to the view | |
@view.display_all_tasks(tasks) | |
end | |
def mark_task_as_done | |
# 1. ask user which task is done? | |
index = @view.ask_for_index | |
# 2. find the correct task in the repo | |
task = @repository.find(index) | |
# 3. mark the task as done | |
task.mark_done! | |
end | |
end |
This file contains hidden or 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
class Repository # Database | |
def initialize | |
@tasks = [] | |
end | |
def add(task) | |
@tasks << task | |
end | |
def all | |
# return all tasks | |
@tasks | |
end | |
def find(index) | |
return @tasks[index] | |
end | |
end |
This file contains hidden or 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
class Router | |
def initialize(controller) | |
@controller = controller | |
end | |
def run | |
loop do | |
puts "- 1: Add a Task" | |
puts "- 2: Display all Tasks" | |
puts "- 3: Mark a Task as done" | |
choice = gets.chomp | |
if choice == "1" | |
@controller.add_task | |
elsif choice == "2" | |
@controller.list_all_tasks | |
elsif choice == "3" | |
@controller.mark_task_as_done | |
else | |
puts "invalid choice" | |
end | |
end | |
end | |
end |
This file contains hidden or 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
class Task | |
attr_reader :name | |
def initialize(name) | |
@name = name | |
@done = false | |
end | |
def done? | |
@done | |
end | |
def mark_done! | |
@done = true | |
end | |
end |
This file contains hidden or 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_relative "repository" | |
require_relative "controller" | |
require_relative "router" | |
# require_relative "task" | |
# require_relative "view" | |
repo = Repository.new | |
controller = Controller.new(repo) | |
router = Router.new(controller) | |
router.run | |
# view = View.new | |
# controller.add_task | |
# controller.add_task | |
# p repo | |
# controller.list_all_tasks | |
# controller.mark_task_as_done | |
# controller.list_all_tasks | |
# shopping = Task.new("Shopping") | |
# repo.add(shopping) | |
# laundry = Task.new("Laundry") | |
# repo.add(laundry) | |
# p repo |
This file contains hidden or 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
class View | |
def ask_user_for_name | |
puts "Enter task name!" | |
return gets.chomp.capitalize | |
end | |
def display_all_tasks(tasks) | |
tasks.each_with_index do |task, index| | |
x = task.done? ? "x" : " " | |
puts "#{index + 1} [#{x}] #{task.name}" | |
end | |
end | |
def ask_for_index | |
puts "Choose a Task" | |
puts "hint: type the index" | |
return gets.chomp.to_i - 1 | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment