Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # 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
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement