Skip to content

Instantly share code, notes, and snippets.

@georgekettle
Created January 21, 2021 23:44
Show Gist options
  • Save georgekettle/c876c57506dfa55f95eaa7be44984658 to your computer and use it in GitHub Desktop.
Save georgekettle/c876c57506dfa55f95eaa7be44984658 to your computer and use it in GitHub Desktop.
# User stories:
# -list tasks
# -add a task
# -mark a task as done
# -remove a task
# MVC(R)
# model
# view
# controller
# router/repository
# require_relative 'task'
# require_relative 'view'
require_relative 'repository'
require_relative 'controller'
repository = Repository.new
controller = Controller.new(repository)
# Router/interpreter
action = nil
until action == 'quit'
puts "which action would you like to do? (list | add | mark | remove | quit)"
action = gets.chomp
case action
when "list" then controller.list
when "add" then controller.add
when "mark" then controller.mark
when "remove" then controller.remove
when "quit" then break
else
puts "this is an invalid input"
end
end
require_relative 'view'
require_relative 'repository'
require_relative 'task'
# Controller/conductor --> Class eventually (not yet)
class Controller
def initialize(repository)
@repo = repository
end
def list
tasks = @repo.all
View.list_tasks(tasks)
end
def add
description = View.return_user_input("what is your next task?")
task = Task.new(description)
@repo.add(task)
end
def mark
tasks = @repo.all
View.list_tasks(tasks)
# list
selected_index = View.return_index_input("Which task would you like to mark?")
selected_task = @repo.find(selected_index)
selected_task.mark_done!
View.print("Successfully updated '#{selected_task.description}'")
end
def remove
tasks = @repo.all
View.list_tasks(tasks)
# list
selected_index = View.return_index_input("Which task would you like to delete (number)")
@repo.remove(selected_index)
View.print("Successfully removed your task")
end
end
# Repository
class Repository
def initialize
@tasks = []
end
def all
@tasks
end
def add(task)
@tasks << task
end
def find(index)
@tasks[index]
end
def remove(index)
@tasks.delete_at(index)
end
end
# MODEL
class Task
attr_accessor :description, :done
def initialize(description)
@description = description
@done = false
end
def done?
@done
end
def mark_done!
@done = true
end
end
# View
class View
def self.list_tasks(tasks)
puts "Your tasks:"
tasks.each_with_index do |task, index|
marked = task.done? ? "[X]" : "[ ]"
puts "#{index + 1} - #{marked} #{task.description}"
end
end
def self.return_user_input(prompt)
puts prompt
input = gets.chomp
return input
end
def self.return_index_input(prompt)
puts prompt
input = gets.chomp.to_i - 1
return input
end
def self.print(prompt)
puts prompt
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment