Code Monkey home page Code Monkey logo

batch_513_cookbook_day_one's Introduction

Background and Objectives

You are now going to code a Cookbook application that manages recipes.

The idea is quite simple: you love cooking, but you need to remember all the recipes you like. This is your cookbook! It'll keep a list of your recipes, allowing you to list them, add new recipes and delete others.

You will build this app using the MVC pattern, also used in Rails:

  • Model: what is the basic object you want to manipulate?
  • View: this is the place where we display information to the user (puts) and ask for information from the user (gets)
  • Controller: it will fetch and store data of the Model, and tell the view to show or gather data to and from the user.

Please start with a paper and pen to identify your components and their responsibilities.

Specs

Model

Luckily, we already defined our Recipe class in the previous exercise. Now all we need to do is copy that into our cookbook app. To do so, copy this command into your terminal:

cp ../01-Recipe/lib/recipe.rb lib

This is copying the recipe.rb file from the previous exercise, into the lib folder in our cookbook app.

Repository

We now need a structure to store our user's recipes. We don't have a proper database yet, so we will use a class that acts like one (as we saw in the lecture). When a Ruby program exits, we lose all the data that we stored in variables. If we want to retrieve the data next time we run the program, we need to persist them, on the hard drive. We'll use a CSV file for that! The file is empty as this time of the challenge, you'll add your own recipes later through the app.

In the context of this challenge, the repository stores the recipes added by the user. In other words, it is the cookbook. Let's name the class Cookbook to write explicit and meaningful code, but keep in mind that it's the repository from this morning's diagram!

Implement the Cookbook class with 4 methods:

  • initialize(csv_file_path) which loads existing Recipe from the CSV
  • all which returns all the recipes
  • add_recipe(recipe) which adds a new recipe to the cookbook
  • remove_recipe(recipe_index) which removes a recipe from the cookbook.

Controller

The controller will gather data from the cookbook to hand them over to the view. It will also ask the view for information to create new recipes. Here are the methods to implement:

  • initialize(cookbook) takes an instance of the Cookbook as an argument.
  • list all the recipes
  • create a new recipe
  • destroy an existing recipe

View

The view is responsible for all the puts and gets of your MVC. Make sure you never have those words anywhere else! (except maybe for debugging)

Tying it all together

When you are ready, you can test your program with:

ruby lib/app.rb

We give you the app.rb that requires the code to instantiate a Cookbook, Controller and start the app. The infinite loop is given in the Router because this is not part of MVC. In fact, when you'll work with Rails, this will all be taken for granted and done for you. Which is nice ๐Ÿ˜‰

Extra Reading

The following concepts are also important in Software Architecture:

batch_513_cookbook_day_one's People

Contributors

robertobarros avatar

Stargazers

William Lapa avatar

Watchers

 avatar James Cloos avatar

Forkers

pinhojtop

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.