Skip to content

Instantly share code, notes, and snippets.

@MarcelSF
Created August 1, 2020 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MarcelSF/b73e66d9aa5186d487267aa5cf9735f7 to your computer and use it in GitHub Desktop.
Save MarcelSF/b73e66d9aa5186d487267aa5cf9735f7 to your computer and use it in GitHub Desktop.
require_relative 'cookbook' # You need to create this file!
require_relative 'controller' # You need to create this file!
require_relative 'router'
csv_file = File.join(__dir__, 'recipes.csv')
cookbook = Cookbook.new(csv_file)
controller = Controller.new(cookbook)
router = Router.new(controller)
# Start the app
router.run
require_relative 'view'
require_relative 'recipe'
class Controller
def initialize(cookbook)
@view = View.new
@cookbook = cookbook
end
def create
# ask the user for recipe name
# ask user for recipe description
recipe_info = @view.ask_for_recipe
# instantiate a Recipe Object
new_recipe = Recipe.new(recipe_info[0], recipe_info[1])
# store the recipe object in the Repository
@cookbook.add_recipe(new_recipe)
end
def list
# Ask the Cookbook for all the recipes
recipe_array = @cookbook.all
# Show them to the user
@view.display(recipe_array)
end
# List all recipes
# DElete a recipe
def destroy
# list all the recipes for the user
list
# ask the user which recipe should be deleted
recipe_index = @view.ask_for_index
# tell the repository to DELETE that recipe
@cookbook.remove_recipe(recipe_index)
end
end
require 'csv'
class Cookbook
def initialize(filepath)
@recipes = []
@filepath = filepath
load_from_csv
end
def add_recipe(recipe)
@recipes << recipe
save_to_csv
end
def all
@recipes
end
def remove_recipe(index)
@recipes.delete_at(index)
save_to_csv
end
def save_to_csv
CSV.open(@filepath, 'wb') do |csv|
@recipes.each do |recipe|
csv << [recipe.name, recipe.description]
end
end
end
def load_from_csv
CSV.foreach(@filepath) do |row|
@recipes << Recipe.new(row[0], row[1])
end
end
end
class Recipe
attr_reader :name, :description
def initialize(name, description)
@name = name
@description = description
end
end
crumble apple and cool stuff
pudim condensed milk
Pao de queijo amazing bread
class Router
def initialize(controller)
@controller = controller
@running = true
end
def run
puts "Welcome to the Cookbook!"
puts " -- "
while @running
display_tasks
action = gets.chomp.to_i
print `clear`
route_action(action)
end
end
private
def route_action(action)
case action
when 1 then @controller.list
when 2 then @controller.create
when 3 then @controller.destroy
when 4 then stop
else
puts "Please press 1, 2, 3 or 4"
end
end
def stop
@running = false
end
def display_tasks
puts ""
puts "What do you want to do next?"
puts "1 - List all recipes"
puts "2 - Create a new recipe"
puts "3 - Destroy a recipe"
puts "4 - Stop and exit the program"
end
end
class View
def ask_for_recipe
puts "Whats the name of your recipe?"
name = gets.chomp
puts "What's the description of your recipe?"
description = gets.chomp
return [name, description]
end
def display(recipe_array)
recipe_array.each_with_index do |recipe, index|
puts "#{index + 1} - #{recipe.name}: #{recipe.description}"
end
end
def ask_for_index
puts "What is the number of the recipe to be discarded?"
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