Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Justin Holm solution #26

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions Jays-Game/Justins-Game/controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
require_relative 'view'
require_relative 'model'

class GameController
include GameView

def run!
todo_list = List.new

Print::welcome

loop do
Print::menu
case Print::fetch_input
when "V"
Print::print_list(todo_list.todos)
when "A"
Print::add_todo(Print::serialize_todo)
when "C"
Print::complete_todo(Print::completed_id.to_i)
when "D"
Print::delete_todo(Print::deleted_id.to_i)
when "Q"
puts "Thanks for playing!"
exit
else
Print::error_message
end
end
end
end

GameController.new.run!
47 changes: 47 additions & 0 deletions Jays-Game/Justins-Game/model.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
class Todo
attr_reader :id, :name, :description, :completed

def initialize(args)
@id = args[:id]
@name = args[:name]
@description = args[:description]
@completed = false
end

def complete
@completed = true
end

def completed?
@completed
end
end

class List
attr_reader :todos

def initialize
@pid = 0
@todos = []
end

def add_todo(input)
@todos << Todo.new(input.merge(find_id))
end

def complete_todo(id)
completed_item = @todos.select{ |todo| todo.id == id }
unless completed_item
fail "Cannot find todo with id of #{id}"
end
end

def delete_todo(id)
@todos.delete_if{ |n| n.id == id }
end

private
def find_id
{id: @pid += 1}
end
end
69 changes: 69 additions & 0 deletions Jays-Game/Justins-Game/view.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
module GameView
module Print
class << self
def error_message
puts "Oops, that doesn't appear to be a command key. Please try again!"
end

def welcome
welcome = <<WELCOME
**********************
* WELCOME! *
**********************
WELCOME
puts welcome
end

def menu
menu = <<MEN
**********:)**********
-> (V)iew your todos
-> (A)dd a todo
-> (C)omplete a todo
-> (D)elete a todo
-> (Q)uit program
**********************
MEN
puts menu
end

def print_list(todos)
todos.each do |todo|
if todo.completed?
puts "[X] #{todo.id} || #{todo.name} - #{todo.description}"
else
puts "[X] #{todo.id} || #{todo.name} - #{todo.description}"
end
end
end

def serialize_todo
{}.tap do |obj|
["\nName: ", "\nDescription: "].each do |t|
if obj.empty?
obj[:title] = fetch_input(t)
else
obj[:description] = fetch_input(t)
end
end
end
end

def deleted_id
given_id = "Enter the id of the todo you want to delete: "
fetch_input(given_id)
end

def completed_id
given_id = "Enter the id of the todo you want to mark as completed: "
fetch_input(given_id)
end

def fetch_input(question=nil)
puts question if question
print "> "
gets.chomp
end
end
end
end
2 changes: 1 addition & 1 deletion Jays-Game/view.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def run_spinner
def error_message
puts "That's not a command key. Try again!"
end

def title_screen
title = <<TITLE

Expand Down