Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FarMar - Primary + Optionals #44

Open
wants to merge 29 commits into
base: KED/master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
36361d9
added #all method to Market class
kedevlin Oct 19, 2015
e35490f
Added #all method to Product class
kedevlin Oct 20, 2015
699b2e6
Added #all method to Sale Class
kedevlin Oct 20, 2015
817c020
added #all method to Vendor class
kedevlin Oct 20, 2015
2ae33ed
Added #find(id) method to Vendor class
kedevlin Oct 20, 2015
242b8ac
Added #find(id) method to Sales class
kedevlin Oct 20, 2015
f948040
added #find(id) to Product class
kedevlin Oct 20, 2015
98741b8
Added #find(id) to Market class
kedevlin Oct 20, 2015
38be600
Added .vendors method to Market class
kedevlin Oct 21, 2015
6776c61
added .market method to Vendor class
kedevlin Oct 21, 2015
f2f646a
Added .products method to Vendor class
kedevlin Oct 21, 2015
42524d9
added .sales method to Vendor class
kedevlin Oct 21, 2015
d52c4aa
Added .revenue method to Vendor class
kedevlin Oct 21, 2015
ed7a850
Added #by_market(market_id) method to Vendor class
kedevlin Oct 21, 2015
b83728a
added .vendor method to Product class
kedevlin Oct 21, 2015
3abaabe
Updated #all method for each class to use class variable
kedevlin Oct 21, 2015
422e5d9
Added .sales and .number_of_sales methods to Product class
kedevlin Oct 21, 2015
a9aa67e
added #by_vendor method to Product class
kedevlin Oct 21, 2015
311626e
Added .vendor method to Sale class
kedevlin Oct 21, 2015
b4368bf
Added #between method to Sale class
kedevlin Oct 21, 2015
660d15e
added optional .products method to Market class
kedevlin Oct 22, 2015
98b6a01
Added optional #search method to Market class
kedevlin Oct 22, 2015
8536039
Same as prior commit with uncommented tests in Market_Spec
kedevlin Oct 22, 2015
1e0e340
added optional .preferred_vendor method to Market class
kedevlin Oct 22, 2015
c4f3697
added optional .preferred_vendor method to Market class
kedevlin Oct 23, 2015
e45c49c
updated .revenue method in Vendor class to accept dates, updated .pre…
kedevlin Oct 23, 2015
85c708f
updated spec docs to use best practices
kedevlin Oct 23, 2015
d59b136
fixed indent errors
kedevlin Oct 23, 2015
ee340f2
revised all 4 classes
kedevlin Oct 23, 2015
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 61 additions & 1 deletion lib/far_mar/Market.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,66 @@
require './lib/far_mar'
require 'pry'
module FarMar
class Market

attr_reader :id, :name, :address, :city, :county, :state, :zip
def initialize(id, name, address, city, county, state, zip)
@id = id.to_i
@name = name
@address = address
@city = city
@county = county
@state = state
@zip = zip
end
def self.all
@@markets_all ||= []
if @@markets_all == []
CSV.read("./support/markets.csv").map do |row|
@@markets_all.push(FarMar::Market.new(row[0], row[1], row[2], row[3], row[4], row[5], row[6]))
end
end
return @@markets_all
end
def self.find(id)
FarMar::Market.all.find do |market|
market.id == id
end
end
def vendors
FarMar::Vendor.all.find_all do |vendor|
self.id == vendor.market_id
end
end
def products
products = []
vendors.map do |vendor|
vendor.products.map do |product|
products.push(product)
end
end
return products
end
def self.search(search_term)
search_term = search_term.downcase
matching_markets = []
all.each do |market|
matching_markets.push(market) if (market.name.downcase.include? search_term) && !(matching_markets.include? market)
end
FarMar::Vendor.all.each do |vendor|
matching_markets.push(vendor.market) if (vendor.name.downcase.include? search_term) && !(matching_markets.include? vendor.market)
end
return matching_markets
end
def preferred_vendor(date = nil)
if date == nil
vendors.max_by do |vendor|
vendor.revenue
end
else
vendors.max_by do |vendor|
vendor.revenue(date)
end
end
end
end
end
39 changes: 38 additions & 1 deletion lib/far_mar/Product.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,43 @@
require './lib/far_mar'
module FarMar
class Product

