Skip to content

Instantly share code, notes, and snippets.

@luizfonseca
Created July 14, 2017 16:58
Show Gist options
  • Save luizfonseca/1dd44d52129466c6399c889c8bb1b7a8 to your computer and use it in GitHub Desktop.
Save luizfonseca/1dd44d52129466c6399c889c8bb1b7a8 to your computer and use it in GitHub Desktop.
Todo List using MVC
# this file serves as a router for any questions related to tasks
class Controller
# Called on `Controller.new`
def initialize(repository, view)
# STATE
# We receive an instance of Repository Class
@repository = repository
# Receive a instance of View, so we can display things
@view = view
end
# Create
def add_task
# 1. to display to the user a screen
# and the method returns the user input
title = @view.ask_for_task
# 2. Instantiate a new Task
task = Task.new(title)
# 3. Store the task on repository
@repository.add_task(task)
end
# Read the list of tasks
def list_tasks
# 1. Get the list of tasks
tasks = @repository.tasks
# 2. Display the list of tasks
@view.show_list_of_tasks(tasks)
end
def mark_task_as_completed
# Reusing the READ method (listing tasks)
list_tasks
# Showing a new screen/interface to mark a task
# as completed
task_index = @view.ask_which_task_to_mark
task = @repository.find(task_index)
task.mark_as_completed
end
end
# Todo manager app
1. Show interface to the user (*View*)
2. Get the task on the To-do list (title) (*Controller* / *Model*)
3. Store the task in a file/list/whatever (*Repository*)
4. See the list of tasks (*Controller*)
5. I want to mark if a task is completed/done (*Controller*)
# This class handles everything related to STORAGE
# we store class on it nad perform other DATABASE related actions
class Repository
attr_reader :tasks
# Called on `Repository.new`
def initialize
# STATE:
# Empty list of tasks by default
@tasks = []
end
# Find a task based on index position
def find(index_number)
@tasks[index_number]
end
# Crud actions
# add_task (create)
def add_task(task)
@tasks << task
end
# show_task (read)
# edit_task (update)
# remove_task (demolish)
end
# This is the MODEL
# where we can change data about a Task
class Task
attr_reader :title, :completed
# 1. Is to define a beginning (constructor)
# 2. Task.new("receives a title")
def initialize(title)
# State
@title = title
@completed = false # By default, a task is not completed
end
# Returns the info about completed
def completed?
return @completed
end
# Mark task as completed
def mark_as_completed
@completed = true
end
end
# NO user interaction on this one, but if you look at KARR, there's an example of Router.rb
# to follow
require_relative "controller"
require_relative "repository"
require_relative "task"
require_relative "view"
# this is the place where we store tasks
repository = Repository.new
# this is the class that displays the interface
view = View.new
# this is the place we understand user actions and delegate/translate them
controller = Controller.new(repository, view)
# task1 = Task.new("Grocery Shop")
# Calling the action on the controller
controller.add_task
controller.add_task
# Mark one task as completed, receiving input from the user
controller.mark_task_as_completed
# Show list of tasks, ask the controller
controller.list_tasks
# This file presents/display everything related to the task controller
class View
# Display a nice screen to the user
# asking about a title for the task
def ask_for_task
puts "What do you want to do?"
print "> "
# Gets user input and returns it on the method
user_input = gets.chomp
return user_input
# gets.chomp on the last line
end
# Display tasks
# Tasks is an array of tasks
def show_list_of_tasks(tasks)
puts "Here is the list of tasks:"
# [ ] not completed
# [x] completed
tasks.each_with_index do |task, index|
x = task.completed? ? "x" : " "
puts "#{index + 1}. [#{x}] #{task.title}"
end
end
def ask_which_task_to_mark
puts "Which task to mark as completed? (number)"
user_input = gets.chomp
# Fix user input, so we get a correct index number
return user_input.to_i - 1
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment