From 36361d9d31b15b71003b4cd6cdb15602942b6317 Mon Sep 17 00:00:00 2001 From: Kelly Date: Mon, 19 Oct 2015 16:27:19 -0700 Subject: [PATCH 01/29] added #all method to Market class --- lib/far_mar/Market.rb | 23 ++++++++++++++++++++++- spec/far_mar/Market_spec.rb | 10 +++++++++- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/lib/far_mar/Market.rb b/lib/far_mar/Market.rb index f804f194..2c6c0952 100644 --- a/lib/far_mar/Market.rb +++ b/lib/far_mar/Market.rb @@ -1,6 +1,27 @@ require './lib/far_mar' module FarMar class Market - + attr_reader :id, :name, :address, :city, :county, :state, :zip + def initialize(id, name, address, city, county, state, zip) + #fixnum; unique + @id = id.to_i + #string + @name = name + #string + @address = address + #string + @city = city + #string + @county = county + #string + @state = state + #string + @zip = zip + end + def self.all + CSV.read("./support/markets.csv").map do |row| + FarMar::Market.new(row[0], row[1], row[2], row[3], row[4], row[5], row[6]) + end + end end end diff --git a/spec/far_mar/Market_spec.rb b/spec/far_mar/Market_spec.rb index 53003bca..7c27ee7e 100644 --- a/spec/far_mar/Market_spec.rb +++ b/spec/far_mar/Market_spec.rb @@ -1,11 +1,19 @@ require 'spec_helper' describe FarMar::Market do before :each do - @market = FarMar::Market.new + @market = FarMar::Market.new("1", "People's Co-op Farmers Market", "30th and Burnside", "Portland", "Multnomah", "Oregon", "97202") 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 end From e35490f850b8dfc30540bbe20b2d8771b1f2b6b2 Mon Sep 17 00:00:00 2001 From: Kelly Date: Tue, 20 Oct 2015 14:22:21 -0700 Subject: [PATCH 02/29] Added #all method to Product class --- lib/far_mar/Product.rb | 12 +++++++++++- spec/far_mar/Product_spec.rb | 10 +++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/lib/far_mar/Product.rb b/lib/far_mar/Product.rb index 5377c1bd..b6965384 100644 --- a/lib/far_mar/Product.rb +++ b/lib/far_mar/Product.rb @@ -1,6 +1,16 @@ 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 + CSV.read("./support/products.csv").map do |row| + FarMar::Product.new(row[0], row[1], row[2]) + end + end end end diff --git a/spec/far_mar/Product_spec.rb b/spec/far_mar/Product_spec.rb index 32cf098d..b4171d22 100644 --- a/spec/far_mar/Product_spec.rb +++ b/spec/far_mar/Product_spec.rb @@ -1,11 +1,19 @@ require 'spec_helper' describe FarMar::Product do before :each do - @product = FarMar::Product.new + @product = FarMar::Product.new("1", "Dry", "Beets") 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 end From 699b2e60f808a7c19bcb600b1ef6e81fe8a2fdf1 Mon Sep 17 00:00:00 2001 From: Kelly Date: Tue, 20 Oct 2015 14:47:38 -0700 Subject: [PATCH 03/29] Added #all method to Sale Class --- lib/far_mar/Sale.rb | 14 +++++++++++++- spec/far_mar/Sale_spec.rb | 10 +++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/lib/far_mar/Sale.rb b/lib/far_mar/Sale.rb index 9f967839..b1fe8110 100644 --- a/lib/far_mar/Sale.rb +++ b/lib/far_mar/Sale.rb @@ -1,6 +1,18 @@ 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 + CSV.read("./support/sales.csv").map do |row| + FarMar::Sale.new(row[0], row[1], row[2], row[3], row[4]) + end + end end end diff --git a/spec/far_mar/Sale_spec.rb b/spec/far_mar/Sale_spec.rb index 70ba0c60..fc65029e 100644 --- a/spec/far_mar/Sale_spec.rb +++ b/spec/far_mar/Sale_spec.rb @@ -1,11 +1,19 @@ require 'spec_helper' describe FarMar::Sale do before :each do - @sale = FarMar::Sale.new + @sale = FarMar::Sale.new("1", "9290", "2013-11-07 04:34:56 -0800", "1", "1") end describe "#new" do it "creates a new Sale instance" do expect(@sale).to be_an_instance_of FarMar::Sale end end + describe "#all" do + it "creates new Sale instances" do + expect(FarMar::Sale.all[1].amount).to eq 2262 + end + it "returns a collection representing all sales in the CSV" do + expect(FarMar::Sale.all.length).to eq 12798 + end + end end From 817c020cb2de95147f64d13b392ccfbd72c98d25 Mon Sep 17 00:00:00 2001 From: Kelly Date: Tue, 20 Oct 2015 15:02:08 -0700 Subject: [PATCH 04/29] added #all method to Vendor class --- lib/far_mar/Vendor.rb | 13 ++++++++++++- spec/far_mar/Vendor_spec.rb | 10 +++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/lib/far_mar/Vendor.rb b/lib/far_mar/Vendor.rb index 2b2338f1..d28429ae 100644 --- a/lib/far_mar/Vendor.rb +++ b/lib/far_mar/Vendor.rb @@ -1,6 +1,17 @@ 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 + CSV.read("./support/vendors.csv").map do |row| + FarMar::Vendor.new(row[0], row[1], row[2], row[3]) + end + end end end diff --git a/spec/far_mar/Vendor_spec.rb b/spec/far_mar/Vendor_spec.rb index ddf4476d..55f71f62 100644 --- a/spec/far_mar/Vendor_spec.rb +++ b/spec/far_mar/Vendor_spec.rb @@ -1,11 +1,19 @@ require 'spec_helper' describe FarMar::Vendor do before :each do - @vendor = FarMar::Vendor.new + @vendor = FarMar::Vendor.new("6", "Zulauf and Sons", "8", "1") end describe "#new" do it "creates a new Vendor instance" do expect(@vendor).to be_an_instance_of FarMar::Vendor end end + describe "#all" do + it "creates a new Vendor instance" do + expect(FarMar::Vendor.all[3].market_id).to eq 1 + end + it "returns a collection representing all vendors in CSV" do + expect(FarMar::Vendor.all.length).to eq 2690 + end + end end From 2ae33ed4f3940c0932dddf866451d58be5f056cb Mon Sep 17 00:00:00 2001 From: Kelly Date: Tue, 20 Oct 2015 16:06:17 -0700 Subject: [PATCH 05/29] Added #find(id) method to Vendor class --- lib/far_mar/Vendor.rb | 5 +++++ spec/far_mar/Vendor_spec.rb | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/lib/far_mar/Vendor.rb b/lib/far_mar/Vendor.rb index d28429ae..59fb9a59 100644 --- a/lib/far_mar/Vendor.rb +++ b/lib/far_mar/Vendor.rb @@ -13,5 +13,10 @@ def self.all FarMar::Vendor.new(row[0], row[1], row[2], row[3]) end end + def self.find(id) + FarMar::Vendor.all.find do |vendor| + vendor.id == id + end + end end end diff --git a/spec/far_mar/Vendor_spec.rb b/spec/far_mar/Vendor_spec.rb index 55f71f62..89fe7cb4 100644 --- a/spec/far_mar/Vendor_spec.rb +++ b/spec/far_mar/Vendor_spec.rb @@ -16,4 +16,9 @@ expect(FarMar::Vendor.all.length).to eq 2690 end end + describe "#find(id)" do + it "returns the vendor instance with an id matching the parameter" do + expect(FarMar::Vendor.find(556).id).to eq 556 + end + end end From 242b8ac6a4a7ccdf2ae61716cf6cb18abeae83da Mon Sep 17 00:00:00 2001 From: Kelly Date: Tue, 20 Oct 2015 16:10:43 -0700 Subject: [PATCH 06/29] Added #find(id) method to Sales class --- lib/far_mar/Sale.rb | 5 +++++ spec/far_mar/Sale_spec.rb | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/lib/far_mar/Sale.rb b/lib/far_mar/Sale.rb index b1fe8110..1e000870 100644 --- a/lib/far_mar/Sale.rb +++ b/lib/far_mar/Sale.rb @@ -14,5 +14,10 @@ def self.all FarMar::Sale.new(row[0], row[1], row[2], row[3], row[4]) end end + def self.find(id) + FarMar::Sale.all.find do |sale| + sale.id == id + end + end end end diff --git a/spec/far_mar/Sale_spec.rb b/spec/far_mar/Sale_spec.rb index fc65029e..d9202f13 100644 --- a/spec/far_mar/Sale_spec.rb +++ b/spec/far_mar/Sale_spec.rb @@ -16,4 +16,9 @@ expect(FarMar::Sale.all.length).to eq 12798 end end + describe "#find(id)" do + it "returns the sales instance with an id matching the parameter" do + expect(FarMar::Sale.find(20).id).to eq 20 + end + end end From f94804070e1080134b0f3a49682c90d343acce7c Mon Sep 17 00:00:00 2001 From: Kelly Date: Tue, 20 Oct 2015 16:22:20 -0700 Subject: [PATCH 07/29] added #find(id) to Product class --- lib/far_mar/Product.rb | 5 +++++ spec/far_mar/Product_spec.rb | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/lib/far_mar/Product.rb b/lib/far_mar/Product.rb index b6965384..83faf99a 100644 --- a/lib/far_mar/Product.rb +++ b/lib/far_mar/Product.rb @@ -12,5 +12,10 @@ def self.all FarMar::Product.new(row[0], row[1], row[2]) end end + def self.find(id) + FarMar::Product.all.find do |product| + product.id == id + end + end end end diff --git a/spec/far_mar/Product_spec.rb b/spec/far_mar/Product_spec.rb index b4171d22..341068df 100644 --- a/spec/far_mar/Product_spec.rb +++ b/spec/far_mar/Product_spec.rb @@ -16,4 +16,9 @@ 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).name).to eq "Stupendous Carrots" + end + end end From 98741b8395ad78255c6e41af27e8f7462b6d9f5d Mon Sep 17 00:00:00 2001 From: Kelly Date: Tue, 20 Oct 2015 16:27:42 -0700 Subject: [PATCH 08/29] Added #find(id) to Market class --- lib/far_mar/Market.rb | 12 +++++------- spec/far_mar/Market_spec.rb | 5 +++++ spec/far_mar/Product_spec.rb | 2 +- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/lib/far_mar/Market.rb b/lib/far_mar/Market.rb index 2c6c0952..87299a20 100644 --- a/lib/far_mar/Market.rb +++ b/lib/far_mar/Market.rb @@ -3,19 +3,12 @@ module FarMar class Market attr_reader :id, :name, :address, :city, :county, :state, :zip def initialize(id, name, address, city, county, state, zip) - #fixnum; unique @id = id.to_i - #string @name = name - #string @address = address - #string @city = city - #string @county = county - #string @state = state - #string @zip = zip end def self.all @@ -23,5 +16,10 @@ def self.all FarMar::Market.new(row[0], row[1], row[2], row[3], row[4], row[5], row[6]) end end + def self.find(id) + FarMar::Market.all.find do |market| + market.id == id + end + end end end diff --git a/spec/far_mar/Market_spec.rb b/spec/far_mar/Market_spec.rb index 7c27ee7e..439686d5 100644 --- a/spec/far_mar/Market_spec.rb +++ b/spec/far_mar/Market_spec.rb @@ -16,4 +16,9 @@ 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 end diff --git a/spec/far_mar/Product_spec.rb b/spec/far_mar/Product_spec.rb index 341068df..ae5ebb37 100644 --- a/spec/far_mar/Product_spec.rb +++ b/spec/far_mar/Product_spec.rb @@ -18,7 +18,7 @@ end describe "#find(id)" do it "returns the product instance with an id matching the parameter" do - expect(FarMar::Product.find(14).name).to eq "Stupendous Carrots" + expect(FarMar::Product.find(14).id).to eq 14 end end end From 38be60047126abc07f8ba7c3d42669c5214ea990 Mon Sep 17 00:00:00 2001 From: Kelly Date: Wed, 21 Oct 2015 10:19:37 -0700 Subject: [PATCH 09/29] Added .vendors method to Market class --- lib/far_mar/Market.rb | 5 +++++ spec/far_mar/Market_spec.rb | 10 +++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/far_mar/Market.rb b/lib/far_mar/Market.rb index 87299a20..43935258 100644 --- a/lib/far_mar/Market.rb +++ b/lib/far_mar/Market.rb @@ -21,5 +21,10 @@ def self.find(id) market.id == id end end + def vendors + FarMar::Vendor.all.find_all do |vendor| + self.id == vendor.market_id + end + end end end diff --git a/spec/far_mar/Market_spec.rb b/spec/far_mar/Market_spec.rb index 439686d5..37b03717 100644 --- a/spec/far_mar/Market_spec.rb +++ b/spec/far_mar/Market_spec.rb @@ -1,7 +1,7 @@ require 'spec_helper' describe FarMar::Market do before :each do - @market = FarMar::Market.new("1", "People's Co-op Farmers Market", "30th and Burnside", "Portland", "Multnomah", "Oregon", "97202") + @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 @@ -21,4 +21,12 @@ 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 end From 6776c614ca39d2bfdc6eedc5a4e50e80ee014d6c Mon Sep 17 00:00:00 2001 From: Kelly Date: Wed, 21 Oct 2015 10:27:09 -0700 Subject: [PATCH 10/29] added .market method to Vendor class --- lib/far_mar/Vendor.rb | 5 +++++ spec/far_mar/Vendor_spec.rb | 6 ++++++ 2 files changed, 11 insertions(+) diff --git a/lib/far_mar/Vendor.rb b/lib/far_mar/Vendor.rb index 59fb9a59..b15952ab 100644 --- a/lib/far_mar/Vendor.rb +++ b/lib/far_mar/Vendor.rb @@ -18,5 +18,10 @@ def self.find(id) vendor.id == id end end + def market + FarMar::Market.all.find do |market| + market.id == self.market_id + end + end end end diff --git a/spec/far_mar/Vendor_spec.rb b/spec/far_mar/Vendor_spec.rb index 89fe7cb4..3cf68a02 100644 --- a/spec/far_mar/Vendor_spec.rb +++ b/spec/far_mar/Vendor_spec.rb @@ -21,4 +21,10 @@ expect(FarMar::Vendor.find(556).id).to eq 556 end end + describe ".market" do + it "returns the market instance whose id matches the vendor's market_id" do + expect(@vendor.market.id).to eq 1 + expect(@vendor.market.name).to eq "People's Co-op Farmers Market" + end + end end From f2f646aff8442ee5f13aa64cecb258ea9c43b8bd Mon Sep 17 00:00:00 2001 From: Kelly Date: Wed, 21 Oct 2015 10:37:45 -0700 Subject: [PATCH 11/29] Added .products method to Vendor class --- lib/far_mar/Vendor.rb | 5 +++++ spec/far_mar/Vendor_spec.rb | 12 +++++++++--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/lib/far_mar/Vendor.rb b/lib/far_mar/Vendor.rb index b15952ab..244dd52a 100644 --- a/lib/far_mar/Vendor.rb +++ b/lib/far_mar/Vendor.rb @@ -23,5 +23,10 @@ def market market.id == self.market_id end end + def products + FarMar::Product.all.find_all do |product| + product.vendor_id == self.id + end + end end end diff --git a/spec/far_mar/Vendor_spec.rb b/spec/far_mar/Vendor_spec.rb index 3cf68a02..0e6e74e3 100644 --- a/spec/far_mar/Vendor_spec.rb +++ b/spec/far_mar/Vendor_spec.rb @@ -1,7 +1,7 @@ require 'spec_helper' describe FarMar::Vendor do before :each do - @vendor = FarMar::Vendor.new("6", "Zulauf and Sons", "8", "1") + @vendor = FarMar::Vendor.new("2689", "Durgan-Moen", "1", "500") end describe "#new" do it "creates a new Vendor instance" do @@ -23,8 +23,14 @@ end describe ".market" do it "returns the market instance whose id matches the vendor's market_id" do - expect(@vendor.market.id).to eq 1 - expect(@vendor.market.name).to eq "People's Co-op Farmers Market" + expect(@vendor.market.id).to eq 500 + expect(@vendor.market.name).to eq "Montefiore Medical Center Farmers Market_Thursday" + end + end + describe ".products" do + it "returns a collection of all product instances whose vendor_id matches the vendor's id" do + expect(@vendor.products.length).to eq 5 + expect(@vendor.products[3].class).to eq FarMar::Product end end end From 42524d9adfc1eca8fa8759d3d961253684ff7ee3 Mon Sep 17 00:00:00 2001 From: Kelly Date: Wed, 21 Oct 2015 10:46:49 -0700 Subject: [PATCH 12/29] added .sales method to Vendor class --- lib/far_mar/Vendor.rb | 5 +++++ spec/far_mar/Vendor_spec.rb | 17 +++++++++++++---- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/lib/far_mar/Vendor.rb b/lib/far_mar/Vendor.rb index 244dd52a..633ebc5a 100644 --- a/lib/far_mar/Vendor.rb +++ b/lib/far_mar/Vendor.rb @@ -28,5 +28,10 @@ def products product.vendor_id == self.id end end + def sales + FarMar::Sale.all.find_all do |sale| + sale.vendor_id == self.id + end + end end end diff --git a/spec/far_mar/Vendor_spec.rb b/spec/far_mar/Vendor_spec.rb index 0e6e74e3..8cc8d96a 100644 --- a/spec/far_mar/Vendor_spec.rb +++ b/spec/far_mar/Vendor_spec.rb @@ -23,14 +23,23 @@ end describe ".market" do it "returns the market instance whose id matches the vendor's market_id" do - expect(@vendor.market.id).to eq 500 - expect(@vendor.market.name).to eq "Montefiore Medical Center Farmers Market_Thursday" + market = @vendor.market + expect(market.id).to eq 500 + expect(market.name).to eq "Montefiore Medical Center Farmers Market_Thursday" end end describe ".products" do it "returns a collection of all product instances whose vendor_id matches the vendor's id" do - expect(@vendor.products.length).to eq 5 - expect(@vendor.products[3].class).to eq FarMar::Product + product = @vendor.products + expect(product.length).to eq 5 + expect(product[3].class).to eq FarMar::Product + end + end + describe ".sales" do + it "returns a collection of all sales instances whose vendor_id matches the vendor's id" do + sale = @vendor.sales + expect(sale.length).to eq 2 + expect(sale[1].class).to eq FarMar::Sale end end end From d52c4aacfb2a8c5f91577165f9b6e43b2aeedef9 Mon Sep 17 00:00:00 2001 From: Kelly Date: Wed, 21 Oct 2015 11:02:26 -0700 Subject: [PATCH 13/29] Added .revenue method to Vendor class --- lib/far_mar/Vendor.rb | 6 ++++++ spec/far_mar/Vendor_spec.rb | 5 +++++ 2 files changed, 11 insertions(+) diff --git a/lib/far_mar/Vendor.rb b/lib/far_mar/Vendor.rb index 633ebc5a..59925f28 100644 --- a/lib/far_mar/Vendor.rb +++ b/lib/far_mar/Vendor.rb @@ -33,5 +33,11 @@ def sales sale.vendor_id == self.id end end + def revenue + amounts = self.sales.map do |sale| + sale.amount + end + amounts.inject(0) { |result, sale| result + sale } + end end end diff --git a/spec/far_mar/Vendor_spec.rb b/spec/far_mar/Vendor_spec.rb index 8cc8d96a..e39f383a 100644 --- a/spec/far_mar/Vendor_spec.rb +++ b/spec/far_mar/Vendor_spec.rb @@ -42,4 +42,9 @@ expect(sale[1].class).to eq FarMar::Sale end end + describe ".revenue" do + it "returns the sum of all amounts for sales instances whose vendor_id matches the vendor's id" do + expect(@vendor.revenue).to eq 7586 + end + end end From ed7a850247cdf5640b3ee8ba02af73c3e08d9049 Mon Sep 17 00:00:00 2001 From: Kelly Date: Wed, 21 Oct 2015 11:12:51 -0700 Subject: [PATCH 14/29] Added #by_market(market_id) method to Vendor class --- lib/far_mar/Vendor.rb | 7 ++++++- spec/far_mar/Vendor_spec.rb | 7 +++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/far_mar/Vendor.rb b/lib/far_mar/Vendor.rb index 59925f28..72d93d74 100644 --- a/lib/far_mar/Vendor.rb +++ b/lib/far_mar/Vendor.rb @@ -37,7 +37,12 @@ def revenue amounts = self.sales.map do |sale| sale.amount end - amounts.inject(0) { |result, sale| result + sale } + amounts.inject { |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 diff --git a/spec/far_mar/Vendor_spec.rb b/spec/far_mar/Vendor_spec.rb index e39f383a..c669ad7f 100644 --- a/spec/far_mar/Vendor_spec.rb +++ b/spec/far_mar/Vendor_spec.rb @@ -47,4 +47,11 @@ expect(@vendor.revenue).to eq 7586 end end + describe "#by_market(market_id)" do + it "returns all vendor instances whose market_id matches the parameter passed in" do + vendors = FarMar::Vendor.by_market(498) + expect(vendors.length).to eq 2 + expect(vendors[1].class).to eq FarMar::Vendor + end + end end From b83728ab70bc94c8540bed9da95134301d3a7967 Mon Sep 17 00:00:00 2001 From: Kelly Date: Wed, 21 Oct 2015 13:39:13 -0700 Subject: [PATCH 15/29] added .vendor method to Product class --- lib/far_mar/Product.rb | 5 +++++ lib/far_mar/Vendor.rb | 2 +- spec/far_mar/Product_spec.rb | 9 ++++++++- spec/far_mar/Vendor_spec.rb | 2 ++ 4 files changed, 16 insertions(+), 2 deletions(-) diff --git a/lib/far_mar/Product.rb b/lib/far_mar/Product.rb index 83faf99a..9da16e41 100644 --- a/lib/far_mar/Product.rb +++ b/lib/far_mar/Product.rb @@ -17,5 +17,10 @@ def self.find(id) product.id == id end end + def vendor + FarMar::Vendor.all.find do |vendor| + vendor.id == self.vendor_id + end + end end end diff --git a/lib/far_mar/Vendor.rb b/lib/far_mar/Vendor.rb index 72d93d74..c49f2333 100644 --- a/lib/far_mar/Vendor.rb +++ b/lib/far_mar/Vendor.rb @@ -37,7 +37,7 @@ def revenue amounts = self.sales.map do |sale| sale.amount end - amounts.inject { |result, sale| result + sale } + amounts.inject(0) { |result, sale| result + sale } end def self.by_market(market_id) self.all.find_all do |vendor| diff --git a/spec/far_mar/Product_spec.rb b/spec/far_mar/Product_spec.rb index ae5ebb37..002815b2 100644 --- a/spec/far_mar/Product_spec.rb +++ b/spec/far_mar/Product_spec.rb @@ -1,7 +1,7 @@ require 'spec_helper' describe FarMar::Product do before :each do - @product = FarMar::Product.new("1", "Dry", "Beets") + @product = FarMar::Product.new("8188", "Brief Carrots", "2689") end describe "#new" do it "creates a new Product instance" do @@ -21,4 +21,11 @@ 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 + vendor = @product.vendor + expect(vendor.id).to eq 2689 + expect(vendor.name).to eq "Durgan-Moen" + end + end end diff --git a/spec/far_mar/Vendor_spec.rb b/spec/far_mar/Vendor_spec.rb index c669ad7f..15a7c85e 100644 --- a/spec/far_mar/Vendor_spec.rb +++ b/spec/far_mar/Vendor_spec.rb @@ -44,7 +44,9 @@ end describe ".revenue" do it "returns the sum of all amounts for sales instances whose vendor_id matches the vendor's id" do + @vendor_0_sales = FarMar::Vendor.new("51", "Bernier Inc", "1", "12") expect(@vendor.revenue).to eq 7586 + expect(@vendor_0_sales.revenue).to eq 0 end end describe "#by_market(market_id)" do From 3abaabe31d470e2c2fcd198db86641c3f7c527a0 Mon Sep 17 00:00:00 2001 From: Kelly Date: Wed, 21 Oct 2015 14:10:59 -0700 Subject: [PATCH 16/29] Updated #all method for each class to use class variable --- lib/far_mar/Market.rb | 8 ++++++-- lib/far_mar/Product.rb | 8 ++++++-- lib/far_mar/Sale.rb | 8 ++++++-- lib/far_mar/Vendor.rb | 8 ++++++-- 4 files changed, 24 insertions(+), 8 deletions(-) diff --git a/lib/far_mar/Market.rb b/lib/far_mar/Market.rb index 43935258..32153bd1 100644 --- a/lib/far_mar/Market.rb +++ b/lib/far_mar/Market.rb @@ -12,9 +12,13 @@ def initialize(id, name, address, city, county, state, zip) @zip = zip end def self.all - CSV.read("./support/markets.csv").map do |row| - FarMar::Market.new(row[0], row[1], row[2], row[3], row[4], row[5], row[6]) + @@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| diff --git a/lib/far_mar/Product.rb b/lib/far_mar/Product.rb index 9da16e41..2b0e5ddd 100644 --- a/lib/far_mar/Product.rb +++ b/lib/far_mar/Product.rb @@ -8,9 +8,13 @@ def initialize(id, name, vendor_id) @vendor_id = vendor_id.to_i end def self.all - CSV.read("./support/products.csv").map do |row| - FarMar::Product.new(row[0], row[1], row[2]) + @@products_all ||= [] + if @@products_all == [] + CSV.read("./support/products.csv").map do |row| + @@products_all.push(FarMar::Product.new(row[0], row[1], row[2])) + end end + return @@products_all end def self.find(id) FarMar::Product.all.find do |product| diff --git a/lib/far_mar/Sale.rb b/lib/far_mar/Sale.rb index 1e000870..98cec8d2 100644 --- a/lib/far_mar/Sale.rb +++ b/lib/far_mar/Sale.rb @@ -10,9 +10,13 @@ def initialize(id, amount, purchase_time, vendor_id, product_id) @product_id = product_id.to_i end def self.all - CSV.read("./support/sales.csv").map do |row| - FarMar::Sale.new(row[0], row[1], row[2], row[3], row[4]) + @@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) FarMar::Sale.all.find do |sale| diff --git a/lib/far_mar/Vendor.rb b/lib/far_mar/Vendor.rb index c49f2333..10fb6fff 100644 --- a/lib/far_mar/Vendor.rb +++ b/lib/far_mar/Vendor.rb @@ -9,9 +9,13 @@ def initialize(id, name, num_employees, market_id) @market_id = market_id.to_i end def self.all - CSV.read("./support/vendors.csv").map do |row| - FarMar::Vendor.new(row[0], row[1], row[2], row[3]) + @@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) FarMar::Vendor.all.find do |vendor| From 422e5d911cc1927992e85b466cf09927d1301bb7 Mon Sep 17 00:00:00 2001 From: Kelly Date: Wed, 21 Oct 2015 14:28:31 -0700 Subject: [PATCH 17/29] Added .sales and .number_of_sales methods to Product class --- lib/far_mar/Product.rb | 8 ++++++++ spec/far_mar/Product_spec.rb | 11 +++++++++++ 2 files changed, 19 insertions(+) diff --git a/lib/far_mar/Product.rb b/lib/far_mar/Product.rb index 2b0e5ddd..7aaa9477 100644 --- a/lib/far_mar/Product.rb +++ b/lib/far_mar/Product.rb @@ -26,5 +26,13 @@ def vendor vendor.id == self.vendor_id end end + def sales + FarMar::Sale.all.find_all do |sale| + sale.product_id == self.id + end + end + def number_of_sales + sales.length + end end end diff --git a/spec/far_mar/Product_spec.rb b/spec/far_mar/Product_spec.rb index 002815b2..8267216d 100644 --- a/spec/far_mar/Product_spec.rb +++ b/spec/far_mar/Product_spec.rb @@ -28,4 +28,15 @@ expect(vendor.name).to eq "Durgan-Moen" 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 + expect(@product.sales[0].class).to eq FarMar::Sale + 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 end From a9aa67e77832effe428a1f03b0e36e21391e94b5 Mon Sep 17 00:00:00 2001 From: Kelly Date: Wed, 21 Oct 2015 14:41:39 -0700 Subject: [PATCH 18/29] added #by_vendor method to Product class --- lib/far_mar/Product.rb | 5 +++++ spec/far_mar/Product_spec.rb | 7 +++++++ 2 files changed, 12 insertions(+) diff --git a/lib/far_mar/Product.rb b/lib/far_mar/Product.rb index 7aaa9477..b11d69a9 100644 --- a/lib/far_mar/Product.rb +++ b/lib/far_mar/Product.rb @@ -34,5 +34,10 @@ def sales 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 diff --git a/spec/far_mar/Product_spec.rb b/spec/far_mar/Product_spec.rb index 8267216d..30d96e38 100644 --- a/spec/far_mar/Product_spec.rb +++ b/spec/far_mar/Product_spec.rb @@ -39,4 +39,11 @@ 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 + expect(vendor[3].name).to eq "Energetic Pretzel" + end + end end From 311626eb67587615d1b05a98452482d5d82f3319 Mon Sep 17 00:00:00 2001 From: Kelly Date: Wed, 21 Oct 2015 14:49:57 -0700 Subject: [PATCH 19/29] Added .vendor method to Sale class --- lib/far_mar/Sale.rb | 5 +++++ spec/far_mar/Sale_spec.rb | 8 +++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/far_mar/Sale.rb b/lib/far_mar/Sale.rb index 98cec8d2..a5772ef6 100644 --- a/lib/far_mar/Sale.rb +++ b/lib/far_mar/Sale.rb @@ -23,5 +23,10 @@ def self.find(id) sale.id == id end end + def vendor + FarMar::Vendor.all.find do |vendor| + vendor.id == self.vendor_id + end + end end end diff --git a/spec/far_mar/Sale_spec.rb b/spec/far_mar/Sale_spec.rb index d9202f13..8ed3c3a0 100644 --- a/spec/far_mar/Sale_spec.rb +++ b/spec/far_mar/Sale_spec.rb @@ -1,7 +1,7 @@ require 'spec_helper' describe FarMar::Sale do before :each do - @sale = FarMar::Sale.new("1", "9290", "2013-11-07 04:34:56 -0800", "1", "1") + @sale = FarMar::Sale.new("3339", "7316", "2013-11-11 13:56:55 -0800", "742", "2213") end describe "#new" do it "creates a new Sale instance" do @@ -21,4 +21,10 @@ expect(FarMar::Sale.find(20).id).to eq 20 end end + describe ".vendor" do + it "returns the vendor instance whose id matches the sale's vendor_id" do + expect(@sale.vendor.id).to eq 742 + expect(@sale.vendor.name).to eq "Beer, Heathcote and Leffler" + end + end end From b4368bf0b5c39479a941e344759a78c1623a2fba Mon Sep 17 00:00:00 2001 From: Kelly Date: Wed, 21 Oct 2015 15:30:27 -0700 Subject: [PATCH 20/29] Added #between method to Sale class --- lib/far_mar/Sale.rb | 12 ++++++++++++ spec/far_mar/Sale_spec.rb | 29 +++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/lib/far_mar/Sale.rb b/lib/far_mar/Sale.rb index a5772ef6..45027ea4 100644 --- a/lib/far_mar/Sale.rb +++ b/lib/far_mar/Sale.rb @@ -28,5 +28,17 @@ def vendor vendor.id == self.vendor_id end end + def product + FarMar::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 diff --git a/spec/far_mar/Sale_spec.rb b/spec/far_mar/Sale_spec.rb index 8ed3c3a0..c7daac40 100644 --- a/spec/far_mar/Sale_spec.rb +++ b/spec/far_mar/Sale_spec.rb @@ -27,4 +27,33 @@ expect(@sale.vendor.name).to eq "Beer, Heathcote and Leffler" end end + describe ".product" do + it "returns the product instance whose id matches the sales's product_id" do + expect(@sale.product.id).to eq 2213 + expect(@sale.product.name).to eq "Rotten Fruit" + end + end + describe "#between(beginning_time, end_time)"do + it "returns a collection of sales where the purchase_time is between the two parameters" do + begin_time = "2013-11-06 08:47:12 -0800" + end_time = "2013-11-06 08:52:59 -0800" + expect(FarMar::Sale.between(begin_time, end_time).length).to eq 4 + expect(FarMar::Sale.between(begin_time, end_time)[1].id).to eq 4119 + expect(FarMar::Sale.between(begin_time, end_time)[1].class).to eq FarMar::Sale + end + context "when end_time is before beginning_time" do + it "returns an empty array" do + begin_time = "2013-11-06 08:52:59 -0800" + end_time = "2013-11-06 08:47:12 -0800" + expect(FarMar::Sale.between(begin_time, end_time)).to eq [] + end + end + context "when beginning_time and end_time are the same" do + it "returns an empty array" do + begin_time = "2013-11-06 08:47:12 -0800" + end_time = "2013-11-06 08:47:12 -0800" + expect(FarMar::Sale.between(begin_time, end_time)).to eq [] + end + end + end end From 660d15e0cebf3e142569d573ae99340fafae40d4 Mon Sep 17 00:00:00 2001 From: Kelly Date: Thu, 22 Oct 2015 11:07:28 -0700 Subject: [PATCH 21/29] added optional .products method to Market class --- lib/far_mar/Market.rb | 9 +++++++++ spec/far_mar/Market_spec.rb | 8 ++++++++ 2 files changed, 17 insertions(+) diff --git a/lib/far_mar/Market.rb b/lib/far_mar/Market.rb index 32153bd1..ae6af05c 100644 --- a/lib/far_mar/Market.rb +++ b/lib/far_mar/Market.rb @@ -30,5 +30,14 @@ def vendors 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 end end diff --git a/spec/far_mar/Market_spec.rb b/spec/far_mar/Market_spec.rb index 37b03717..f2772ef6 100644 --- a/spec/far_mar/Market_spec.rb +++ b/spec/far_mar/Market_spec.rb @@ -29,4 +29,12 @@ 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 + expect(@market.products[0].class).to eq FarMar::Product + end + end + end From 98b6a01f000cf1d82b34eea2e4216306b3590b3e Mon Sep 17 00:00:00 2001 From: Kelly Date: Thu, 22 Oct 2015 14:31:46 -0700 Subject: [PATCH 22/29] Added optional #search method to Market class --- lib/far_mar/Market.rb | 16 ++++++++++++++++ spec/far_mar/Market_spec.rb | 29 ++++++++++++++++++++++++++++- support/markets.csv | 2 +- 3 files changed, 45 insertions(+), 2 deletions(-) diff --git a/lib/far_mar/Market.rb b/lib/far_mar/Market.rb index ae6af05c..36873531 100644 --- a/lib/far_mar/Market.rb +++ b/lib/far_mar/Market.rb @@ -1,4 +1,5 @@ require './lib/far_mar' +require 'pry' module FarMar class Market attr_reader :id, :name, :address, :city, :county, :state, :zip @@ -39,5 +40,20 @@ def products end return products end + def self.search(search_term) + search_term = search_term.downcase + matching_markets = [] + all.each do |market| + if (market.name.downcase.include? search_term) && !(matching_markets.include? market) + matching_markets.push(market) + end + end + FarMar::Vendor.all.each do |vendor| + if (vendor.name.downcase.include? search_term) && !(matching_markets.include? vendor.market) + matching_markets.push(vendor.market) + end + end + return matching_markets + end end end diff --git a/spec/far_mar/Market_spec.rb b/spec/far_mar/Market_spec.rb index f2772ef6..cede3ae4 100644 --- a/spec/far_mar/Market_spec.rb +++ b/spec/far_mar/Market_spec.rb @@ -1,4 +1,5 @@ require 'spec_helper' +require 'pry' describe FarMar::Market do before :each do @market = FarMar::Market.new("500", "Montefiore Medical Center Farmers Market_Thursday", "111 E. 210th Street", "Bronx", "Bronx", "New York", "10467") @@ -36,5 +37,31 @@ expect(@market.products[0].class).to eq FarMar::Product 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 end diff --git a/support/markets.csv b/support/markets.csv index 6a265cd0..9eff6fb4 100644 --- a/support/markets.csv +++ b/support/markets.csv @@ -497,4 +497,4 @@ 497,Nevada County Farmers Market,11078 West First Street North,Prescott,Nevada,Arkansas,71857 498,Morningside Park Farmers Market,110th Street & Manhattan Avenue,Mornignside Heights,New York,New York,10027 499,Fletcher Allen's Farmers' Market,111 Colchester Ave 204 EN3,Burlington,Chittenden,Vermont,5401 -500,Montefiore Medical Center Farmers Market_Thursday,111 E. 210th Street,Bronx,Bronx,New York,10467 \ No newline at end of file +500,Montefiore Medical Center Farmers Market_Thursday,111 E. 210th Street,Bronx,Bronx,New York,10467 From 8536039eae0f3e00af841b2374a2f9dcfe4c59b9 Mon Sep 17 00:00:00 2001 From: Kelly Date: Thu, 22 Oct 2015 14:33:20 -0700 Subject: [PATCH 23/29] Same as prior commit with uncommented tests in Market_Spec --- spec/far_mar/Market_spec.rb | 44 ++++++++++++++++++------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/spec/far_mar/Market_spec.rb b/spec/far_mar/Market_spec.rb index cede3ae4..84e3351a 100644 --- a/spec/far_mar/Market_spec.rb +++ b/spec/far_mar/Market_spec.rb @@ -38,28 +38,28 @@ 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 "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 From 1e0e340ff065148c7f59043491ecb84a8f9054aa Mon Sep 17 00:00:00 2001 From: Kelly Date: Thu, 22 Oct 2015 15:07:28 -0700 Subject: [PATCH 24/29] added optional .preferred_vendor method to Market class --- lib/far_mar/Market.rb | 5 +++++ spec/far_mar/Market_spec.rb | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/lib/far_mar/Market.rb b/lib/far_mar/Market.rb index 36873531..f61ad8ff 100644 --- a/lib/far_mar/Market.rb +++ b/lib/far_mar/Market.rb @@ -55,5 +55,10 @@ def self.search(search_term) end return matching_markets end + def preferred_vendor + vendors.max_by do |vendor| + vendor.revenue + end + end end end diff --git a/spec/far_mar/Market_spec.rb b/spec/far_mar/Market_spec.rb index 84e3351a..19776e1b 100644 --- a/spec/far_mar/Market_spec.rb +++ b/spec/far_mar/Market_spec.rb @@ -64,4 +64,9 @@ 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 end From c4f3697d27039e4534871ef792f14a4a8737d726 Mon Sep 17 00:00:00 2001 From: Kelly Date: Fri, 23 Oct 2015 10:24:07 -0700 Subject: [PATCH 25/29] added optional .preferred_vendor method to Market class --- lib/far_mar/Market.rb | 23 ++++++++++++++++++++--- spec/far_mar/Market_spec.rb | 5 +++++ 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/lib/far_mar/Market.rb b/lib/far_mar/Market.rb index f61ad8ff..b6c97ca0 100644 --- a/lib/far_mar/Market.rb +++ b/lib/far_mar/Market.rb @@ -55,9 +55,26 @@ def self.search(search_term) end return matching_markets end - def preferred_vendor - vendors.max_by do |vendor| - vendor.revenue + def preferred_vendor(date = nil) + if date == nil + vendors.max_by do |vendor| + vendor.revenue + end + else + date = DateTime.parse(date) + sales_on_date = FarMar::Sale.all.find_all do |sale| + sale.purchase_time.to_date == date.to_date + end + revenue_hash = Hash.new(0) + sales_on_date.each do |sale| + if sale.vendor.market_id == self.id + revenue_hash["#{sale.vendor_id}"] += sale.amount + end + end + max = revenue_hash.max_by do |vendor_id, revenue| + revenue + end + return FarMar::Vendor.find(max[0].to_i) end end end diff --git a/spec/far_mar/Market_spec.rb b/spec/far_mar/Market_spec.rb index 19776e1b..ee4074be 100644 --- a/spec/far_mar/Market_spec.rb +++ b/spec/far_mar/Market_spec.rb @@ -69,4 +69,9 @@ 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 From e45c49ce70de0d80f72423b53b0da506486e73b5 Mon Sep 17 00:00:00 2001 From: Kelly Date: Fri, 23 Oct 2015 14:40:09 -0700 Subject: [PATCH 26/29] updated .revenue method in Vendor class to accept dates, updated .preferred_vendor method in Market class --- lib/far_mar/Market.rb | 15 ++------------- lib/far_mar/Vendor.rb | 16 +++++++++++++--- spec/far_mar/Vendor_spec.rb | 34 +++++++++++++++++++++++++++------- 3 files changed, 42 insertions(+), 23 deletions(-) diff --git a/lib/far_mar/Market.rb b/lib/far_mar/Market.rb index b6c97ca0..c7b06945 100644 --- a/lib/far_mar/Market.rb +++ b/lib/far_mar/Market.rb @@ -61,20 +61,9 @@ def preferred_vendor(date = nil) vendor.revenue end else - date = DateTime.parse(date) - sales_on_date = FarMar::Sale.all.find_all do |sale| - sale.purchase_time.to_date == date.to_date - end - revenue_hash = Hash.new(0) - sales_on_date.each do |sale| - if sale.vendor.market_id == self.id - revenue_hash["#{sale.vendor_id}"] += sale.amount - end - end - max = revenue_hash.max_by do |vendor_id, revenue| - revenue + vendors.max_by do |vendor| + vendor.revenue(date) end - return FarMar::Vendor.find(max[0].to_i) end end end diff --git a/lib/far_mar/Vendor.rb b/lib/far_mar/Vendor.rb index 10fb6fff..e20c0154 100644 --- a/lib/far_mar/Vendor.rb +++ b/lib/far_mar/Vendor.rb @@ -37,9 +37,19 @@ def sales sale.vendor_id == self.id end end - def revenue - amounts = self.sales.map do |sale| - sale.amount + 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 diff --git a/spec/far_mar/Vendor_spec.rb b/spec/far_mar/Vendor_spec.rb index 15a7c85e..40958349 100644 --- a/spec/far_mar/Vendor_spec.rb +++ b/spec/far_mar/Vendor_spec.rb @@ -42,13 +42,6 @@ expect(sale[1].class).to eq FarMar::Sale end end - describe ".revenue" do - it "returns the sum of all amounts for sales instances whose vendor_id matches the vendor's id" do - @vendor_0_sales = FarMar::Vendor.new("51", "Bernier Inc", "1", "12") - expect(@vendor.revenue).to eq 7586 - expect(@vendor_0_sales.revenue).to eq 0 - end - end describe "#by_market(market_id)" do it "returns all vendor instances whose market_id matches the parameter passed in" do vendors = FarMar::Vendor.by_market(498) @@ -56,4 +49,31 @@ expect(vendors[1].class).to eq FarMar::Vendor end end + describe ".revenue(date)" do + context "when no date parameter is passed in" do + it "returns total revenue from all dates" do + expect(@vendor.revenue).to eq 7586 + end + it "returns 0 when vendor had no sales" do + @vendor_0_sales = FarMar::Vendor.new("51", "Bernier Inc", "1", "12") + expect(@vendor_0_sales.revenue).to eq 0 + end + end + context "when no sales happened on date" do + it "returns 0" do + expect(@vendor.revenue("2013-11-09")).to eq 0 + end + end + context "when 1 sale happened on date" do + it "returns the sale amount" do + expect(@vendor.revenue("2013-11-10")).to eq 3793 + end + end + context "when multiple sales happened on date" do + it "returns the sum of sales amounts for the date" do + @vendor2 = FarMar::Vendor.new("2687", "Pacocha Group", "5", "500") + expect(@vendor2.revenue("2013-11-12")).to eq 20781 + end + end + end end From 85c708fe8fb1893d7c9592cfad2500dedfa3e066 Mon Sep 17 00:00:00 2001 From: Kelly Date: Fri, 23 Oct 2015 14:57:10 -0700 Subject: [PATCH 27/29] updated spec docs to use best practices --- spec/far_mar/Market_spec.rb | 1 - spec/far_mar/Product_spec.rb | 6 +----- spec/far_mar/Sale_spec.rb | 16 +++++++++------- spec/far_mar/Vendor_spec.rb | 17 +++++------------ 4 files changed, 15 insertions(+), 25 deletions(-) diff --git a/spec/far_mar/Market_spec.rb b/spec/far_mar/Market_spec.rb index ee4074be..4124d751 100644 --- a/spec/far_mar/Market_spec.rb +++ b/spec/far_mar/Market_spec.rb @@ -34,7 +34,6 @@ 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 - expect(@market.products[0].class).to eq FarMar::Product end end describe "#search(search_term)" do diff --git a/spec/far_mar/Product_spec.rb b/spec/far_mar/Product_spec.rb index 30d96e38..da0a87ed 100644 --- a/spec/far_mar/Product_spec.rb +++ b/spec/far_mar/Product_spec.rb @@ -23,15 +23,12 @@ end describe ".vendor" do it "returns the vendor instance whose id matches the product's vendor_id" do - vendor = @product.vendor - expect(vendor.id).to eq 2689 - expect(vendor.name).to eq "Durgan-Moen" + 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 - expect(@product.sales[0].class).to eq FarMar::Sale end end describe ".number_of_sales" do @@ -43,7 +40,6 @@ 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 - expect(vendor[3].name).to eq "Energetic Pretzel" end end end diff --git a/spec/far_mar/Sale_spec.rb b/spec/far_mar/Sale_spec.rb index c7daac40..39c5a695 100644 --- a/spec/far_mar/Sale_spec.rb +++ b/spec/far_mar/Sale_spec.rb @@ -24,22 +24,24 @@ describe ".vendor" do it "returns the vendor instance whose id matches the sale's vendor_id" do expect(@sale.vendor.id).to eq 742 - expect(@sale.vendor.name).to eq "Beer, Heathcote and Leffler" end end describe ".product" do it "returns the product instance whose id matches the sales's product_id" do expect(@sale.product.id).to eq 2213 - expect(@sale.product.name).to eq "Rotten Fruit" end end - describe "#between(beginning_time, end_time)"do - it "returns a collection of sales where the purchase_time is between the two parameters" do + describe "#between(beginning_time, end_time)" do + context "when beginning_time is before end_time" do begin_time = "2013-11-06 08:47:12 -0800" end_time = "2013-11-06 08:52:59 -0800" - expect(FarMar::Sale.between(begin_time, end_time).length).to eq 4 - expect(FarMar::Sale.between(begin_time, end_time)[1].id).to eq 4119 - expect(FarMar::Sale.between(begin_time, end_time)[1].class).to eq FarMar::Sale + it "returns a collection of sales instances" do + expect(FarMar::Sale.between(begin_time, end_time)[1].class).to eq FarMar::Sale + end + it "returns all sales that occurred between beginning_time and end_time (exclusive)" do + expect(FarMar::Sale.between(begin_time, end_time).length).to eq 4 + expect(FarMar::Sale.between(begin_time, end_time)[1].id).to eq 4119 + end end context "when end_time is before beginning_time" do it "returns an empty array" do diff --git a/spec/far_mar/Vendor_spec.rb b/spec/far_mar/Vendor_spec.rb index 40958349..475c6221 100644 --- a/spec/far_mar/Vendor_spec.rb +++ b/spec/far_mar/Vendor_spec.rb @@ -23,30 +23,23 @@ end describe ".market" do it "returns the market instance whose id matches the vendor's market_id" do - market = @vendor.market - expect(market.id).to eq 500 - expect(market.name).to eq "Montefiore Medical Center Farmers Market_Thursday" + expect(@vendor.market.id).to eq 500 end end describe ".products" do - it "returns a collection of all product instances whose vendor_id matches the vendor's id" do - product = @vendor.products - expect(product.length).to eq 5 - expect(product[3].class).to eq FarMar::Product + it "returns all product instances whose vendor_id matches the vendor's id" do + expect(@vendor.products.length).to eq 5 end end describe ".sales" do - it "returns a collection of all sales instances whose vendor_id matches the vendor's id" do - sale = @vendor.sales - expect(sale.length).to eq 2 - expect(sale[1].class).to eq FarMar::Sale + it "returns all sales instances whose vendor_id matches the vendor's id" do + expect(@vendor.sales.length).to eq 2 end end describe "#by_market(market_id)" do it "returns all vendor instances whose market_id matches the parameter passed in" do vendors = FarMar::Vendor.by_market(498) expect(vendors.length).to eq 2 - expect(vendors[1].class).to eq FarMar::Vendor end end describe ".revenue(date)" do From d59b13600b3229151bbd6c4e5af2746d51908f7f Mon Sep 17 00:00:00 2001 From: Kelly Date: Fri, 23 Oct 2015 15:03:06 -0700 Subject: [PATCH 28/29] fixed indent errors --- lib/far_mar/Market.rb | 2 +- lib/far_mar/Sale.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/far_mar/Market.rb b/lib/far_mar/Market.rb index c7b06945..381402ce 100644 --- a/lib/far_mar/Market.rb +++ b/lib/far_mar/Market.rb @@ -19,7 +19,7 @@ def self.all @@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 + return @@markets_all end def self.find(id) FarMar::Market.all.find do |market| diff --git a/lib/far_mar/Sale.rb b/lib/far_mar/Sale.rb index 45027ea4..7339befb 100644 --- a/lib/far_mar/Sale.rb +++ b/lib/far_mar/Sale.rb @@ -16,7 +16,7 @@ def self.all @@sales_all.push(FarMar::Sale.new(row[0], row[1], row[2], row[3], row[4])) end end - return @@sales_all + return @@sales_all end def self.find(id) FarMar::Sale.all.find do |sale| From ee340f2ebe949075c412408e5dd308492a194b3a Mon Sep 17 00:00:00 2001 From: Kelly Date: Fri, 23 Oct 2015 15:38:21 -0700 Subject: [PATCH 29/29] revised all 4 classes --- lib/far_mar/Market.rb | 8 ++------ lib/far_mar/Product.rb | 8 ++++---- lib/far_mar/Sale.rb | 6 +++--- lib/far_mar/Vendor.rb | 8 ++++---- 4 files changed, 13 insertions(+), 17 deletions(-) diff --git a/lib/far_mar/Market.rb b/lib/far_mar/Market.rb index 381402ce..a497baf0 100644 --- a/lib/far_mar/Market.rb +++ b/lib/far_mar/Market.rb @@ -44,14 +44,10 @@ def self.search(search_term) search_term = search_term.downcase matching_markets = [] all.each do |market| - if (market.name.downcase.include? search_term) && !(matching_markets.include? market) - matching_markets.push(market) - end + matching_markets.push(market) if (market.name.downcase.include? search_term) && !(matching_markets.include? market) end FarMar::Vendor.all.each do |vendor| - if (vendor.name.downcase.include? search_term) && !(matching_markets.include? vendor.market) - matching_markets.push(vendor.market) - end + matching_markets.push(vendor.market) if (vendor.name.downcase.include? search_term) && !(matching_markets.include? vendor.market) end return matching_markets end diff --git a/lib/far_mar/Product.rb b/lib/far_mar/Product.rb index b11d69a9..aa8c98bd 100644 --- a/lib/far_mar/Product.rb +++ b/lib/far_mar/Product.rb @@ -11,23 +11,23 @@ def self.all @@products_all ||= [] if @@products_all == [] CSV.read("./support/products.csv").map do |row| - @@products_all.push(FarMar::Product.new(row[0], row[1], row[2])) + @@products_all.push(self.new(row[0], row[1], row[2])) end end return @@products_all end def self.find(id) - FarMar::Product.all.find do |product| + all.find do |product| product.id == id end end def vendor - FarMar::Vendor.all.find do |vendor| + Vendor.all.find do |vendor| vendor.id == self.vendor_id end end def sales - FarMar::Sale.all.find_all do |sale| + Sale.all.find_all do |sale| sale.product_id == self.id end end diff --git a/lib/far_mar/Sale.rb b/lib/far_mar/Sale.rb index 7339befb..4e20ee60 100644 --- a/lib/far_mar/Sale.rb +++ b/lib/far_mar/Sale.rb @@ -19,17 +19,17 @@ def self.all return @@sales_all end def self.find(id) - FarMar::Sale.all.find do |sale| + all.find do |sale| sale.id == id end end def vendor - FarMar::Vendor.all.find do |vendor| + Vendor.all.find do |vendor| vendor.id == self.vendor_id end end def product - FarMar::Product.all.find do |product| + Product.all.find do |product| product.id == self.product_id end end diff --git a/lib/far_mar/Vendor.rb b/lib/far_mar/Vendor.rb index e20c0154..9d4d8fea 100644 --- a/lib/far_mar/Vendor.rb +++ b/lib/far_mar/Vendor.rb @@ -18,22 +18,22 @@ def self.all return @@vendors_all end def self.find(id) - FarMar::Vendor.all.find do |vendor| + all.find do |vendor| vendor.id == id end end def market - FarMar::Market.all.find do |market| + Market.all.find do |market| market.id == self.market_id end end def products - FarMar::Product.all.find_all do |product| + Product.all.find_all do |product| product.vendor_id == self.id end end def sales - FarMar::Sale.all.find_all do |sale| + Sale.all.find_all do |sale| sale.vendor_id == self.id end end