attr_reader :id, :name, :vendor_id
def initialize(id, name, vendor_id)
@id = id.to_i
@name = name
@vendor_id = vendor_id.to_i
end
def self.all
@@products_all ||= []
if @@products_all == []
CSV.read("./support/products.csv").map do |row|
@@products_all.push(self.new(row[0], row[1], row[2]))
end
end
return @@products_all
end
def self.find(id)
all.find do |product|
product.id == id
end
end
def vendor
Vendor.all.find do |vendor|
vendor.id == self.vendor_id
end
end
def sales
Sale.all.find_all do |sale|
sale.product_id == self.id
end
end
def number_of_sales
sales.length
end
def self.by_vendor(vendor_id)
self.all.find_all do |product|
product.vendor_id == vendor_id
end
end
end
end
40 changes: 39 additions & 1 deletion lib/far_mar/Sale.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,44 @@
require './lib/far_mar'
module FarMar
class Sale

attr_reader :id, :amount, :purchase_time, :vendor_id, :product_id
def initialize(id, amount, purchase_time, vendor_id, product_id)
@id = id.to_i
@amount = amount.to_i
@purchase_time = DateTime.strptime(purchase_time, "%Y-%m-%d %H:%M:%S %z")
@vendor_id = vendor_id.to_i
@product_id = product_id.to_i
end
def self.all
@@sales_all ||= []
if @@sales_all == []
CSV.read("./support/sales.csv").map do |row|
@@sales_all.push(FarMar::Sale.new(row[0], row[1], row[2], row[3], row[4]))
end
end
return @@sales_all
end
def self.find(id)
all.find do |sale|
sale.id == id
end
end
def vendor
Vendor.all.find do |vendor|
vendor.id == self.vendor_id
end
end
def product
Product.all.find do |product|
product.id == self.product_id
end
end
def self.between(beginning_time, end_time)
beginning_time = DateTime.strptime(beginning_time, "%Y-%m-%d %H:%M:%S %z")
end_time = DateTime.strptime(end_time, "%Y-%m-%d %H:%M:%S %z")
self.all.find_all do |sale|
sale.purchase_time > beginning_time && sale.purchase_time < end_time
end
end
end
end
58 changes: 57 additions & 1 deletion lib/far_mar/Vendor.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,62 @@
require './lib/far_mar'
module FarMar
class Vendor

