Skip to content

Instantly share code, notes, and snippets.

@jonny-gates
Created October 20, 2019 16:49
Show Gist options
  • Save jonny-gates/fe524857625445d6e03bc7f39920340f to your computer and use it in GitHub Desktop.
Save jonny-gates/fe524857625445d6e03bc7f39920340f to your computer and use it in GitHub Desktop.
cookbook-day-1-recap
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)
@cookbook = cookbook
@view = View.new
end
def list
display_recipes
end
def create
# 1. Ask the user for the name
name = @view.ask_user_for('name')
# 2. Ask the user for the description
description = @view.ask_user_for('description')
# 3. Create a recipe
recipe = Recipe.new(name, description)
# 4. Store the recipe in the cookbook
@cookbook.add_recipe(recipe)
end
def destroy
# 1. List recipes
display_recipes
# 2. Ask the for user for index
recipe_index = @view.ask_user_for_index
# 3. Tell the cookbook to delete the recipe at index
@cookbook.remove_recipe(recipe_index)
end
private
def display_recipes
# 1. Ask the cookbook for the recipes
recipes = @cookbook.all
# 2. Ask the view to print the recipes
@view.list_recipes(recipes)
end
end
require 'csv'
require_relative 'recipe'
class Cookbook
def initialize(csv_path)
@recipes = []
@csv_path = csv_path
load_csv
end
def all
return @recipes
end
def add_recipe(recipe)
@recipes << recipe
save_csv
end
def remove_recipe(recipe_index)
@recipes.delete_at(recipe_index)
save_csv
end
private
def save_csv
CSV.open(@csv_path, 'wb') do |csv|
@recipes.each do |recipe|
csv << [recipe.name, recipe.description]
end
end
end
def load_csv
CSV.foreach(@csv_path) 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
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 list_recipes(recipes)
recipes.each_with_index do |recipe, index|
puts "#{index + 1}. #{recipe.name}"
end
end
def ask_user_for(attribute)
puts "Please enter a #{attribute}"
return gets.chomp
end
def ask_user_for_index
puts "Which number"
return 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