Skip to content

Instantly share code, notes, and snippets.

@nmajor
Created January 26, 2018 11:26
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 nmajor/aee26cf35191927574ace79e2a2eb4ba to your computer and use it in GitHub Desktop.
Save nmajor/aee26cf35191927574ace79e2a2eb4ba to your computer and use it in GitHub Desktop.
require_relative 'task'
require_relative 'task_repository'
require_relative 'tasks_controller'
require_relative 'router'
task_repo = TaskRepository.new
task1 = Task.new('Buy bitoque ingredients')
task2 = Task.new('Take train to LeWagon')
task3 = Task.new('Tach an awesome class!')
task_repo.add task1
task_repo.add task2
task_repo.add task3
tasks_controller = TasksController.new(task_repo)
router = Router.new(tasks_controller)
router.run
require_relative 'task'
require_relative 'task_repository'
repository = TaskRepository.new
task1 = Task.new('Buy bitoque ingredients')
task2 = Task.new('Take train to LeWagon')
task3 = Task.new('Tach an awesome class!')
repository.add task1
repository.add task2
repository.add task3
repository.all.each_with_index do |task, index|
if task.done == true
puts "#{index + 1} - [X] #{task.desc}"
else
puts "#{index + 1} - [ ] #{task.desc}"
end
end
puts "Which task do you want to mark complete?"
user_selection = gets.chomp.to_i - 1
task = repository.find(user_selection)
task.mark_as_done!
repository.all.each_with_index do |task, index|
if task.done == true
puts "#{index + 1} - [X] #{task.desc}"
else
puts "#{index + 1} - [ ] #{task.desc}"
end
end
# ----------------
# task_description = gets.chomp # view
# new_task = Task.new(task_description) # controller
# repository.add new_task # controller
# ----------------
# task1 = Task.new('Buy bitoque ingredients')
# task2 = Task.new('Take train to LeWagon')
# task3 = Task.new('Tach an awesome class!')
# repo = TaskRepository.new
# repo.add task1
# repo.add task2
# repo.add task3
# p repo.all
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 complete'
puts '4 - Delete a task'
puts '---'
end
def dispatch(action)
case action
when 1 then @controller.list
when 2 then @controller.create
when 3 then @controller.update
when 4 then @controller.delete
end
end
end
#
# 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 '---'
# end
# def dispatch(action)
# case action
# when 1 then @controller.list
# when 2 then @controller.create
# 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 :desc, :done
def initialize(desc)
@desc = desc
@done = false
end
def mark_as_done!
@done = true
end
end
# List
class TaskRepository
def initialize
@tasks = []
end
def add(task)
@tasks << task
end
def remove(index)
@tasks.delete_at(index)
end
def all
@tasks
end
def find(index)
@tasks[index]
end
end
require_relative 'task_repository'
require_relative 'tasks_view'
class TasksController
def initialize(repository)
@repository = repository
@view = TasksView.new
end
def list
list_all_tasks
end
def create
task_description = @view.ask_for_description
new_task = Task.new(task_description) # controller
@repository.add new_task # controller
end
def update
list_all_tasks
user_selection = @view.ask_which_task_to_complete
task = @repository.find(user_selection)
task.mark_as_done!
end
def delete
list_all_tasks
# Have the vew:
# ask the user which item to delete
# gets.chomp
user_selection = @view.ask_which_task_to_delete
# call the repository.remove
@repository.remove(user_selection)
end
private
def list_all_tasks
tasks = @repository.all
@view.print_tasks(tasks)
end
end
class TasksView
def print_tasks(tasks)
tasks.each_with_index do |task, index|
if task.done == true
puts "#{index + 1} - [X] #{task.desc}"
else
puts "#{index + 1} - [ ] #{task.desc}"
end
end
end
def ask_for_description
puts 'Please enter a task description:'
gets.chomp
end
def ask_which_task_to_complete
puts "Which task do you want to mark complete?"
gets.chomp.to_i - 1
end
def ask_which_task_to_delete
puts "Which item would you like to delete?"
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