attr_reader :id, :name, :num_employees, :market_id
def initialize(id, name, num_employees, market_id)
@id = id.to_i
@name = name
@num_employees = num_employees.to_i
@market_id = market_id.to_i
end
def self.all
@@vendors_all ||= []
if @@vendors_all == []
CSV.read("./support/vendors.csv").map do |row|
@@vendors_all.push(FarMar::Vendor.new(row[0], row[1], row[2], row[3]))
end
end
return @@vendors_all
end
def self.find(id)
all.find do |vendor|
vendor.id == id
end
end
def market
Market.all.find do |market|
market.id == self.market_id
end
end
def products
Product.all.find_all do |product|
product.vendor_id == self.id
end
end
def sales
Sale.all.find_all do |sale|
sale.vendor_id == self.id
end
end
def revenue(date = nil)
if date == nil
amounts = self.sales.map do |sale|
sale.amount
end
else
date = DateTime.parse(date)
amounts = []
sales.each do |sale|
if sale.purchase_time.to_date == date.to_date
amounts.push(sale.amount)
end
end
end
amounts.inject(0) { |result, sale| result + sale }
end
def self.by_market(market_id)
self.all.find_all do |vendor|
market_id == vendor.market_id
end
end
end
end
67 changes: 66 additions & 1 deletion spec/far_mar/Market_spec.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,76 @@
require 'spec_helper'
require 'pry'
describe FarMar::Market do
before :each do
@market = FarMar::Market.new
@market = FarMar::Market.new("500", "Montefiore Medical Center Farmers Market_Thursday", "111 E. 210th Street", "Bronx", "Bronx", "New York", "10467")
end
describe "#new" do
it "creates a new Market instance" do
expect(@market).to be_an_instance_of FarMar::Market
end
end
describe "#all" do
it "creates new Market instances" do
expect(FarMar::Market.all[0].id).to eq 1
end
it "returns a collection representing all the markets in the CSV" do
expect(FarMar::Market.all.length).to eq 500
end
end
describe "#find(id)" do
it "returns the market instance with an id matching the parameter" do
expect(FarMar::Market.find(9).id).to eq 9
end
end
describe ".vendors" do
it "returns collection of all vendor instances with market_id matching market instance's id" do
expect(@market.vendors.length).to eq 10
end
it "returns a collection of vendor instances" do
expect(@market.vendors[0].class).to eq FarMar::Vendor
end
end
describe ".products" do
it "returns a collection of product instances associated through vendors" do
#vendor ids: 2681, 2682, 2683, 2684, 2685, 2686, 2687, 2688, 2689, 2690
expect(@market.products.length).to eq 34
end
end
describe "#search(search_term)" do
it "returns market instances where the market name contains search_term" do
expect(FarMar::Market.search('braintree')[0].id).to eq 36
end
it "returns market instances where the vendor name contains search_term" do
expect(FarMar::Market.search("Eenhol")[1].id).to eq 21
end
it "returns all market and vendor instances where search_term is within the market or vendor name only" do
search = FarMar::Market.search("johns")
# 3 markets with "johns" in name
# 25 vendors with "johns" in name
expect(search.length).to eq 28
end
it "returns only market instances" do
search = FarMar::Market.search("johns")
incorrect_class = search.find_all do |instance|
instance.class != FarMar::Market
end
expect(incorrect_class).to eq []
end
it "returns empty array if no search results" do
expect(FarMar::Market.search("a;oeaifhgsoifgj ~~~")).to eq []
end
it "does not duplicate (if same market and vendor)" do
expect(FarMar::Market.search("").length).to eq 500
end
end
describe ".preferred_vendor" do
it "returns the vendor with the highest revenue" do
expect(@market.preferred_vendor.id).to eq 2684
end
end
describe ".preferred_vendor(date)" do
it "returns the vendor with the highest revenue for date passed in" do
expect(@market.preferred_vendor("2013-11-09").id).to eq 2687
end
end
end
36 changes: 35 additions & 1 deletion spec/far_mar/Product_spec.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,45 @@
require 'spec_helper'
describe FarMar::Product do
before :each do
@product = FarMar::Product.new
@product = FarMar::Product.new("8188", "Brief Carrots", "2689")
end
describe "#new" do
it "creates a new Product instance" do
expect(@product).to be_an_instance_of FarMar::Product
end
end
describe "#all" do
it "creates new Product instances" do
expect(FarMar::Product.all[0].name).to eq "Dry Beets"
end
it "returns a collection representing all the products in the CSV" do
expect(FarMar::Product.all.length).to eq 8193
end
end
describe "#find(id)" do
it "returns the product instance with an id matching the parameter" do
expect(FarMar::Product.find(14).id).to eq 14
end
end
describe ".vendor" do
it "returns the vendor instance whose id matches the product's vendor_id" do
expect(@product.vendor.id).to eq 2689
end
end
describe ".sales" do
it "returns a collection of sales instances whose product_id matches the product's id" do
expect(@product.sales.length).to eq 2
end
end
describe ".number_of_sales" do
it "returns number of sales instances whose product_id matches the product's id" do
expect(@product.number_of_sales).to eq 2
end
end
describe "#by_vendor(vendor_id)" do
it "returns all product instances whose vendor_id matches the parameter passed in" do
vendor = FarMar::Product.by_vendor(2672)
expect(vendor.length).to eq 4
end
end
end
Loading