Skip to content

Instantly share code, notes, and snippets.

@rodloboz
Created August 7, 2020 08:24
Show Gist options
  • Save rodloboz/9b7f98b2979739513d1f025d43c166dd to your computer and use it in GitHub Desktop.
Save rodloboz/9b7f98b2979739513d1f025d43c166dd to your computer and use it in GitHub Desktop.
MVC Intro - TODO App
require_relative "task"
require_relative "task_repository"
require_relative "tasks_controller"
require_relative "router"
require "byebug"
# Create an instance of TaskRepository
task_repository = TaskRepository.new
# task = Task.new("Learn MVC")
# task_repository.add_task(task)
# p task_repository.all
# Create a controller instance
# Needs a TaskRepository instance
tasks_controller = TasksController.new(task_repository)
# Create the Router instance
# Needs a TasksController instance
router = Router.new(tasks_controller)
# Start the programme
router.run
# Temporary FOR DEBUGGING
# Create an instance of TaskRepository
# task_repository = TaskRepository.new
# # Create an instance of Task
task = Task.new("Learn MVC")
task_repository.add_task(task)
# p task_repository.all
class Router
def initialize(controller)
@controller = controller
@running = false
end
# Public Interface
def run
@running = true
while @running do
# Display the menu
print_actions
# Capture user action
action = gets.chomp.to_i
# Dispatch the action to the controller
dispatch(action)
end
end
private
def print_actions
puts "\n----- MENU -----"
puts "1. List Tasks"
puts "2. Add Task"
puts "3. Mark a Task as Done"
puts "4. Remove a Task"
puts "5. Quit"
puts "\n----------------"
end
def dispatch(action)
case action
when 1 then @controller.list
when 2 then @controller.add
when 3 then @controller.mark_as_done
when 4 then @controller.destroy
when 5 then @running = false
end
end
end
# This is out model
class Task
attr_reader :description
# Task.new("Learn OOP")
def initialize(description)
# @description = "Learn OOP"
@description = description
@done = false # Boolean
end
# Read for booleans - by convention ends with ?
def done?
@done
end
# attr_reader implements this method for us
# def description
# @description
# end
def mark_as_done!
@done = true
end
def to_s
done = done? ? "[x]" : "[ ]"
"#{done} #{@description}"
end
end
# Repo (Will later be replaced by a real DB)
class TaskRepository
def initialize
@tasks = []
end
def all
@tasks
end
# Receives a task instance
def add_task(task)
@tasks << task
end
def find(index)
# Array CRUD: Read
@tasks[index]
end
def remove(index)
# Array CRUD: Delete
@tasks.delete_at(index)
end
end
require_relative "tasks_view"
class TasksController
def initialize(task_repository)
@task_repository = task_repository
@view = TasksView.new
end
def list
display_tasks
end
def add
# Tell the view to ask the user for a task desc.
# Store the user description
description = @view.ask_for_description
# Create a Task instance
task = Task.new(description)
# Tell the repo to add the new task
@task_repository.add_task(task)
end
def mark_as_done
display_tasks
# Tell the view to ask the User for an index
index = @view.ask_for_index
# Ask the repo to find the task with the index
task = @task_repository.find(index)
# Ask the task to mark itself as done
task.mark_as_done!
end
def destroy
# Display the tasks
display_tasks
# Tell the view to ask the User for an index
index = @view.ask_for_index
# Asks the repo to remove the task with the index
@task_repository.remove(index)
end
private
def display_tasks
# Ask the repo for the list of tasks
tasks = @task_repository.all
# Tell the view to display the tasks
@view.display_tasks(tasks)
end
end
# Only has behaviour
# User by the TasksController
class TasksView
def display_tasks(tasks)
# 1 - [] Learn MVC
# 2 -
tasks.each_with_index do |task, index|
puts "#{index + 1} - #{task}"
end
end
def ask_for_description
puts "What's the task you want to add?"
print "> "
# Capture and return User input
gets.chomp
end
def ask_for_index
puts "Which task?"
print "> "
# Capture and return User input
# convert to integer and to the real index
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