From f49c11be06ef9da05039b0105468072da2bcc284 Mon Sep 17 00:00:00 2001 From: Tammy Date: Tue, 20 Oct 2015 14:51:51 -0700 Subject: [PATCH 01/23] working methods for all and find for market --- lib/far_mar/market.rb | 46 ++++++++++++++++++++++++++++++++---- lib/far_mar/product.rb | 8 ++++--- spec/far_mar/market_spec.rb | 17 +++++++++++-- spec/far_mar/product_spec.rb | 4 ++++ spec/far_mar/sale_spec.rb | 4 ++++ spec/far_mar/vendor_spec.rb | 6 ++++- 6 files changed, 75 insertions(+), 10 deletions(-) diff --git a/lib/far_mar/market.rb b/lib/far_mar/market.rb index 81fde0da..659e36d3 100644 --- a/lib/far_mar/market.rb +++ b/lib/far_mar/market.rb @@ -1,10 +1,48 @@ +require 'pry' module FarMar class Market - attr_accessor :name - - def initialize - @name ="" + attr_accessor :name, :id, :address, :county, :state, :zip, :city + + def initialize (id, name, address, city, county, state, zip) + @name = name + @id = id + @address = address + @city = city + @county = county + @state = state + @zip = zip + end + + + + def self.all + market_array =[] + market_csv = CSV.read("./support/markets.csv") + market_csv.each do |row| + new_market = Market.new(row[0].to_i, row[1], row[2], row[3], row[4], row[5], row[6]) + market_array.push(new_market) + # binding.pry + end + return market_array end + + def self.find(id) + markets = self.all + market1 = nil + markets.each do |market| + if market.id == id + market1= market + end + return market1 + end + # markets.find do |market| + # market.id = id + # end + end + + + + end end diff --git a/lib/far_mar/product.rb b/lib/far_mar/product.rb index 50b4aea1..8d649e15 100644 --- a/lib/far_mar/product.rb +++ b/lib/far_mar/product.rb @@ -1,9 +1,11 @@ module FarMar class Product - attr_accessor :name - def initialize - @name = "" + attr_accessor :name , :id, :vendor_id + def initialize (id, name, vendor_id) + @name = name + @id = id + @vendor_id = vendor_id end end end diff --git a/spec/far_mar/market_spec.rb b/spec/far_mar/market_spec.rb index 921e8d9d..9f1235d9 100644 --- a/spec/far_mar/market_spec.rb +++ b/spec/far_mar/market_spec.rb @@ -2,12 +2,25 @@ describe FarMar::Market do before :each do - @market=FarMar::Market.new + @market=FarMar::Market.new(233,"market", "123","Seattle", "King county", "WA", "98034") end - describe "initialize market" do + describe "initialize" do it "creat a new market" do expect(@market).to be_an_instance_of FarMar::Market end end + + describe "all" do + it "returns an array of Market instances" do + expect(FarMar::Market.all).to be_an(Array) + end + end + + describe "find" do + it "return an instence of Market" do + market1 = FarMar::Market.find(1) + expect(market1.city).to eq("Portland") + end + end end diff --git a/spec/far_mar/product_spec.rb b/spec/far_mar/product_spec.rb index 4536969e..927423d3 100644 --- a/spec/far_mar/product_spec.rb +++ b/spec/far_mar/product_spec.rb @@ -10,4 +10,8 @@ expect(@product).to be_an_instance_of FarMar::Product end end + + describe "self.all" do + it "returns a collection of Product instances" + end end diff --git a/spec/far_mar/sale_spec.rb b/spec/far_mar/sale_spec.rb index 36135aeb..4bb85e33 100644 --- a/spec/far_mar/sale_spec.rb +++ b/spec/far_mar/sale_spec.rb @@ -10,4 +10,8 @@ expect(@sale).to be_an_instance_of FarMar::Sale end end + + describe "self.all" do + it "returns a collection of Sale instances" + end end diff --git a/spec/far_mar/vendor_spec.rb b/spec/far_mar/vendor_spec.rb index 46788a60..c70e7d70 100644 --- a/spec/far_mar/vendor_spec.rb +++ b/spec/far_mar/vendor_spec.rb @@ -7,7 +7,11 @@ describe "initialize" do it "creat a new market" do - expect(@vendor).to be_an_instance_of FarMar::Vendor + expect(@vendor).to be_an_instance_of FarMar::Vendor end end + + describe "self.all" do + it "returns a collection of Vendor instances" + end end From 40e897cb40bbaafe1d58252c7bfb6f2a6820b811 Mon Sep 17 00:00:00 2001 From: Tammy Date: Tue, 20 Oct 2015 15:00:56 -0700 Subject: [PATCH 02/23] complete find for product --- lib/far_mar/product.rb | 11 +++++++++++ spec/far_mar/product_spec.rb | 8 +++++--- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/lib/far_mar/product.rb b/lib/far_mar/product.rb index 8d649e15..55e02e14 100644 --- a/lib/far_mar/product.rb +++ b/lib/far_mar/product.rb @@ -2,10 +2,21 @@ module FarMar class Product attr_accessor :name , :id, :vendor_id + def initialize (id, name, vendor_id) @name = name @id = id @vendor_id = vendor_id end + + def self.all + product_array =[] + product_csv = CSV.read("./support/products.csv") + product_csv.each do |product| + new_product = Product.new(product[0].to_i, product[1], product[2].to_i) + product_array.push(new_product) + end + return product_array + end end end diff --git a/spec/far_mar/product_spec.rb b/spec/far_mar/product_spec.rb index 927423d3..0b996c32 100644 --- a/spec/far_mar/product_spec.rb +++ b/spec/far_mar/product_spec.rb @@ -2,7 +2,7 @@ describe FarMar::Product do before :each do - @product=FarMar::Product.new() + @product=FarMar::Product.new(3, "prori", 1) end describe "initialize Product" do @@ -11,7 +11,9 @@ end end - describe "self.all" do - it "returns a collection of Product instances" + describe "all" do + it "returns a collection of Product instances" do + expect(FarMar::Product.all).to be_an(Array) + end end end From c4760993556718b3a6ce81acd3d3b42dcffa55d7 Mon Sep 17 00:00:00 2001 From: Tammy Date: Tue, 20 Oct 2015 16:24:36 -0700 Subject: [PATCH 03/23] all for sale --- lib/far_mar/sale.rb | 20 +++++++++++++++++--- spec/far_mar/sale_spec.rb | 6 ++++-- spec/far_mar/vendor_spec.rb | 8 +++++--- 3 files changed, 26 insertions(+), 8 deletions(-) diff --git a/lib/far_mar/sale.rb b/lib/far_mar/sale.rb index fd255fac..59ea50b7 100644 --- a/lib/far_mar/sale.rb +++ b/lib/far_mar/sale.rb @@ -1,9 +1,23 @@ module FarMar class Sale - attr_accessor :amount - def initialize - @amount = 3 + attr_accessor :amount , :id, :purchase_time, :vendor_id , :product_id + def initialize (id, amount, purchase_time, vendor_id, product_id) + @amount = amount + @id = id + @purchase_time = purchase_time#DateTime.strptime(purchase_time, "%Y-%m-%d %H:%M:%S %z") + @vendor_id = vendor_id + @product_id = product_id + end + + def self.all + sale_array =[] + sale_csv = CSV.read("./support/products.csv") + sale_csv.each do |sale| + new_sale = Sale.new(sale[0].to_i, sale[1].to_i, sale[2], sale[3].to_i, sale[4].to_i) + sale_array.push(new_sale) + end + return sale_array end end end diff --git a/spec/far_mar/sale_spec.rb b/spec/far_mar/sale_spec.rb index 4bb85e33..4ab8f9ac 100644 --- a/spec/far_mar/sale_spec.rb +++ b/spec/far_mar/sale_spec.rb @@ -2,7 +2,7 @@ describe FarMar::Sale do before :each do - @sale =FarMar::Sale.new + @sale = FarMar::Sale.new(11,1030,"2013-11-10 18:56:53 -0800",3,4) end describe "initialize Sale" do @@ -12,6 +12,8 @@ end describe "self.all" do - it "returns a collection of Sale instances" + it "returns a collection of Sale instances" do + expect(FarMar::Sale.all).to be_an(Array) + end end end diff --git a/spec/far_mar/vendor_spec.rb b/spec/far_mar/vendor_spec.rb index c70e7d70..e4eb66dc 100644 --- a/spec/far_mar/vendor_spec.rb +++ b/spec/far_mar/vendor_spec.rb @@ -11,7 +11,9 @@ end end - describe "self.all" do - it "returns a collection of Vendor instances" - end + # describe "self.all" do + # it "returns a collection of Vendors instances" do + # expect(FarMar::Vendor.all).to be_an(Array) + # end + # end end From bce4f79fb3508a5fda754cc9aac9a8560e754534 Mon Sep 17 00:00:00 2001 From: Tammy Date: Tue, 20 Oct 2015 16:31:40 -0700 Subject: [PATCH 04/23] all all works --- lib/far_mar/sale.rb | 2 +- lib/far_mar/vendor.rb | 20 +++++++++++++++++--- spec/far_mar/market_spec.rb | 2 +- spec/far_mar/product_spec.rb | 2 +- spec/far_mar/sale_spec.rb | 2 +- spec/far_mar/vendor_spec.rb | 12 ++++++------ 6 files changed, 27 insertions(+), 13 deletions(-) diff --git a/lib/far_mar/sale.rb b/lib/far_mar/sale.rb index 59ea50b7..4fa27f5f 100644 --- a/lib/far_mar/sale.rb +++ b/lib/far_mar/sale.rb @@ -12,7 +12,7 @@ def initialize (id, amount, purchase_time, vendor_id, product_id) def self.all sale_array =[] - sale_csv = CSV.read("./support/products.csv") + sale_csv = CSV.read("./support/sales.csv") sale_csv.each do |sale| new_sale = Sale.new(sale[0].to_i, sale[1].to_i, sale[2], sale[3].to_i, sale[4].to_i) sale_array.push(new_sale) diff --git a/lib/far_mar/vendor.rb b/lib/far_mar/vendor.rb index cf065221..ee634d4c 100644 --- a/lib/far_mar/vendor.rb +++ b/lib/far_mar/vendor.rb @@ -1,8 +1,22 @@ module FarMar class Vendor - attr_accessor :name - def initialize - @name ="" + attr_accessor :name, :id, :no_employess, :market_id + + def initialize (id, name, no_employess, market_id) + @name = name + @id = id + @no_employess = no_employess + @market_id = market_id + end + + def self.all + vendor_array =[] + vendor_csv = CSV.read("./support/vendors.csv") + vendor_csv.each do |sale| + new_vendor = Vendor.new(sale[0], sale[1].to_i, sale[2], sale[3].to_i) + vendor_array.push(new_vendor) + end + return vendor_array end end end diff --git a/spec/far_mar/market_spec.rb b/spec/far_mar/market_spec.rb index 9f1235d9..5a58d88d 100644 --- a/spec/far_mar/market_spec.rb +++ b/spec/far_mar/market_spec.rb @@ -11,7 +11,7 @@ end end - describe "all" do + describe ".all" do it "returns an array of Market instances" do expect(FarMar::Market.all).to be_an(Array) end diff --git a/spec/far_mar/product_spec.rb b/spec/far_mar/product_spec.rb index 0b996c32..6ef737df 100644 --- a/spec/far_mar/product_spec.rb +++ b/spec/far_mar/product_spec.rb @@ -11,7 +11,7 @@ end end - describe "all" do + describe ".all" do it "returns a collection of Product instances" do expect(FarMar::Product.all).to be_an(Array) end diff --git a/spec/far_mar/sale_spec.rb b/spec/far_mar/sale_spec.rb index 4ab8f9ac..356e1573 100644 --- a/spec/far_mar/sale_spec.rb +++ b/spec/far_mar/sale_spec.rb @@ -11,7 +11,7 @@ end end - describe "self.all" do + describe ".all" do it "returns a collection of Sale instances" do expect(FarMar::Sale.all).to be_an(Array) end diff --git a/spec/far_mar/vendor_spec.rb b/spec/far_mar/vendor_spec.rb index e4eb66dc..33f47aad 100644 --- a/spec/far_mar/vendor_spec.rb +++ b/spec/far_mar/vendor_spec.rb @@ -2,7 +2,7 @@ describe FarMar::Vendor do before :each do - @vendor =FarMar::Vendor.new + @vendor =FarMar::Vendor.new("Hamill", "Kilback and Pfeffer",5,1) end describe "initialize" do @@ -11,9 +11,9 @@ end end - # describe "self.all" do - # it "returns a collection of Vendors instances" do - # expect(FarMar::Vendor.all).to be_an(Array) - # end - # end + describe ".all" do + it "returns a collection of Vendors instances" do + expect(FarMar::Vendor.all).to be_an(Array) + end + end end From ebf1a6974a30a3cab7ee08b7bd60c99751da0189 Mon Sep 17 00:00:00 2001 From: Tammy Date: Tue, 20 Oct 2015 16:45:17 -0700 Subject: [PATCH 05/23] finish all find and all.. whoohhooo --- lib/far_mar/market.rb | 10 +++------- lib/far_mar/product.rb | 7 +++++++ lib/far_mar/sale.rb | 8 ++++++++ lib/far_mar/vendor.rb | 7 +++++++ spec/far_mar/product_spec.rb | 9 ++++++++- 5 files changed, 33 insertions(+), 8 deletions(-) diff --git a/lib/far_mar/market.rb b/lib/far_mar/market.rb index 659e36d3..52c8ace2 100644 --- a/lib/far_mar/market.rb +++ b/lib/far_mar/market.rb @@ -28,13 +28,9 @@ def self.all end def self.find(id) - markets = self.all - market1 = nil - markets.each do |market| - if market.id == id - market1= market - end - return market1 + market_array = self.all + market_array.find do |market| + market.id == id end # markets.find do |market| # market.id = id diff --git a/lib/far_mar/product.rb b/lib/far_mar/product.rb index 55e02e14..61302109 100644 --- a/lib/far_mar/product.rb +++ b/lib/far_mar/product.rb @@ -18,5 +18,12 @@ def self.all end return product_array end + + def self.find(id) + product_array = self.all + product_array.find do |product| + product.id == id + end + end end end diff --git a/lib/far_mar/sale.rb b/lib/far_mar/sale.rb index 4fa27f5f..3562f7e8 100644 --- a/lib/far_mar/sale.rb +++ b/lib/far_mar/sale.rb @@ -19,5 +19,13 @@ def self.all end return sale_array end + + def self.find(id) + sale_array = self.all + sale_array.find do |sale| + sale.id == id + end + end + end end diff --git a/lib/far_mar/vendor.rb b/lib/far_mar/vendor.rb index ee634d4c..5658082f 100644 --- a/lib/far_mar/vendor.rb +++ b/lib/far_mar/vendor.rb @@ -18,5 +18,12 @@ def self.all end return vendor_array end + + def self.find(id) + vendor_array = self.all + vendor_array.find do |vendor| + vendor.id == id + end + end end end diff --git a/spec/far_mar/product_spec.rb b/spec/far_mar/product_spec.rb index 6ef737df..4266fec9 100644 --- a/spec/far_mar/product_spec.rb +++ b/spec/far_mar/product_spec.rb @@ -2,7 +2,7 @@ describe FarMar::Product do before :each do - @product=FarMar::Product.new(3, "prori", 1) + @product=FarMar::Product.new(8, "Shaky Honey", 5) end describe "initialize Product" do @@ -16,4 +16,11 @@ expect(FarMar::Product.all).to be_an(Array) end end + + describe "find" do + it "return the product, usind id " do + expect(FarMar::Product.find(8).name).to eq("Shaky Honey") + end + + end end From 15704f336385cf54f05976accddc237187c0e4e9 Mon Sep 17 00:00:00 2001 From: Tammy Date: Tue, 20 Oct 2015 16:53:43 -0700 Subject: [PATCH 06/23] 12 example. no errors --- lib/far_mar/vendor.rb | 3 ++- spec/far_mar/product_spec.rb | 2 +- spec/far_mar/sale_spec.rb | 7 +++++++ spec/far_mar/vendor_spec.rb | 9 ++++++++- 4 files changed, 18 insertions(+), 3 deletions(-) diff --git a/lib/far_mar/vendor.rb b/lib/far_mar/vendor.rb index 5658082f..d416bf8f 100644 --- a/lib/far_mar/vendor.rb +++ b/lib/far_mar/vendor.rb @@ -1,3 +1,4 @@ +require 'pry' module FarMar class Vendor attr_accessor :name, :id, :no_employess, :market_id @@ -13,7 +14,7 @@ def self.all vendor_array =[] vendor_csv = CSV.read("./support/vendors.csv") vendor_csv.each do |sale| - new_vendor = Vendor.new(sale[0], sale[1].to_i, sale[2], sale[3].to_i) + new_vendor = Vendor.new(sale[0].to_i, sale[1], sale[2], sale[3].to_i) vendor_array.push(new_vendor) end return vendor_array diff --git a/spec/far_mar/product_spec.rb b/spec/far_mar/product_spec.rb index 4266fec9..fef19520 100644 --- a/spec/far_mar/product_spec.rb +++ b/spec/far_mar/product_spec.rb @@ -21,6 +21,6 @@ it "return the product, usind id " do expect(FarMar::Product.find(8).name).to eq("Shaky Honey") end - end + end diff --git a/spec/far_mar/sale_spec.rb b/spec/far_mar/sale_spec.rb index 356e1573..a0fc6457 100644 --- a/spec/far_mar/sale_spec.rb +++ b/spec/far_mar/sale_spec.rb @@ -16,4 +16,11 @@ expect(FarMar::Sale.all).to be_an(Array) end end + + describe "find" do + it "return the sale, usind id " do + expect(FarMar::Sale.find(11).amount).to eq(1030) + end + end + end diff --git a/spec/far_mar/vendor_spec.rb b/spec/far_mar/vendor_spec.rb index 33f47aad..13cd0412 100644 --- a/spec/far_mar/vendor_spec.rb +++ b/spec/far_mar/vendor_spec.rb @@ -2,7 +2,7 @@ describe FarMar::Vendor do before :each do - @vendor =FarMar::Vendor.new("Hamill", "Kilback and Pfeffer",5,1) + @vendor =FarMar::Vendor.new(6,"Zulauf and Sons",8,1) end describe "initialize" do @@ -16,4 +16,11 @@ expect(FarMar::Vendor.all).to be_an(Array) end end + + describe "find" do + it "return the vendor, usind id " do + expect(FarMar::Vendor.find(6).name).to eq("Zulauf and Sons") + end + end + end From 95dd4b83613e9908115c50c106f5113936a0712b Mon Sep 17 00:00:00 2001 From: Tammy Date: Wed, 21 Oct 2015 08:51:59 -0700 Subject: [PATCH 07/23] got the vendor method working --- lib/far_mar/market.rb | 6 +++--- lib/far_mar/product.rb | 2 ++ lib/far_mar/vendor.rb | 9 +++++++++ spec/far_mar/market_spec.rb | 17 +++++++++++++++++ spec/far_mar/vendor_spec.rb | 16 +++++++++++++++- 5 files changed, 46 insertions(+), 4 deletions(-) diff --git a/lib/far_mar/market.rb b/lib/far_mar/market.rb index 52c8ace2..b0a09a8c 100644 --- a/lib/far_mar/market.rb +++ b/lib/far_mar/market.rb @@ -32,11 +32,11 @@ def self.find(id) market_array.find do |market| market.id == id end - # markets.find do |market| - # market.id = id - # end end + def vendors + return FarMar::Vendor.find_by_market_id(self.id) + end diff --git a/lib/far_mar/product.rb b/lib/far_mar/product.rb index 61302109..a31c5ee0 100644 --- a/lib/far_mar/product.rb +++ b/lib/far_mar/product.rb @@ -25,5 +25,7 @@ def self.find(id) product.id == id end end + + end end diff --git a/lib/far_mar/vendor.rb b/lib/far_mar/vendor.rb index d416bf8f..ce6e9b32 100644 --- a/lib/far_mar/vendor.rb +++ b/lib/far_mar/vendor.rb @@ -26,5 +26,14 @@ def self.find(id) vendor.id == id end end + + def self.find_by_market_id(id) + vendor_array = self.all + vendor_array.find_all do |vendor| + vendor.market_id == id + end + end + + end end diff --git a/spec/far_mar/market_spec.rb b/spec/far_mar/market_spec.rb index 5a58d88d..dab618d9 100644 --- a/spec/far_mar/market_spec.rb +++ b/spec/far_mar/market_spec.rb @@ -23,4 +23,21 @@ expect(market1.city).to eq("Portland") end end + + describe "vendors" do + before :each do + @market_array = @market.vendors + end + + it "returns a collection of FarMar::Vendor" do + expect(@market_array).to be_an_instance_of(Array) + end + + + end end + +# it "return vendors that associated with the market" +# expact(@market_array).to include +# end +# end diff --git a/spec/far_mar/vendor_spec.rb b/spec/far_mar/vendor_spec.rb index 13cd0412..0c54a5cc 100644 --- a/spec/far_mar/vendor_spec.rb +++ b/spec/far_mar/vendor_spec.rb @@ -22,5 +22,19 @@ expect(FarMar::Vendor.find(6).name).to eq("Zulauf and Sons") end end - + + describe "find_by_market_id" do + it "return a vendor array" do + expect(FarMar::Vendor.find_by_market_id(2)).to be_an_instance_of(Array) + end + + it "return the right array" do + vendor_array = FarMar::Vendor.find_by_market_id(1) + vendor_array.each do |vendor| + (@check = true) if vendor.id == 1 + end + expect(@check).to be_truthy + end + end + end From c4f30f1499277380acf74c37c31962bec7b442d3 Mon Sep 17 00:00:00 2001 From: Tammy Date: Wed, 21 Oct 2015 10:38:22 -0700 Subject: [PATCH 08/23] moving to products with no errors --- lib/far_mar/market.rb | 2 +- lib/far_mar/product.rb | 2 +- lib/far_mar/sale.rb | 4 +++- lib/far_mar/vendor.rb | 2 +- spec/far_mar/market_spec.rb | 1 - spec/far_mar/vendor_spec.rb | 8 +++++--- 6 files changed, 11 insertions(+), 8 deletions(-) diff --git a/lib/far_mar/market.rb b/lib/far_mar/market.rb index b0a09a8c..671404ed 100644 --- a/lib/far_mar/market.rb +++ b/lib/far_mar/market.rb @@ -35,7 +35,7 @@ def self.find(id) end def vendors - return FarMar::Vendor.find_by_market_id(self.id) + return FarMar::Vendor.market(self.id) end diff --git a/lib/far_mar/product.rb b/lib/far_mar/product.rb index a31c5ee0..c4f9d63d 100644 --- a/lib/far_mar/product.rb +++ b/lib/far_mar/product.rb @@ -26,6 +26,6 @@ def self.find(id) end end - + end end diff --git a/lib/far_mar/sale.rb b/lib/far_mar/sale.rb index 3562f7e8..5be44584 100644 --- a/lib/far_mar/sale.rb +++ b/lib/far_mar/sale.rb @@ -1,3 +1,4 @@ +require 'pry' module FarMar class Sale @@ -5,9 +6,10 @@ class Sale def initialize (id, amount, purchase_time, vendor_id, product_id) @amount = amount @id = id - @purchase_time = purchase_time#DateTime.strptime(purchase_time, "%Y-%m-%d %H:%M:%S %z") + @purchase_time = DateTime.parse(purchase_time)#DateTime.strptime(purchase_time, "%Y-%m-%d %H:%M:%S %z") @vendor_id = vendor_id @product_id = product_id + end def self.all diff --git a/lib/far_mar/vendor.rb b/lib/far_mar/vendor.rb index ce6e9b32..0827fba4 100644 --- a/lib/far_mar/vendor.rb +++ b/lib/far_mar/vendor.rb @@ -27,7 +27,7 @@ def self.find(id) end end - def self.find_by_market_id(id) + def self.market(id) vendor_array = self.all vendor_array.find_all do |vendor| vendor.market_id == id diff --git a/spec/far_mar/market_spec.rb b/spec/far_mar/market_spec.rb index dab618d9..afa6b43b 100644 --- a/spec/far_mar/market_spec.rb +++ b/spec/far_mar/market_spec.rb @@ -33,7 +33,6 @@ expect(@market_array).to be_an_instance_of(Array) end - end end diff --git a/spec/far_mar/vendor_spec.rb b/spec/far_mar/vendor_spec.rb index 0c54a5cc..816cbf8d 100644 --- a/spec/far_mar/vendor_spec.rb +++ b/spec/far_mar/vendor_spec.rb @@ -23,13 +23,13 @@ end end - describe "find_by_market_id" do + describe "market" do it "return a vendor array" do - expect(FarMar::Vendor.find_by_market_id(2)).to be_an_instance_of(Array) + expect(FarMar::Vendor.market(2)).to be_an_instance_of(Array) end it "return the right array" do - vendor_array = FarMar::Vendor.find_by_market_id(1) + vendor_array = FarMar::Vendor.market(1) vendor_array.each do |vendor| (@check = true) if vendor.id == 1 end @@ -37,4 +37,6 @@ end end + + end From 09b60350079ce14fc0ba59191ef16c9434cda8cb Mon Sep 17 00:00:00 2001 From: Tammy Date: Wed, 21 Oct 2015 11:18:58 -0700 Subject: [PATCH 09/23] done with sales by vendor, no errors --- lib/far_mar/product.rb | 8 ++++++++ lib/far_mar/sale.rb | 7 +++++++ lib/far_mar/vendor.rb | 4 ++++ spec/far_mar/product_spec.rb | 9 ++++++++- spec/far_mar/sale_spec.rb | 16 +++++++++++++++- spec/far_mar/vendor_spec.rb | 11 +++++++++++ 6 files changed, 53 insertions(+), 2 deletions(-) diff --git a/lib/far_mar/product.rb b/lib/far_mar/product.rb index c4f9d63d..d1df5394 100644 --- a/lib/far_mar/product.rb +++ b/lib/far_mar/product.rb @@ -26,6 +26,14 @@ def self.find(id) end end + def self.by_vendor(id) + product_array = self.all + product_array.find_all do |product| + product.vendor_id == id + end + end + + end end diff --git a/lib/far_mar/sale.rb b/lib/far_mar/sale.rb index 5be44584..f1a8d51c 100644 --- a/lib/far_mar/sale.rb +++ b/lib/far_mar/sale.rb @@ -9,6 +9,7 @@ def initialize (id, amount, purchase_time, vendor_id, product_id) @purchase_time = DateTime.parse(purchase_time)#DateTime.strptime(purchase_time, "%Y-%m-%d %H:%M:%S %z") @vendor_id = vendor_id @product_id = product_id + # @sale_array = self.all -check it later end @@ -29,5 +30,11 @@ def self.find(id) end end + def self.by_vendor(id) + sale_array = self.all + sale_array.find_all do |sale| + sale.vendor_id == id + end + end end end diff --git a/lib/far_mar/vendor.rb b/lib/far_mar/vendor.rb index 0827fba4..8ce5e750 100644 --- a/lib/far_mar/vendor.rb +++ b/lib/far_mar/vendor.rb @@ -34,6 +34,10 @@ def self.market(id) end end + def products + return products_array = FarMar::Product.by_vendor(self.id) + end + end end diff --git a/spec/far_mar/product_spec.rb b/spec/far_mar/product_spec.rb index fef19520..c83f2236 100644 --- a/spec/far_mar/product_spec.rb +++ b/spec/far_mar/product_spec.rb @@ -22,5 +22,12 @@ expect(FarMar::Product.find(8).name).to eq("Shaky Honey") end end - + + describe "by_vendor" do + it "return products by vendor id" do + vendor_array = FarMar::Product.by_vendor(2) + expect(vendor_array[0].id).to eq 2 + end + end + end diff --git a/spec/far_mar/sale_spec.rb b/spec/far_mar/sale_spec.rb index a0fc6457..4c338d63 100644 --- a/spec/far_mar/sale_spec.rb +++ b/spec/far_mar/sale_spec.rb @@ -22,5 +22,19 @@ expect(FarMar::Sale.find(11).amount).to eq(1030) end end - + + describe "by_vendor" do + before :each do + @sale_array = FarMar::Sale.by_vendor(1) + end + + it "return an array of sales " do + expect(@sale_array).to be_an(Array) + end + + it "return an array by vendor id" do + expect(@sale_array[0].id).to eq 1 + end + end + end diff --git a/spec/far_mar/vendor_spec.rb b/spec/far_mar/vendor_spec.rb index 816cbf8d..e1daa851 100644 --- a/spec/far_mar/vendor_spec.rb +++ b/spec/far_mar/vendor_spec.rb @@ -37,6 +37,17 @@ end end + describe "products" do + # it "returns a collection of with the vendor id" do + # + # end + + it "return an Array of products" do + expect(@vendor.products).to be_an (Array) + end + + end + end From 8f095296313f0a3e8ea38807b39e6fac19a21462 Mon Sep 17 00:00:00 2001 From: Tammy Date: Wed, 21 Oct 2015 14:31:09 -0700 Subject: [PATCH 10/23] yay- done with revenue NO ERRRORS --- lib/far_mar/vendor.rb | 13 +++++++++++++ spec/far_mar/vendor_spec.rb | 18 ++++++++++++++---- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/lib/far_mar/vendor.rb b/lib/far_mar/vendor.rb index 8ce5e750..01f2159b 100644 --- a/lib/far_mar/vendor.rb +++ b/lib/far_mar/vendor.rb @@ -38,6 +38,19 @@ def products return products_array = FarMar::Product.by_vendor(self.id) end + def sales + return Farmar::Sale.by_vendor(self.id) + end + + + def revenue + sale_array = FarMar::Sale.by_vendor(self.id) + return sale_array.inject(0){|sum, sale| sum + sale.amount} + end + + + # return sale_array.inject(0) {|sum, sale.amount| sum + sale.amount} + #end end end diff --git a/spec/far_mar/vendor_spec.rb b/spec/far_mar/vendor_spec.rb index e1daa851..6758a3b1 100644 --- a/spec/far_mar/vendor_spec.rb +++ b/spec/far_mar/vendor_spec.rb @@ -38,16 +38,26 @@ end describe "products" do - # it "returns a collection of with the vendor id" do - # - # end - it "return an Array of products" do expect(@vendor.products).to be_an (Array) end + end + describe "sales" do + it "return an Array of sales" do + expect(FarMar::Sale.by_vendor(1)).to be_an(Array) + end end + describe "revenue" do + it "return an integer" do + expect(@vendor.revenue).to be_an_instance_of(Fixnum) + end + + it "sum it all the vendor's sales" do + expect(@vendor.revenue).to eq 2977 + end + end end From b36a8e1696ff4d7e45e41c5ad3ceb4c57eb3a1d4 Mon Sep 17 00:00:00 2001 From: Tammy Date: Wed, 21 Oct 2015 14:44:58 -0700 Subject: [PATCH 11/23] finished Vendor methods --- lib/far_mar/vendor.rb | 6 ++++++ spec/far_mar/vendor_spec.rb | 7 +++++++ 2 files changed, 13 insertions(+) diff --git a/lib/far_mar/vendor.rb b/lib/far_mar/vendor.rb index 01f2159b..4b76146f 100644 --- a/lib/far_mar/vendor.rb +++ b/lib/far_mar/vendor.rb @@ -48,6 +48,12 @@ def revenue return sale_array.inject(0){|sum, sale| sum + sale.amount} end + def self.by_market(market_id) + vendor_array = self.all + vendor_array.find_all do |vendor| + vendor.market_id == market_id + end + end # return sale_array.inject(0) {|sum, sale.amount| sum + sale.amount} #end diff --git a/spec/far_mar/vendor_spec.rb b/spec/far_mar/vendor_spec.rb index 6758a3b1..9f7a9386 100644 --- a/spec/far_mar/vendor_spec.rb +++ b/spec/far_mar/vendor_spec.rb @@ -59,5 +59,12 @@ end end + describe "by_market" do + it "returns an Array" do + expect(FarMar::Vendor.by_market(2)).to be_an_instance_of(Array) + end + end + + end From 936627506d27f36ba377cd28b867d322eb6c832c Mon Sep 17 00:00:00 2001 From: Tammy Date: Wed, 21 Oct 2015 15:25:05 -0700 Subject: [PATCH 12/23] in products: vendor and sales are working --- lib/far_mar/product.rb | 10 +++++++++- lib/far_mar/vendor.rb | 2 +- spec/far_mar/product_spec.rb | 16 +++++++++++++--- 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/lib/far_mar/product.rb b/lib/far_mar/product.rb index d1df5394..55490382 100644 --- a/lib/far_mar/product.rb +++ b/lib/far_mar/product.rb @@ -26,13 +26,21 @@ def self.find(id) end end - def self.by_vendor(id) + def self.vendor(id) product_array = self.all product_array.find_all do |product| product.vendor_id == id end end + def sales + sales_array = FarMar::Sale.all + sales_array.find_all do |sale| + sale.product_id == self.id + end + end + + end diff --git a/lib/far_mar/vendor.rb b/lib/far_mar/vendor.rb index 4b76146f..3016c9a2 100644 --- a/lib/far_mar/vendor.rb +++ b/lib/far_mar/vendor.rb @@ -35,7 +35,7 @@ def self.market(id) end def products - return products_array = FarMar::Product.by_vendor(self.id) + return products_array = FarMar::Product.vendor(self.id) end def sales diff --git a/spec/far_mar/product_spec.rb b/spec/far_mar/product_spec.rb index c83f2236..13b1a484 100644 --- a/spec/far_mar/product_spec.rb +++ b/spec/far_mar/product_spec.rb @@ -2,7 +2,7 @@ describe FarMar::Product do before :each do - @product=FarMar::Product.new(8, "Shaky Honey", 5) + @product=FarMar::Product.new(2,"Fierce Greens",2) end describe "initialize Product" do @@ -23,11 +23,21 @@ end end - describe "by_vendor" do + describe "vendor" do it "return products by vendor id" do - vendor_array = FarMar::Product.by_vendor(2) + vendor_array = FarMar::Product.vendor(2) expect(vendor_array[0].id).to eq 2 end end + describe "sales" do + it "return an array of sales" do + expect(@product.sales).to be_an(Array) + end + + it "return an array of sales for this product" do + expect(@product.sales[0].id).to eq 8 + end + end + end From 3971b695ac965bc8f2e3e383edbbad2d916d1ae6 Mon Sep 17 00:00:00 2001 From: Tammy Date: Wed, 21 Oct 2015 16:00:01 -0700 Subject: [PATCH 13/23] I made everything much faster --- lib/far_mar/market.rb | 20 +++++++++++--------- lib/far_mar/product.rb | 17 +++++++++-------- lib/far_mar/sale.rb | 22 ++++++++++++---------- lib/far_mar/vendor.rb | 26 ++++++++++++++------------ spec/far_mar/product_spec.rb | 5 +++++ 5 files changed, 51 insertions(+), 39 deletions(-) diff --git a/lib/far_mar/market.rb b/lib/far_mar/market.rb index 671404ed..55172ab2 100644 --- a/lib/far_mar/market.rb +++ b/lib/far_mar/market.rb @@ -17,19 +17,21 @@ def initialize (id, name, address, city, county, state, zip) def self.all - market_array =[] - market_csv = CSV.read("./support/markets.csv") - market_csv.each do |row| - new_market = Market.new(row[0].to_i, row[1], row[2], row[3], row[4], row[5], row[6]) - market_array.push(new_market) - # binding.pry + @@market_all ||= [] + if @@market_all == [] + market_csv = CSV.read("./support/markets.csv") + market_csv.each do |row| + new_market = Market.new(row[0].to_i, row[1], row[2], row[3], row[4], row[5], row[6]) + @@market_all.push(new_market) + # binding.pry + end end - return market_array + return @@market_all end def self.find(id) - market_array = self.all - market_array.find do |market| + @@market_all= self.all + @@market_all.find do |market| market.id == id end end diff --git a/lib/far_mar/product.rb b/lib/far_mar/product.rb index 55490382..ccc0a61d 100644 --- a/lib/far_mar/product.rb +++ b/lib/far_mar/product.rb @@ -10,13 +10,15 @@ def initialize (id, name, vendor_id) end def self.all - product_array =[] + @@product_all ||= [] + if @@product_all == [] product_csv = CSV.read("./support/products.csv") product_csv.each do |product| new_product = Product.new(product[0].to_i, product[1], product[2].to_i) - product_array.push(new_product) + @@product_all.push(new_product) end - return product_array + end + return @@product_all end def self.find(id) @@ -27,8 +29,7 @@ def self.find(id) end def self.vendor(id) - product_array = self.all - product_array.find_all do |product| + @@product_all.find_all do |product| product.vendor_id == id end end @@ -39,9 +40,9 @@ def sales sale.product_id == self.id end end - - - + # + # def number_of_sales + # end end end diff --git a/lib/far_mar/sale.rb b/lib/far_mar/sale.rb index f1a8d51c..0aa85619 100644 --- a/lib/far_mar/sale.rb +++ b/lib/far_mar/sale.rb @@ -14,25 +14,27 @@ def initialize (id, amount, purchase_time, vendor_id, product_id) end def self.all - sale_array =[] - sale_csv = CSV.read("./support/sales.csv") - sale_csv.each do |sale| - new_sale = Sale.new(sale[0].to_i, sale[1].to_i, sale[2], sale[3].to_i, sale[4].to_i) - sale_array.push(new_sale) + @@sale_all ||= [] + if @@sale_all == [] + sale_csv = CSV.read("./support/sales.csv") + sale_csv.each do |sale| + new_sale = Sale.new(sale[0].to_i, sale[1].to_i, sale[2], sale[3].to_i, sale[4].to_i) + @@sale_all.push(new_sale) + end end - return sale_array + return @@sale_all end def self.find(id) - sale_array = self.all - sale_array.find do |sale| + @@sale_all = self.all + @@sale_all.find do |sale| sale.id == id end end def self.by_vendor(id) - sale_array = self.all - sale_array.find_all do |sale| + @@sale_all = self.all + @@sale_all.find_all do |sale| sale.vendor_id == id end end diff --git a/lib/far_mar/vendor.rb b/lib/far_mar/vendor.rb index 3016c9a2..907ff2db 100644 --- a/lib/far_mar/vendor.rb +++ b/lib/far_mar/vendor.rb @@ -11,25 +11,27 @@ def initialize (id, name, no_employess, market_id) end def self.all - vendor_array =[] - vendor_csv = CSV.read("./support/vendors.csv") - vendor_csv.each do |sale| - new_vendor = Vendor.new(sale[0].to_i, sale[1], sale[2], sale[3].to_i) - vendor_array.push(new_vendor) + @@array_all ||= [] + if @@array_all == [] + vendor_csv = CSV.read("./support/vendors.csv") + vendor_csv.each do |sale| + new_vendor = Vendor.new(sale[0].to_i, sale[1], sale[2], sale[3].to_i) + @@array_all.push(new_vendor) + end end - return vendor_array + return @@array_all end def self.find(id) - vendor_array = self.all - vendor_array.find do |vendor| + @@array_all = self.all + @@array_all.find do |vendor| vendor.id == id end end def self.market(id) - vendor_array = self.all - vendor_array.find_all do |vendor| + @@array_all = self.all + @@array_all.find_all do |vendor| vendor.market_id == id end end @@ -49,8 +51,8 @@ def revenue end def self.by_market(market_id) - vendor_array = self.all - vendor_array.find_all do |vendor| + @@array_all = self.all + @@array_all.find_all do |vendor| vendor.market_id == market_id end end diff --git a/spec/far_mar/product_spec.rb b/spec/far_mar/product_spec.rb index 13b1a484..3839b6ee 100644 --- a/spec/far_mar/product_spec.rb +++ b/spec/far_mar/product_spec.rb @@ -40,4 +40,9 @@ end end + # describe "number_of_sales" do + # it "returns how many time this product got sold" + # expect(@product.number_of_sales).to eq 1 + # end + end From 0d82e717c8eae094525ef95fefb8d47a964ec673 Mon Sep 17 00:00:00 2001 From: Tammy Date: Wed, 21 Oct 2015 16:06:12 -0700 Subject: [PATCH 14/23] done number of sales for product --- lib/far_mar/product.rb | 7 ++++--- spec/far_mar/product_spec.rb | 13 +++++++++---- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/lib/far_mar/product.rb b/lib/far_mar/product.rb index ccc0a61d..732e1035 100644 --- a/lib/far_mar/product.rb +++ b/lib/far_mar/product.rb @@ -40,9 +40,10 @@ def sales sale.product_id == self.id end end - # - # def number_of_sales - # end + + def number_of_sales + return self.sales.length + end end end diff --git a/spec/far_mar/product_spec.rb b/spec/far_mar/product_spec.rb index 3839b6ee..0aa0b32d 100644 --- a/spec/far_mar/product_spec.rb +++ b/spec/far_mar/product_spec.rb @@ -40,9 +40,14 @@ end end - # describe "number_of_sales" do - # it "returns how many time this product got sold" - # expect(@product.number_of_sales).to eq 1 - # end + describe "number_of_sales" do + it "returns how many time this product got sold" do + expect(@product.number_of_sales).to eq 1 + end + + it "return an integer" do + expect(@product.number_of_sales).to be_an_instance_of(Fixnum) + end + end end From 34ee907b8ba01ee3f26a6b453cbfa5b6f692091f Mon Sep 17 00:00:00 2001 From: Tammy Date: Thu, 22 Oct 2015 07:55:06 -0700 Subject: [PATCH 15/23] wrote the between method. looks good --- lib/far_mar/product.rb | 4 ++-- lib/far_mar/sale.rb | 12 +++++++++++- lib/far_mar/vendor.rb | 2 +- spec/far_mar/product_spec.rb | 4 ++-- spec/far_mar/sale_spec.rb | 13 +++++++++++++ 5 files changed, 29 insertions(+), 6 deletions(-) diff --git a/lib/far_mar/product.rb b/lib/far_mar/product.rb index 732e1035..7f2d1679 100644 --- a/lib/far_mar/product.rb +++ b/lib/far_mar/product.rb @@ -28,9 +28,9 @@ def self.find(id) end end - def self.vendor(id) + def self.by_vendor(vendor_id) @@product_all.find_all do |product| - product.vendor_id == id + product.vendor_id == vendor_id end end diff --git a/lib/far_mar/sale.rb b/lib/far_mar/sale.rb index 0aa85619..e35fbdc3 100644 --- a/lib/far_mar/sale.rb +++ b/lib/far_mar/sale.rb @@ -6,7 +6,7 @@ class Sale def initialize (id, amount, purchase_time, vendor_id, product_id) @amount = amount @id = id - @purchase_time = DateTime.parse(purchase_time)#DateTime.strptime(purchase_time, "%Y-%m-%d %H:%M:%S %z") + @purchase_time = DateTime.strptime(purchase_time, "%Y-%m-%d %H:%M:%S %z" ) @vendor_id = vendor_id @product_id = product_id # @sale_array = self.all -check it later @@ -38,5 +38,15 @@ def self.by_vendor(id) sale.vendor_id == id end end + + def self.between(beginnig_time, end_time) + @@sale_all = self.all + array_sales = [] + @@sale_all.map do |sale| + check = sale.purchase_time.between?(beginnig_time, end_time) + array_sales.push(sale) if check + end + return array_sales + end end end diff --git a/lib/far_mar/vendor.rb b/lib/far_mar/vendor.rb index 907ff2db..069c3525 100644 --- a/lib/far_mar/vendor.rb +++ b/lib/far_mar/vendor.rb @@ -37,7 +37,7 @@ def self.market(id) end def products - return products_array = FarMar::Product.vendor(self.id) + return products_array = FarMar::Product.by_vendor(self.id) end def sales diff --git a/spec/far_mar/product_spec.rb b/spec/far_mar/product_spec.rb index 0aa0b32d..57eedf51 100644 --- a/spec/far_mar/product_spec.rb +++ b/spec/far_mar/product_spec.rb @@ -23,9 +23,9 @@ end end - describe "vendor" do + describe "by_vendor" do it "return products by vendor id" do - vendor_array = FarMar::Product.vendor(2) + vendor_array = FarMar::Product.by_vendor(2) expect(vendor_array[0].id).to eq 2 end end diff --git a/spec/far_mar/sale_spec.rb b/spec/far_mar/sale_spec.rb index 4c338d63..1396e285 100644 --- a/spec/far_mar/sale_spec.rb +++ b/spec/far_mar/sale_spec.rb @@ -37,4 +37,17 @@ end end + describe "between" do + it "returns an array of sales" do + beginning_time = DateTime.strptime("2013-11-06 08:35:40 -0800", "%Y-%m-%d %H:%M:%S %z") + end_time = DateTime.strptime("2013-11-08 16:36:03 -0800", "%Y-%m-%d %H:%M:%S %z" ) + expect(FarMar::Sale.between(beginning_time, end_time)).to be_an_instance_of(Array) + end + it "purchases between the two times" do + beginning_time = DateTime.strptime("2013-11-13 01:48:37 -0800", "%Y-%m-%d %H:%M:%S %z") + end_time = DateTime.strptime("2013-11-13 01:50:37 -0800", "%Y-%m-%d %H:%M:%S %z" ) + sale = FarMar::Sale.between(beginning_time, end_time) + expect(sale[0].id).to eq(3) + end + end end From 33f5f0839821e31ae2550bac04a085ebb6d5799e Mon Sep 17 00:00:00 2001 From: Tammy Date: Thu, 22 Oct 2015 08:03:12 -0700 Subject: [PATCH 16/23] vendor method - check --- lib/far_mar/sale.rb | 5 +++++ lib/far_mar/vendor.rb | 4 +--- spec/far_mar/sale_spec.rb | 7 +++++++ 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/lib/far_mar/sale.rb b/lib/far_mar/sale.rb index e35fbdc3..5305c605 100644 --- a/lib/far_mar/sale.rb +++ b/lib/far_mar/sale.rb @@ -48,5 +48,10 @@ def self.between(beginnig_time, end_time) end return array_sales end + + def vendor + return FarMar::Vendor.find(self.vendor_id) + end + end end diff --git a/lib/far_mar/vendor.rb b/lib/far_mar/vendor.rb index 069c3525..3e8c598c 100644 --- a/lib/far_mar/vendor.rb +++ b/lib/far_mar/vendor.rb @@ -57,8 +57,6 @@ def self.by_market(market_id) end end - # return sale_array.inject(0) {|sum, sale.amount| sum + sale.amount} - #end - + end end diff --git a/spec/far_mar/sale_spec.rb b/spec/far_mar/sale_spec.rb index 1396e285..9d1636f8 100644 --- a/spec/far_mar/sale_spec.rb +++ b/spec/far_mar/sale_spec.rb @@ -50,4 +50,11 @@ expect(sale[0].id).to eq(3) end end + + describe "vendor" do + it "return the vendor the associated with the sale" do + expect(@sale.vendor.name).to eq("Breitenberg Inc") + end + + end end From fd36968911c468b0e0475b0a40bc9b3776785950 Mon Sep 17 00:00:00 2001 From: Tammy Date: Thu, 22 Oct 2015 08:08:53 -0700 Subject: [PATCH 17/23] FINISH OPTIONAL --- lib/far_mar/sale.rb | 6 +++++- spec/far_mar/sale_spec.rb | 8 +++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/far_mar/sale.rb b/lib/far_mar/sale.rb index 5305c605..e8c3973b 100644 --- a/lib/far_mar/sale.rb +++ b/lib/far_mar/sale.rb @@ -52,6 +52,10 @@ def self.between(beginnig_time, end_time) def vendor return FarMar::Vendor.find(self.vendor_id) end - + + def product + return FarMar::Product.find(self.product_id) + end + end end diff --git a/spec/far_mar/sale_spec.rb b/spec/far_mar/sale_spec.rb index 9d1636f8..e6a28435 100644 --- a/spec/far_mar/sale_spec.rb +++ b/spec/far_mar/sale_spec.rb @@ -52,9 +52,15 @@ end describe "vendor" do - it "return the vendor the associated with the sale" do + it "return the vendor of the sale" do expect(@sale.vendor.name).to eq("Breitenberg Inc") end + end + describe "product" do + it "return the product of the sale" do + expect(@sale.product.name).to eq("Yummy Fruit") + end end + end From 0062f8bf940f31dc58da3e4c7b3eb2579bfab9f4 Mon Sep 17 00:00:00 2001 From: Tammy Date: Thu, 22 Oct 2015 12:09:08 -0700 Subject: [PATCH 18/23] done with products --- lib/far_mar/market.rb | 4 ++++ lib/far_mar/product.rb | 13 +++++++++++-- lib/far_mar/vendor.rb | 21 +++++++++++++++++---- spec/far_mar/market_spec.rb | 10 ++++++++++ spec/far_mar/vendor_spec.rb | 4 ++++ 5 files changed, 46 insertions(+), 6 deletions(-) diff --git a/lib/far_mar/market.rb b/lib/far_mar/market.rb index 55172ab2..8baa6794 100644 --- a/lib/far_mar/market.rb +++ b/lib/far_mar/market.rb @@ -40,6 +40,10 @@ def vendors return FarMar::Vendor.market(self.id) end + def products + # return FarMar::Product.by_market(self.id) + return FarMar::Vendor.market(self.id, true) + end end diff --git a/lib/far_mar/product.rb b/lib/far_mar/product.rb index 7f2d1679..9a5e16a1 100644 --- a/lib/far_mar/product.rb +++ b/lib/far_mar/product.rb @@ -22,17 +22,26 @@ def self.all end def self.find(id) - product_array = self.all - product_array.find do |product| + @@product_all = self.all + @@product_all.find do |product| product.id == id end end def self.by_vendor(vendor_id) + @@product_all = self.all @@product_all.find_all do |product| product.vendor_id == vendor_id end end + # + # def self.by_market(market_id) + # @@product_all = self.all + # @@product_all.find_all do |product| + # product.market_id == market_id + # end + # end + def sales sales_array = FarMar::Sale.all diff --git a/lib/far_mar/vendor.rb b/lib/far_mar/vendor.rb index 3e8c598c..642489a6 100644 --- a/lib/far_mar/vendor.rb +++ b/lib/far_mar/vendor.rb @@ -29,10 +29,23 @@ def self.find(id) end end - def self.market(id) +# found all the vendors in the market - with default of salfe. +# if the test = true => it return all the products in that market. + def self.market(id, extra_test = false) @@array_all = self.all - @@array_all.find_all do |vendor| - vendor.market_id == id + if extra_test == false + @@array_all.find_all do |vendor| + vendor.market_id == id + end + else + final_array = [] + products_array = FarMar::Product.all + @@array_all.each do |vendor| + products_array.each do |product| + final_array.push(product) if (product.vendor_id == vendor.id) && (vendor.id == id) + end + end + return final_array end end @@ -57,6 +70,6 @@ def self.by_market(market_id) end end - + end end diff --git a/spec/far_mar/market_spec.rb b/spec/far_mar/market_spec.rb index afa6b43b..6a3b62a0 100644 --- a/spec/far_mar/market_spec.rb +++ b/spec/far_mar/market_spec.rb @@ -32,8 +32,18 @@ it "returns a collection of FarMar::Vendor" do expect(@market_array).to be_an_instance_of(Array) end + end + + describe "products" do + it "return an array of products" do + expect(@market.products).to be_an(Array) + end + it "return the right array" do + expect(@market.products[0].id).to eq 732 + end end + end # it "return vendors that associated with the market" diff --git a/spec/far_mar/vendor_spec.rb b/spec/far_mar/vendor_spec.rb index 9f7a9386..f5063f98 100644 --- a/spec/far_mar/vendor_spec.rb +++ b/spec/far_mar/vendor_spec.rb @@ -63,6 +63,10 @@ it "returns an Array" do expect(FarMar::Vendor.by_market(2)).to be_an_instance_of(Array) end + + it "return the right array" do + expect(FarMar::Vendor.by_market(2)[0].id).to eq 7 + end end From 35b66b15d033d36fd813a3e78dba53dcb8ded71d Mon Sep 17 00:00:00 2001 From: Tammy Date: Thu, 22 Oct 2015 14:09:11 -0700 Subject: [PATCH 19/23] done with prefered_vendor) --- lib/far_mar/market.rb | 3 +++ lib/far_mar/vendor.rb | 14 ++++++++++++++ spec/far_mar/market_spec.rb | 5 +++++ spec/far_mar/vendor_spec.rb | 6 ++++++ 4 files changed, 28 insertions(+) diff --git a/lib/far_mar/market.rb b/lib/far_mar/market.rb index 8baa6794..0c116b2b 100644 --- a/lib/far_mar/market.rb +++ b/lib/far_mar/market.rb @@ -45,6 +45,9 @@ def products return FarMar::Vendor.market(self.id, true) end + def prefered_vendor + return FarMar::Vendor.best_revenue(self.id) + end end end diff --git a/lib/far_mar/vendor.rb b/lib/far_mar/vendor.rb index 642489a6..8438e516 100644 --- a/lib/far_mar/vendor.rb +++ b/lib/far_mar/vendor.rb @@ -63,6 +63,20 @@ def revenue return sale_array.inject(0){|sum, sale| sum + sale.amount} end + def self.best_revenue(market_id) + vendors = self.by_market(market_id) + max = 0 + winner = nil + vendors.each do |vendor| + revenue = vendor.revenue.to_i + if revenue > max + max = revenue + winner = vendor + end + end + return winner + end + def self.by_market(market_id) @@array_all = self.all @@array_all.find_all do |vendor| diff --git a/spec/far_mar/market_spec.rb b/spec/far_mar/market_spec.rb index 6a3b62a0..fbe7cdf1 100644 --- a/spec/far_mar/market_spec.rb +++ b/spec/far_mar/market_spec.rb @@ -44,6 +44,11 @@ end end + describe "prefered_vendor" do + it "return the best seller vendor" do + expect(@market.prefered_vendor).to be_an_instance_of(FarMar::Vendor) + end + end end # it "return vendors that associated with the market" diff --git a/spec/far_mar/vendor_spec.rb b/spec/far_mar/vendor_spec.rb index f5063f98..948e5bab 100644 --- a/spec/far_mar/vendor_spec.rb +++ b/spec/far_mar/vendor_spec.rb @@ -69,6 +69,12 @@ end end + describe "best_revenue" do + it "return the vendor with the best revenue" do + expect(FarMar::Vendor.best_revenue(2)).to be_an_instance_of(FarMar::Vendor) + end + end + end From ca99c9a4f829930feaccf166644c3524aa450ab3 Mon Sep 17 00:00:00 2001 From: Tammy Date: Thu, 22 Oct 2015 15:22:02 -0700 Subject: [PATCH 20/23] done with worst_vendor --- lib/far_mar/vendor.rb | 14 ++++++++++++++ spec/far_mar/vendor_spec.rb | 8 ++++++++ 2 files changed, 22 insertions(+) diff --git a/lib/far_mar/vendor.rb b/lib/far_mar/vendor.rb index 8438e516..9c829870 100644 --- a/lib/far_mar/vendor.rb +++ b/lib/far_mar/vendor.rb @@ -77,6 +77,20 @@ def self.best_revenue(market_id) return winner end + def self.worst_revenue(market_id) + vendors = self.by_market(market_id) + min = 10000000000000000 + looser = nil + vendors.each do |vendor| + revenue = vendor.revenue.to_i + if revenue < min + min = revenue + looser = vendor + end + end + return looser + end + def self.by_market(market_id) @@array_all = self.all @@array_all.find_all do |vendor| diff --git a/spec/far_mar/vendor_spec.rb b/spec/far_mar/vendor_spec.rb index 948e5bab..7f16388b 100644 --- a/spec/far_mar/vendor_spec.rb +++ b/spec/far_mar/vendor_spec.rb @@ -75,6 +75,14 @@ end end + describe "worst_revenue" do + it "return the vendor with the worse revenue" do + expect(FarMar::Vendor.worst_revenue(2)).to be_an_instance_of(FarMar::Vendor) + end + end + + + end From 6b2210b31a3f9d285488a4146e497f2a2921f911 Mon Sep 17 00:00:00 2001 From: Tammy Date: Thu, 22 Oct 2015 15:28:29 -0700 Subject: [PATCH 21/23] now i REALLY did the worst vendor --- lib/far_mar/market.rb | 4 ++++ lib/far_mar/vendor.rb | 2 +- spec/far_mar/market_spec.rb | 6 ++++++ 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/far_mar/market.rb b/lib/far_mar/market.rb index 0c116b2b..373a6d34 100644 --- a/lib/far_mar/market.rb +++ b/lib/far_mar/market.rb @@ -49,5 +49,9 @@ def prefered_vendor return FarMar::Vendor.best_revenue(self.id) end + def worst_vendor + return FarMar::Vendor.worst_revenue(self.id) + end + end end diff --git a/lib/far_mar/vendor.rb b/lib/far_mar/vendor.rb index 9c829870..dc64171e 100644 --- a/lib/far_mar/vendor.rb +++ b/lib/far_mar/vendor.rb @@ -79,7 +79,7 @@ def self.best_revenue(market_id) def self.worst_revenue(market_id) vendors = self.by_market(market_id) - min = 10000000000000000 + min = 10000000000 looser = nil vendors.each do |vendor| revenue = vendor.revenue.to_i diff --git a/spec/far_mar/market_spec.rb b/spec/far_mar/market_spec.rb index fbe7cdf1..9b1f846e 100644 --- a/spec/far_mar/market_spec.rb +++ b/spec/far_mar/market_spec.rb @@ -49,6 +49,12 @@ expect(@market.prefered_vendor).to be_an_instance_of(FarMar::Vendor) end end + + describe "worst vendor" do + it "returns a vendor" do + expect(@market.worst_vendor).to be_an_instance_of(FarMar::Vendor) + end + end end # it "return vendors that associated with the market" From 152c369c97eb98e2e0db1675cb8c039551568af3 Mon Sep 17 00:00:00 2001 From: Tammy Date: Thu, 22 Oct 2015 20:48:09 -0700 Subject: [PATCH 22/23] covered the search method --- lib/far_mar/market.rb | 18 +++++++++++++++++- spec/far_mar/market_spec.rb | 7 +++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/lib/far_mar/market.rb b/lib/far_mar/market.rb index 373a6d34..28662c98 100644 --- a/lib/far_mar/market.rb +++ b/lib/far_mar/market.rb @@ -47,11 +47,27 @@ def products def prefered_vendor return FarMar::Vendor.best_revenue(self.id) - end + end def worst_vendor return FarMar::Vendor.worst_revenue(self.id) end + def self.search(search_term) + include_array =[] + vendor_array = FarMar::Vendor.all + vendor_array.each do |vendor| + if vendor.name.include?(search_term) + market = self.find(vendor.market_id) + if !inclde_array.iclude?(market) + include_array.push(market) + end + end + end + return include_array + end + + + end end diff --git a/spec/far_mar/market_spec.rb b/spec/far_mar/market_spec.rb index 9b1f846e..3d1ef6bc 100644 --- a/spec/far_mar/market_spec.rb +++ b/spec/far_mar/market_spec.rb @@ -55,8 +55,15 @@ expect(@market.worst_vendor).to be_an_instance_of(FarMar::Vendor) end end + + describe "search" do + it "retun an array of market " do + expect(FarMar::Market.search("Jefferson")).to be_an(Array) + end + end end + # it "return vendors that associated with the market" # expact(@market_array).to include # end From f60940588339e2d502017aef60cff06b02cd90f6 Mon Sep 17 00:00:00 2001 From: Tammy Date: Fri, 23 Oct 2015 14:05:53 -0700 Subject: [PATCH 23/23] done with search --- lib/far_mar/market.rb | 27 +++++++++++++++++++++++---- spec/far_mar/market_spec.rb | 14 ++++++++++---- 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/lib/far_mar/market.rb b/lib/far_mar/market.rb index 28662c98..64d16b95 100644 --- a/lib/far_mar/market.rb +++ b/lib/far_mar/market.rb @@ -57,17 +57,36 @@ def self.search(search_term) include_array =[] vendor_array = FarMar::Vendor.all vendor_array.each do |vendor| - if vendor.name.include?(search_term) + vendor_name = vendor.name + if vendor_name.include?(search_term) market = self.find(vendor.market_id) - if !inclde_array.iclude?(market) + if !(check_include(include_array, market)) + # !include_array.iclude?(market) include_array.push(market) end end end - return include_array + @@market_all = self.all + @@market_all.each do |market| + market_name = market.name + if market_name.include?(search_term) #working + # binding.pry + if (check_include(include_array, market) == false) + include_array.push(market) + end + end + end + return include_array end - + def self.check_include (include_array , market) + include_array.each do |mar| + if mar.name == market.name + return true + end + end + return false + end end end diff --git a/spec/far_mar/market_spec.rb b/spec/far_mar/market_spec.rb index 3d1ef6bc..df226e25 100644 --- a/spec/far_mar/market_spec.rb +++ b/spec/far_mar/market_spec.rb @@ -60,11 +60,17 @@ it "retun an array of market " do expect(FarMar::Market.search("Jefferson")).to be_an(Array) end + # + it "return the right array" do + array = FarMar::Market.search("Jefferson") + expect(array[0].id).to eq 6 + end + end end - -# it "return vendors that associated with the market" -# expact(@market_array).to include -# end +# +# # it "return vendors that associated with the market" +# # expact(@market_array).to include +# # end # end