Skip to content

Instantly share code, notes, and snippets.

@rikas
Created April 19, 2019 10:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rikas/fcecb361140e78554dd2e18a9b541f90 to your computer and use it in GitHub Desktop.
Save rikas/fcecb361140e78554dd2e18a9b541f90 to your computer and use it in GitHub Desktop.
TODO
# controller.rb
require_relative 'view'
require_relative 'task'
class Controller
def initialize(repository)
@repo = repository
@view = View.new
end
def create
# 1. Ask for the title
title = @view.ask_task_title
# 2. Create the task
task = Task.new(title)
# 3. Store the task in the repo
@repo.add(task)
end
def mark_as_done
# 1. Display the list of tasks
display_tasks
# 2. Ask for the index
index = @view.ask_for_index - 1
# 3. Fetch the task with the given index
task = @repo.find(index)
# 4. Mark the task as done
task.mark_as_done!
display_tasks
# 5. Save the task again (not needed because tasks are in memory)
end
def display_tasks
tasks = @repo.task_list
@view.display(tasks)
end
def delete
# 1. Display the tasks
display_tasks
# 2. Ask for the index
index = @view.ask_for_index - 1
# 3. Delete the task with the given index
@repo.delete_at(index)
display_tasks
end
end
require_relative 'router'
require_relative 'repository'
require_relative 'controller'
repo = Repository.new
controller = Controller.new(repo)
router = Router.new(controller)
router.run
# require_relative 'task'
# require_relative 'repository'
# require_relative 'view'
# repo = Repository.new
# task1 = Task.new('Do the laundry')
# task2 = Task.new('Buy food')
# # --------------
# repo.add(task1)
# repo.add(task2)
# view = View.new
# view.display(repo.task_list)
# controller = Controller.new(repo)
# # repo.find(0)
# # tasks = repo.task_list
# # task2.toggle_done!
# # task1.toggle_done!
# # task1.toggle_done!
# # task1.mark_as_done!
# repository.rb
class Repository
def initialize
@tasks = []
end
def add(task)
@tasks.push(task)
end
def find(index)
@tasks[index]
end
def delete_at(index)
@tasks.delete_at(index)
end
def task_list
@tasks
end
end
# router.rb
class Router
def initialize(controller)
@controller = controller
@running = true
end
def run
while @running do
print_actions
action = gets.chomp.to_i
dispatch(action)
end
end
def print_actions
puts "---------------"
puts "What do you want to do?"
puts "1. Add a new task"
puts "2. List the tasks"
puts "3. Mark task as done"
puts "4. Delete a task"
puts "9. Quit"
print '> '
end
def dispatch(action)
case action
when 1 then @controller.create
when 2 then @controller.display_tasks
when 3 then @controller.mark_as_done
when 4 then @controller.delete
when 9 then @running = false
else
puts "Invalid input!!!!!!"
end
end
end
# task.rb
class Task
attr_reader :title
def initialize(title)
@title = title
@done = false
end
def done?
return @done
end
def mark_as_done!
@done = true
end
def toggle_done!
@done = !@done
end
end
# view.rb
class View
def display(tasks)
puts ""
puts "~~~~~~~~"
tasks.each_with_index do |task, index|
puts "#{index + 1} [#{task.done? ? '✅' : ''}] #{task.title}"
end
puts "~~~~~~~~"
puts ""
end
def ask_task_title
puts "What's title?"
print "> "
gets.chomp
end
def ask_for_index
puts "What's the task number?"
print '> '
gets.chomp.to_i
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment