Created
July 17, 2017 13:17
-
-
Save luizfonseca/26b32556120ae3ea69c75fa7a9853e9c to your computer and use it in GitHub Desktop.
01 - Cookbook
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
# Requiring it because Rake tests controllers | |
# individually | |
require_relative 'view' | |
require_relative 'recipe' | |
class Controller | |
def initialize(cookbook) | |
@cookbook = cookbook | |
@view = View.new | |
end | |
# CRUD: Read | |
# List recipes | |
def list | |
show_recipes | |
end | |
# CRUD: Create | |
def create | |
name = @view.ask_for_name | |
description = @view.ask_for_description | |
recipe = Recipe.new(name, description) | |
@cookbook.add_recipe(recipe) | |
end | |
# CRUD: Delete | |
def destroy | |
show_recipes # => calling the private method | |
recipe_index = @view.delete_recipe | |
@cookbook.remove_recipe(recipe_index) | |
end | |
private | |
# defining a method that has a common behavior | |
# so we can reuse it inside other methods | |
def show_recipes | |
recipes = @cookbook.all | |
@view.list_recipes(recipes) | |
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
# This class is the repository, | |
# where we store things and define methods | |
# related to storing/retrieving | |
require 'csv' | |
require_relative 'recipe' | |
class Cookbook | |
# This method initializes with a path | |
# to a csv file (see router.rb) | |
def initialize(filepath) | |
@recipes = [] | |
@filepath = filepath | |
# At the start, we get all recipes from CSV | |
# and transform them to Recipe objects | |
# So listing them works just fine | |
CSV.foreach(@filepath) do |row| | |
@recipes << Recipe.new(row[0], row[1]) | |
end | |
end | |
# Just return all recipes (instance method) | |
def all | |
@recipes | |
end | |
# Add a recipe to the Recipes Array | |
def add_recipe(recipe) | |
@recipes << recipe | |
# This method saves the recipe to the CSV | |
# after we add it to the array | |
write_recipes # reusing a common instruction | |
end | |
# Remove a recipe from the Recipes Array | |
# and update the CSV afterwards | |
def remove_recipe(recipe_index) | |
@recipes.delete_at(recipe_index) | |
write_recipes # reusing a common instruction | |
end | |
private | |
# Common instruction: it updates the CSV | |
# | |
def write_recipes | |
CSV.open(@filepath, 'wb') do |csv| | |
@recipes.each do |recipe| | |
csv << [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 | |
# define that :name and :description | |
# can be read, but no written | |
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
# This class is responsible for only: | |
# Displaying (puts) and | |
# Receiving (gets) user inputs/outputs | |
# ITS only related to recipes controller | |
class View | |
def list_recipes(recipes) | |
if recipes.empty? | |
puts "You have no recipes!!" | |
else | |
recipes.each_with_index do |recipe, index| | |
puts "#{index + 1} - #{recipe.name} (#{recipe.description})" | |
end | |
end | |
end | |
def ask_for_name | |
puts "What's the name of your recipe?" | |
print "> " | |
gets.chomp | |
end | |
def ask_for_description | |
puts "What's the description of your recipe?" | |
print "> " | |
gets.chomp | |
end | |
def delete_recipe | |
puts "What recipe do you want to delete?" | |
print "> " | |
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