Created
July 19, 2019 22:00
-
-
Save Martin-Alexander/569d8cb1b5d666eca8b7833d49a4f0b0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require_relative "view" | |
require_relative "recipe" | |
class Controller | |
def initialize(cookbook) | |
@view = View.new | |
@cookbook = cookbook | |
end | |
def list | |
# Ask the repo for all the recipes | |
recipes = @cookbook.all | |
# Tell to the view to display them all | |
@view.display_recipes(recipes) | |
end | |
def create | |
# Tell the view to ask the user for the name and description | |
recipe_name = @view.ask_for_name | |
recipe_description = @view.ask_for_description | |
# Create a recipe object | |
new_recipe = Recipe.new(recipe_name, recipe_description) | |
# Give it to the repo to store it | |
@cookbook.add_recipe(new_recipe) | |
end | |
def destroy | |
list | |
# Tell the view to ask the user which recipe they want to delete | |
recipe_index = @view.ask_for_index | |
# Tell the repo to delete that recipe | |
@cookbook.remove_recipe(recipe_index) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "csv" | |
require_relative "recipe" | |
class Cookbook | |
def initialize(csv_file_path) | |
@csv_file_path = csv_file_path | |
@recipes = [] # Recipe instances (not arrays or hashes...) | |
CSV.foreach(@csv_file_path) do |row| | |
# row == ["cake", "eggs milk and butter"] | |
@recipes << Recipe.new(row[0], row[1]) | |
end | |
end | |
def all | |
@recipes | |
end | |
def add_recipe(new_recipe) | |
@recipes << new_recipe | |
save_to_csv | |
end | |
def remove_recipe(recipe_index) | |
@recipes.delete_at(recipe_index) | |
save_to_csv | |
end | |
private | |
def save_to_csv | |
CSV.open(@csv_file_path, "w") do |csv_file| | |
@recipes.each do |recipe| | |
csv_file << [recipe.name, recipe.description] | |
end | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Recipe | |
attr_reader :name, :description | |
def initialize(name, description) | |
@name = name | |
@description = description | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cake | eggs milk and butter | |
---|---|---|
cookies | butter flour and chocolate |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class View | |
def display_recipes(recipes) | |
recipes.each_with_index do |recipe, index| | |
puts "#{index + 1} - #{recipe.name}: #{recipe.description}" | |
end | |
end | |
def ask_for_name | |
puts "Name:" | |
recipe_name = gets.chomp | |
return recipe_name | |
end | |
def ask_for_description | |
puts "Description:" | |
recipe_description = gets.chomp | |
return recipe_description | |
end | |
def ask_for_index | |
puts "Index:" | |
recipe_index = gets.chomp.to_i - 1 | |
return recipe_index | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment