Advertisement
Guest User

Untitled

a guest
Jul 17th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.81 KB | None | 0 0
  1. # Requiring it because Rake tests controllers
  2. # individually
  3. require_relative 'view'
  4. require_relative 'recipe'
  5.  
  6. class Controller
  7. def initialize(cookbook)
  8. @cookbook = cookbook
  9. @view = View.new
  10. end
  11.  
  12. # CRUD: Read
  13. # List recipes
  14. def list
  15. show_recipes
  16. end
  17.  
  18.  
  19. # CRUD: Create
  20. def create
  21. name = @view.ask_for_name
  22. description = @view.ask_for_description
  23. recipe = Recipe.new(name, description)
  24.  
  25. @cookbook.add_recipe(recipe)
  26. end
  27.  
  28.  
  29. # CRUD: Delete
  30. def destroy
  31. show_recipes # => calling the private method
  32.  
  33. recipe_index = @view.delete_recipe
  34.  
  35. @cookbook.remove_recipe(recipe_index)
  36. end
  37.  
  38. private
  39. # defining a method that has a common behavior
  40. # so we can reuse it inside other methods
  41. def show_recipes
  42. recipes = @cookbook.all
  43. @view.list_recipes(recipes)
  44. end
  45. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement