In this assignment we will be creating an application to look up Farmers Markets and their related vendors, products, and sales. We will use CSV files as our database.
Richa Arora
Kathy Lepe
- Pair up!
- The individual closest to the center of the room will fork the repo: https://github.com/Ada-Developers-Academy/far_mar
- Add the second individual as a collaborator
- Both individuals will clone the repo
git clone [YOUR FORKED REPO URL]
- Both individuals cd into the dir created
cd far_mar
- Both individuals install needed tools (in Terminal.app)
gem install rspec
gem install simplecov
# this updates the global settings for rspec to give output in color
# you only need to do it once, and it won't give you any type of confirmation
echo --color >> ~/.rspec
Start building! (but read on for more info)
There are several CSV data files:
/support/markets.csv
/support/products.csv
/support/vendors.csv
/support/sales.csv
Build classes to query the CSV data including objects and methods listed below. Before going too deeply into the methods listed, start by building a system to read the csv files and turn each row of data into an instance of the corresponding ruby class.
To manage our data classes we will use a file named /lib/far_mar.rb
require 'csv'
require 'time'
require 'lib/market'
# ... require all needed classes
Each of the classes you build will be nested under the FarMar
module.
module FarMar
class Market
# Your code goes here
end
end
All this nesting does is name spacing. This is different from inheritance, all this is doing is making the classes we make not collide with a class that another gem might use.
For example Sale
could easily be defined in many other code bases, but since we namespace is as FarMar::Sale
it is now more uniquely named.
Our supporting classes will also live in the /lib/far_mar
dir.
The FarMar::Market data, in order in the CSV, consists of:
- ID
- Name
- Address
- City
- County
- State
- Zip
An individual market has many vendors
The FarMar::Vendor data, in order in the CSV, consists of:
- ID
- Name
- No. of Employees
- Market_id
Each vendor belongs to a market, the market_id field relates to the FarMar::Market ID field. Each vendor has many products they sell.
The FarMar::Product data, in order in the CSV, consists of:
- ID
- Name
- Vendor_id
Each product belongs to a vendor, the vendor_id field relates to the FarMar::Vendor ID field.
The FarMar::Sale data, in order in the CSV, consists of:
- ID
- Amount (in cents)
- Purchase Time
- Vendor_id
- Product_id
Each sale belongs to a vendor AND an product, the vendor_id and product_id fields relates to the FarMar::Vendor and FarMar::Product ID fields
For each of the data classes build the following methods:
self.all
- returns all rows of the CSV file as objectsself.find(id)
- returns the row where the ID field matches the argument
Additional FarMar::Market Methods
vendors
- returns a collection ofFarMar::Vendor
instances that are associated with the market by the market_id field.
Additional FarMar::Vendor Methods
market
- returns theFarMar::Market
instance that is associated with this vendor using theFarMar::Vendor
market_id
fieldproducts
- returns a collection ofFarMar::Product
instances that are associated with market by theFarMar::Product
vendor_id
field.sales
- returns a collection ofFarMar::Sale
instances that are associated with market by thevendor_id
field.revenue
- returns the the sum of all of the vendor's sales (in cents)self.by_market(market_id)
- returns all of the vendors with the givenmarket_id
Additional FarMar::Product Methods
vendor
- returns theFarMar::Vendor
instance that is associated with this vendor using theFarMar::Product
vendor_id
fieldsales
- returns a collection ofFarMar::Sale
instances that are associated with market using theFarMar::Sale
product_id
field.number_of_sales
- returns the number of times this product has been sold.self.by_vendor(vendor_id)
- returns all of the products with the givenvendor_id
Additional FarMar::Sale Methods
vendor
- returns theFarMar::Vendor
instance that is associated with this sale using theFarMar::Sale
vendor_id
fieldproduct
- returns theFarMar::Product
instance that is associated with this sale using theFarMar::Sale
product_id
fieldself.between(beginning_time, end_time)
- returns a collection of FarMar::Sale objects where the purchase time is between the two times given as arguments
Silver FarMar::Market Methods
products
returns a collection ofFarMar::Product
instances that are associated to the market through theFarMar::Vendor
class.self.search(search_term)
returns a collection ofFarMar::Market
instances where the market name or vendor name contain thesearch_term
. For exampleFarMar::Market.search('school')
would return 3 results, one being the market with id 75 (Fox School Farmers FarMar::Market).prefered_vendor
- returns the vendor with the highest revenueprefered_vendor(date)
- returns the vendor with the highest revenue for the given dateworst_vendor
- returns the vendor with the lowest revenueworst_vendor(date)
- returns the vendor with the lowest revenue on the given date
Silver FarMar::Vendor Methods
self.most_revenue(n)
returns the top n vendor instances ranked by total revenueself.most_items(n)
returns the top n vendor instances ranked by total number of items soldself.revenue(date)
returns the total revenue for that date across all vendorsrevenue(date)
returns the total revenue for that specific purchase date and vendor instance
Silver FarMar::Product Methods
self.most_revenue(n)
returns the top n products instances ranked by total revenue
Gold on All Classes
self.find_by_x(match)
- where X is an attribute, returns a single instance whose X attribute case-insensitive attribute matches the match parameter. For instance, FarMar::Vendor.find_by_name("windler inc") could find a FarMar::Vendor with the name attribute "windler inc" or "Windler Inc".self.find_all_by_x(match)
- works just likefind_by_x
but returns a collection containing all possible matches. For exampleFarMar::Market.find_by_state("WA")
could return all of the FarMar::Market object with"WA"
in their state field.
Create an additional class to use inheritance for shared methods (find
, all
).
To run our test suite use:
rspec
Our test suite will run tests to ensure that some of our required methods are implemented correctly.
Note: It will be best to create the basic file for the four supporting classes before running our tests
class FarMar::Market
end
and requiring them in /lib/far_mar.rb
.
You will be evaluated on:
- Running
rspec
from Terminal with no failures - Code quality (is your code organized and logical)
- Extra credit for additional methods to add neat functionality
- Extra credit for writing additional specs