Skip to content

Instantly share code, notes, and snippets.

@ssaunier
Last active August 29, 2015 14:07
Show Gist options
  • Save ssaunier/1386abd77a1246f63381 to your computer and use it in GitHub Desktop.
Save ssaunier/1386abd77a1246f63381 to your computer and use it in GitHub Desktop.
Todo Manager - 17 octobre 2014 - Promo 4 de http://www.lewagon.org
require_relative "task_repository"
require_relative "display"
require_relative "controller"
# Creer fausse base de données
task_repository = TaskRepository.new
# Creer un Display
display = Display.new
# Creer un controlleur
controller = Controller.new(
task_repository, display)
while true
controller.list_tasks
puts "Que voulez-vous faire maintenant?"
puts "1 - Ajouter une tâche"
puts "2 - Terminer une tâche"
action = gets.chomp.to_i
if action == 1
controller.add_task
elsif action == 2
controller.mark_task_as_done
end
end
require_relative "task"
class Controller
def initialize(task_repository, display)
# Injection de dependance
@task_repository = task_repository
@display = display
end
def list_tasks
# recupere toutes les tasks
tasks = @task_repository.tasks
# affiche
@display.print_tasks(tasks)
end
def add_task
description = @display.ask_user_for_new_task_description
task = Task.new(description)
@task_repository.add_task(task)
end
def mark_task_as_done
index = @display.ask_user_for_task_id_to_mark_as_done
if index
task = @task_repository.tasks[index]
task.mark_as_done if task
end
end
# TODO: cocher, supprimer
end
class Display
def ask_user_for_new_task_description
puts "Vous voulez faire quoi ?"
print "> "
return gets.chomp
end
def print_tasks(tasks)
puts "Voici la liste des tâches:"
tasks.each_with_index do |task, index|
done_string = task.done ? "[x]" : "[ ]"
puts "#{index + 1} - #{done_string} #{task.description}"
end
end
def ask_user_for_task_id_to_mark_as_done
puts "Quelle tâche voulez-vous marquer comme faîte ?"
begin
id = Integer(gets.chomp)
id - 1
rescue ArgumentError
puts "Ce n'est pas un id correct"
nil
end
end
end
class Task
attr_reader :description, :done
def initialize(description)
@description = description
@done = false
end
def mark_as_done
@done = true
end
end
class TaskRepository
attr_reader :tasks
def initialize
@tasks = [] # Stocke instances de class Task
end
def add_task(task)
@tasks << task
end
end
@emiliecoudrat
Copy link

yihhhhaaaa!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment