From 4c054db8da40df07fdfe21585f73183fbecbd848 Mon Sep 17 00:00:00 2001 From: Riley Spicer Date: Mon, 19 Oct 2015 16:05:36 -0700 Subject: [PATCH 01/53] Began writing specs for FarMar::Market initialize --- lib/far_mar/market.rb | 16 +++++++++++++--- lib/far_mar/product.rb | 4 +++- lib/far_mar/sale.rb | 2 +- lib/far_mar/vendor.rb | 2 +- spec/far_mar/market_spec.rb | 5 ++++- spec/far_mar/product_spec.rb | 2 +- spec/product_spec.rb | 12 ------------ 7 files changed, 23 insertions(+), 20 deletions(-) delete mode 100644 spec/product_spec.rb diff --git a/lib/far_mar/market.rb b/lib/far_mar/market.rb index 1df3b33f..235633bd 100644 --- a/lib/far_mar/market.rb +++ b/lib/far_mar/market.rb @@ -1,11 +1,21 @@ -# require './support/markets.csv' +# CSV.read('./support/markets.csv') module FarMar class Market - def initialize - + def initialize(id, name, address, city, county, state, zip) + @id = id + @name = name + @address = address + @city = city + @county = county + @state = state + @zip = zip + end + + def self.all + CSV.read('./support/markets.csv') end end diff --git a/lib/far_mar/product.rb b/lib/far_mar/product.rb index b1a44aba..7817df8c 100644 --- a/lib/far_mar/product.rb +++ b/lib/far_mar/product.rb @@ -1,7 +1,9 @@ +# CSV.read("./support/products.csv") + module FarMar class Product end -end \ No newline at end of file +end diff --git a/lib/far_mar/sale.rb b/lib/far_mar/sale.rb index 0e1e3117..15c8fa48 100644 --- a/lib/far_mar/sale.rb +++ b/lib/far_mar/sale.rb @@ -1,4 +1,4 @@ -# require "./support/sales.csv" +# CSV.read("./support/sales.csv") module FarMar diff --git a/lib/far_mar/vendor.rb b/lib/far_mar/vendor.rb index 02459c18..9d23ef1d 100644 --- a/lib/far_mar/vendor.rb +++ b/lib/far_mar/vendor.rb @@ -1,4 +1,4 @@ -# require './suport/vendors.csv' +# CSV.read('./support/vendors.csv') module FarMar diff --git a/spec/far_mar/market_spec.rb b/spec/far_mar/market_spec.rb index dc2bf4e6..cb9d7265 100644 --- a/spec/far_mar/market_spec.rb +++ b/spec/far_mar/market_spec.rb @@ -2,11 +2,14 @@ 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 it 'can create an instance of a FarMar::Market' do expect(@market).to be_an_instance_of(FarMar::Market) end + it 'can retrieve the name of the market instance' do + expect(@market.name).to eq ("People's Co-op Farmers Market") + end end \ No newline at end of file diff --git a/spec/far_mar/product_spec.rb b/spec/far_mar/product_spec.rb index ce78770c..26da14b0 100644 --- a/spec/far_mar/product_spec.rb +++ b/spec/far_mar/product_spec.rb @@ -1,4 +1,4 @@ -# require "./suport/products.csv" + module FarMar diff --git a/spec/product_spec.rb b/spec/product_spec.rb deleted file mode 100644 index b953106d..00000000 --- a/spec/product_spec.rb +++ /dev/null @@ -1,12 +0,0 @@ -require 'spec_helper' - -describe FarMar::Product do - before :each do - @product = FarMar::Product.new - end - - it 'can create an instance of a FarMar::Product' do - expect(@product).to be_an_instance_of(FarMar::Product) - end - -end \ No newline at end of file From 8a8011582960961b2a6e947b954b7b2e81080f8d Mon Sep 17 00:00:00 2001 From: Riley Spicer Date: Mon, 19 Oct 2015 16:22:29 -0700 Subject: [PATCH 02/53] Writing specs and code for Market self.all --- lib/far_mar/market.rb | 8 +++++++- spec/far_mar/market_spec.rb | 33 ++++++++++++++++++++++++++------- 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/lib/far_mar/market.rb b/lib/far_mar/market.rb index 235633bd..6820f3e5 100644 --- a/lib/far_mar/market.rb +++ b/lib/far_mar/market.rb @@ -3,6 +3,7 @@ module FarMar class Market + attr_reader :id, :name, :address, :city, :county, :state, :zip def initialize(id, name, address, city, county, state, zip) @id = id @@ -15,7 +16,12 @@ def initialize(id, name, address, city, county, state, zip) end def self.all - CSV.read('./support/markets.csv') + market_array = [] + CSV.read('./support/markets.csv').each do |row| + market = FarMar::Market.new(row[0], row[1], row[2], row[3], row[4], row[5], row[6]) + market_array.push(market) + end + return market_array end end diff --git a/spec/far_mar/market_spec.rb b/spec/far_mar/market_spec.rb index cb9d7265..da0ab8b5 100644 --- a/spec/far_mar/market_spec.rb +++ b/spec/far_mar/market_spec.rb @@ -1,15 +1,34 @@ 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) - end - it 'can create an instance of a FarMar::Market' do - expect(@market).to be_an_instance_of(FarMar::Market) + describe 'initialize' do + before :each do + @market = FarMar::Market.new(1,"People's Co-op Farmers Market","30th and Burnside","Portland","Multnomah","Oregon",97202) + end + + it 'can create an instance of a FarMar::Market' do + expect(@market).to be_an_instance_of(FarMar::Market) + end + it 'can retrieve the name of the market instance' do + expect(@market.name).to eq ("People's Co-op Farmers Market") + end + it 'assigns the proper variable to state' do + expect(@market.state).to eq("Oregon") + end end - it 'can retrieve the name of the market instance' do - expect(@market.name).to eq ("People's Co-op Farmers Market") + + describe 'self.all' do + before :each do + @market_array = FarMar::Market.all + end + it 'returns an array of Market instances' do + expect(@market_array).to be_an_instance_of(Array) + end + it 'contains all the markets in the CSV file' do + + end end + end \ No newline at end of file From 889ab6d9fabc76c0fe26d60a3edb746045cfaf41 Mon Sep 17 00:00:00 2001 From: Riley Spicer Date: Mon, 19 Oct 2015 16:54:29 -0700 Subject: [PATCH 03/53] working on basic specs and code --- lib/far_mar/market.rb | 6 +++--- lib/far_mar/product.rb | 4 ++++ lib/far_mar/sale.rb | 9 +++++++++ lib/far_mar/vendor.rb | 10 ++++++++++ spec/far_mar/market_spec.rb | 11 +++++++---- spec/far_mar/product_spec.rb | 10 ++++++---- spec/far_mar/sale_spec.rb | 24 ++++++++++++++++++------ spec/far_mar/vendor_spec.rb | 17 +++++++++++------ 8 files changed, 68 insertions(+), 23 deletions(-) diff --git a/lib/far_mar/market.rb b/lib/far_mar/market.rb index 6820f3e5..050d908b 100644 --- a/lib/far_mar/market.rb +++ b/lib/far_mar/market.rb @@ -3,10 +3,10 @@ module FarMar class Market - attr_reader :id, :name, :address, :city, :county, :state, :zip + attr_reader :market_id, :name, :address, :city, :county, :state, :zip - def initialize(id, name, address, city, county, state, zip) - @id = id + def initialize(market_id, name, address, city, county, state, zip) + @market_id = market_id @name = name @address = address @city = city diff --git a/lib/far_mar/product.rb b/lib/far_mar/product.rb index 7817df8c..5b270513 100644 --- a/lib/far_mar/product.rb +++ b/lib/far_mar/product.rb @@ -4,6 +4,10 @@ module FarMar class Product + def initialize() + + end + end end diff --git a/lib/far_mar/sale.rb b/lib/far_mar/sale.rb index 15c8fa48..0b947ed1 100644 --- a/lib/far_mar/sale.rb +++ b/lib/far_mar/sale.rb @@ -3,6 +3,15 @@ module FarMar class Sale + attr_reader :sale_id, :amount, :purchase_time, :vendor_id, :product_id + + def initialize(sale_id, amount, purchase_time, vendor_id, product_id) + @sale_id = sale_id + @amount = amount + @purchase_time = purchase_time + @vendor_id = vendor_id + @product_id = product_id + end end diff --git a/lib/far_mar/vendor.rb b/lib/far_mar/vendor.rb index 9d23ef1d..f1f1f7dc 100644 --- a/lib/far_mar/vendor.rb +++ b/lib/far_mar/vendor.rb @@ -3,6 +3,16 @@ module FarMar class Vendor + attr_reader :vendor_id, :name, :employees, :market_id + + def initialize(vendor_id, name, employees, market_id) + @vendor_id = vendor_id + @name = name + @employees = employees + @market_id = market_id + end + + end diff --git a/spec/far_mar/market_spec.rb b/spec/far_mar/market_spec.rb index da0ab8b5..c0e34490 100644 --- a/spec/far_mar/market_spec.rb +++ b/spec/far_mar/market_spec.rb @@ -22,13 +22,16 @@ before :each do @market_array = FarMar::Market.all end - it 'returns an array of Market instances' do + it 'returns an array' do expect(@market_array).to be_an_instance_of(Array) end - it 'contains all the markets in the CSV file' do - + it 'contains FarMar::Market instances' do + length = @market_array.length - 1 + expect(@market_array[rand(0..length)]).to be_an_instance_of(FarMar::Market) + end + it 'does not have duplicate ids' do + end end - end \ No newline at end of file diff --git a/spec/far_mar/product_spec.rb b/spec/far_mar/product_spec.rb index 26da14b0..7458cbf9 100644 --- a/spec/far_mar/product_spec.rb +++ b/spec/far_mar/product_spec.rb @@ -1,9 +1,11 @@ +require 'spec_helper' +describe FarMar::Product do -module FarMar - - class Product - + describe 'initialize' do + it 'creates an instance of a FarMar::Product' do + + end end end \ No newline at end of file diff --git a/spec/far_mar/sale_spec.rb b/spec/far_mar/sale_spec.rb index 9271f030..dee737a4 100644 --- a/spec/far_mar/sale_spec.rb +++ b/spec/far_mar/sale_spec.rb @@ -1,12 +1,24 @@ require 'spec_helper' describe FarMar::Sale do - before :each do - @sale = FarMar::Sale.new - end + + describe 'initialize' do + before :each do + sale = FarMar::Sale.new + end - it 'can create an instance of a FarMar::Sale' do - expect(@sale).to be_an_instance_of(FarMar::Sale) - end + it 'can create an instance of a FarMar::Sale' do + expect(sale).to be_an_instance_of(FarMar::Sale) + end + end + + describe 'self.all' do + before :each do + sale = FarMar::Sale.all + end + it 'returns an array' do + expect(@sale_array).to be_an_instance_of(Array) + end + end end \ No newline at end of file diff --git a/spec/far_mar/vendor_spec.rb b/spec/far_mar/vendor_spec.rb index ec39f260..b76f3c00 100644 --- a/spec/far_mar/vendor_spec.rb +++ b/spec/far_mar/vendor_spec.rb @@ -1,12 +1,17 @@ require 'spec_helper' describe FarMar::Vendor do - before :each do - @vendor = FarMar::Vendor.new - end - - it 'can create an instance of a FarMar::Vendor' do - expect(@vendor).to be_an_instance_of(FarMar::Vendor) + + describe 'initialize' do + before :each do + @vendor = FarMar::Vendor.new(30,"Koelpin, Koelpin and Wintheiser",10,9) + end + it 'can create an instance of a FarMar::Vendor' do + expect(@vendor).to be_an_instance_of(FarMar::Vendor) + end + it 'can retrieve the id of a given instance' do + expect(@vendor.id).to eq(30) + end end end \ No newline at end of file From b43b2252bd7a18e11c292eb41d54fbeb643c1668 Mon Sep 17 00:00:00 2001 From: Riley Spicer Date: Tue, 20 Oct 2015 14:58:09 -0700 Subject: [PATCH 04/53] Sales specs for .all still failing, moved over to Vendors, three specs failing --- lib/far_mar/sale.rb | 12 +++++++++++- spec/far_mar/market_spec.rb | 4 ++-- spec/far_mar/product_spec.rb | 2 +- spec/far_mar/sale_spec.rb | 24 ++++++++++++++---------- spec/far_mar/vendor_spec.rb | 19 ++++++++++++++++++- 5 files changed, 46 insertions(+), 15 deletions(-) diff --git a/lib/far_mar/sale.rb b/lib/far_mar/sale.rb index 0b947ed1..7167f07a 100644 --- a/lib/far_mar/sale.rb +++ b/lib/far_mar/sale.rb @@ -8,11 +8,21 @@ class Sale def initialize(sale_id, amount, purchase_time, vendor_id, product_id) @sale_id = sale_id @amount = amount - @purchase_time = purchase_time + purchase_time = purchase_time.to_s + @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 + sales_array = [] + CSV.read('./support/markets.csv').each do |row| + sale = FarMar::Sale.new(row[0], row[1], row[2], row[3], row[4]) + sales_array.push(sale) + end + return sales_array + end + end end \ No newline at end of file diff --git a/spec/far_mar/market_spec.rb b/spec/far_mar/market_spec.rb index c0e34490..a81f2826 100644 --- a/spec/far_mar/market_spec.rb +++ b/spec/far_mar/market_spec.rb @@ -2,7 +2,7 @@ describe FarMar::Market do - describe 'initialize' do + describe '#initialize' do before :each do @market = FarMar::Market.new(1,"People's Co-op Farmers Market","30th and Burnside","Portland","Multnomah","Oregon",97202) end @@ -18,7 +18,7 @@ end end - describe 'self.all' do + describe '.all' do before :each do @market_array = FarMar::Market.all end diff --git a/spec/far_mar/product_spec.rb b/spec/far_mar/product_spec.rb index 7458cbf9..832680b1 100644 --- a/spec/far_mar/product_spec.rb +++ b/spec/far_mar/product_spec.rb @@ -2,7 +2,7 @@ describe FarMar::Product do - describe 'initialize' do + describe '#initialize' do it 'creates an instance of a FarMar::Product' do end diff --git a/spec/far_mar/sale_spec.rb b/spec/far_mar/sale_spec.rb index dee737a4..1414d788 100644 --- a/spec/far_mar/sale_spec.rb +++ b/spec/far_mar/sale_spec.rb @@ -2,23 +2,27 @@ describe FarMar::Sale do - describe 'initialize' do - before :each do - sale = FarMar::Sale.new - end - + describe '#initialize' do + before :each do + @sale = FarMar::Sale.new(5,4440,"2013-11-10 05:19:05 -0800",1,1) + end it 'can create an instance of a FarMar::Sale' do - expect(sale).to be_an_instance_of(FarMar::Sale) + expect(@sale).to be_an_instance_of(FarMar::Sale) end end - describe 'self.all' do - before :each do - sale = FarMar::Sale.all - end + describe '.all' do + before :each do + @sale_array = FarMar::Sale.all + end it 'returns an array' do expect(@sale_array).to be_an_instance_of(Array) end + # I don't know if the below spec is too specific or not, + # but I don't know how to generally test the length of a CSV file + it 'returns array containing all CSV info' do + expect(@sale_array.length).to eq(12798) + end end end \ No newline at end of file diff --git a/spec/far_mar/vendor_spec.rb b/spec/far_mar/vendor_spec.rb index b76f3c00..e2cf86b4 100644 --- a/spec/far_mar/vendor_spec.rb +++ b/spec/far_mar/vendor_spec.rb @@ -2,7 +2,7 @@ describe FarMar::Vendor do - describe 'initialize' do + describe '#initialize' do before :each do @vendor = FarMar::Vendor.new(30,"Koelpin, Koelpin and Wintheiser",10,9) end @@ -14,4 +14,21 @@ end end + describe '.all' do + before :each do + @vendor_array = FarMar::Vendor.all + end + it 'returns an array' do + expect(@vendor_array).to be_an_instance_of(Array) + end + # Would prefer to use a variable for CSV length, but not sure how to do that + it 'contains same number of vendors as orginal file' do + expect(@vendor_array.length).to eq(2690) + end + it 'only contains FarMar::Vendor instances' do + length = @vendor_array.length - 1 + expect(@vendor_array[rand(0..length)]).to be_an_instance_of(FarMar::Vendor) + end + end + end \ No newline at end of file From 369f1cef10bdb7d1042188451a46925476cd17fe Mon Sep 17 00:00:00 2001 From: Riley Spicer Date: Tue, 20 Oct 2015 15:00:58 -0700 Subject: [PATCH 05/53] Sale.rb fixed, tests running --- lib/far_mar/sale.rb | 2 +- lib/far_mar/vendor.rb | 8 +++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/far_mar/sale.rb b/lib/far_mar/sale.rb index 7167f07a..76da425a 100644 --- a/lib/far_mar/sale.rb +++ b/lib/far_mar/sale.rb @@ -16,7 +16,7 @@ def initialize(sale_id, amount, purchase_time, vendor_id, product_id) def self.all sales_array = [] - CSV.read('./support/markets.csv').each do |row| + CSV.read('./support/sales.csv').each do |row| sale = FarMar::Sale.new(row[0], row[1], row[2], row[3], row[4]) sales_array.push(sale) end diff --git a/lib/far_mar/vendor.rb b/lib/far_mar/vendor.rb index f1f1f7dc..d217baa0 100644 --- a/lib/far_mar/vendor.rb +++ b/lib/far_mar/vendor.rb @@ -12,7 +12,13 @@ def initialize(vendor_id, name, employees, market_id) @market_id = market_id end - + def self.all + vendor_array = [] + CSV.read('./support/vendors.csv').each do |row| + market = FarMar::Market.new(row[0], row[1], row[2], row[3], row[4], row[5], row[6]) + market_array.push(market) + end + end end From c2843c4b61c8c5b7225fd38e8f7f81093cb5f87f Mon Sep 17 00:00:00 2001 From: Riley Spicer Date: Tue, 20 Oct 2015 15:52:47 -0700 Subject: [PATCH 06/53] .all tests passing for Vendor --- lib/far_mar/vendor.rb | 5 +++-- spec/far_mar/vendor_spec.rb | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/far_mar/vendor.rb b/lib/far_mar/vendor.rb index d217baa0..76e00ba7 100644 --- a/lib/far_mar/vendor.rb +++ b/lib/far_mar/vendor.rb @@ -15,9 +15,10 @@ def initialize(vendor_id, name, employees, market_id) def self.all vendor_array = [] CSV.read('./support/vendors.csv').each do |row| - market = FarMar::Market.new(row[0], row[1], row[2], row[3], row[4], row[5], row[6]) - market_array.push(market) + vendor = FarMar::Vendor.new(row[0], row[1], row[2], row[3]) + vendor_array.push(vendor) end + return vendor_array end end diff --git a/spec/far_mar/vendor_spec.rb b/spec/far_mar/vendor_spec.rb index e2cf86b4..df88ecb3 100644 --- a/spec/far_mar/vendor_spec.rb +++ b/spec/far_mar/vendor_spec.rb @@ -10,7 +10,7 @@ expect(@vendor).to be_an_instance_of(FarMar::Vendor) end it 'can retrieve the id of a given instance' do - expect(@vendor.id).to eq(30) + expect(@vendor.vendor_id).to eq(30) end end From 916634c88fb917fa7d38fa1acf29ae40dac59c99 Mon Sep 17 00:00:00 2001 From: Riley Spicer Date: Tue, 20 Oct 2015 15:59:37 -0700 Subject: [PATCH 07/53] Failing tests for product specs (#init, .all) written --- spec/far_mar/product_spec.rb | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/spec/far_mar/product_spec.rb b/spec/far_mar/product_spec.rb index 832680b1..b67bf2af 100644 --- a/spec/far_mar/product_spec.rb +++ b/spec/far_mar/product_spec.rb @@ -3,8 +3,30 @@ describe FarMar::Product do describe '#initialize' do + before :each do + @product = FarMar::Product.new(14,"Stupendous Carrots",7) + end it 'creates an instance of a FarMar::Product' do - + expect(@product).to be_an_instance_of(FarMar::Product) + end + it 'can access #product_id' do + expect(@product.product_id).to eq(14) + end + it 'can access the #vendor_id' do + expect(@product.vendor_id).to eq(7) + end + end + + describe '.all' do + before :each do + @product_array = FarMar::Product.all + end + it 'returns an array' do + expect(@product_array).to be_an_instance_of(Array) + end + it 'contains only FarMar::Product instances' do + length = @product_array.length - 1 + expect(@product_array[rand(0..length)]).to be_an_instance_of(FarMar::Product) end end From 390737e71de0d4683c6e9534361c26e3b4ebb9d2 Mon Sep 17 00:00:00 2001 From: Riley Spicer Date: Tue, 20 Oct 2015 16:03:22 -0700 Subject: [PATCH 08/53] Product #initialize and .self tests passing. --- lib/far_mar/product.rb | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/lib/far_mar/product.rb b/lib/far_mar/product.rb index 5b270513..92dcef23 100644 --- a/lib/far_mar/product.rb +++ b/lib/far_mar/product.rb @@ -3,9 +3,21 @@ module FarMar class Product + attr_reader :product_id, :name, :vendor_id - def initialize() - + def initialize(product_id, name, vendor_id) + @product_id = product_id + @name = name.to_s + @vendor_id = vendor_id + end + + def self.all + product_array = [] + CSV.read('./support/products.csv').each do |row| + product = FarMar::Product.new(row[0], row[1], row[2]) + product_array.push(product) + end + return product_array end end From f31e8ebd7674c392325e4712f418162b2a666d69 Mon Sep 17 00:00:00 2001 From: Riley Spicer Date: Tue, 20 Oct 2015 16:19:03 -0700 Subject: [PATCH 09/53] Changed the way I'm testing length of csv to length of array created to make it better, hooray --- lib/far_mar/vendor.rb | 6 ++++++ spec/far_mar/market_spec.rb | 7 ++++--- spec/far_mar/product_spec.rb | 4 ++++ spec/far_mar/sale_spec.rb | 7 +++---- spec/far_mar/vendor_spec.rb | 11 +++++++++-- 5 files changed, 26 insertions(+), 9 deletions(-) diff --git a/lib/far_mar/vendor.rb b/lib/far_mar/vendor.rb index 76e00ba7..a0dd1b9f 100644 --- a/lib/far_mar/vendor.rb +++ b/lib/far_mar/vendor.rb @@ -21,6 +21,12 @@ def self.all return vendor_array end + def self.find(id) + vendor_array = self.all + vendor = vendor_array.find { |ven| ven.vendor_id == id } + return vendor + end + end end \ No newline at end of file diff --git a/spec/far_mar/market_spec.rb b/spec/far_mar/market_spec.rb index a81f2826..689115ea 100644 --- a/spec/far_mar/market_spec.rb +++ b/spec/far_mar/market_spec.rb @@ -25,12 +25,13 @@ it 'returns an array' do expect(@market_array).to be_an_instance_of(Array) end - it 'contains FarMar::Market instances' do + it 'contains only FarMar::Market instances' do length = @market_array.length - 1 expect(@market_array[rand(0..length)]).to be_an_instance_of(FarMar::Market) end - it 'does not have duplicate ids' do - + it 'contains all data from CSV file' do + csv = CSV.read("support/markets.csv") + expect(@market_array.length).to eq(csv.length) end end diff --git a/spec/far_mar/product_spec.rb b/spec/far_mar/product_spec.rb index b67bf2af..eadd2fbc 100644 --- a/spec/far_mar/product_spec.rb +++ b/spec/far_mar/product_spec.rb @@ -28,6 +28,10 @@ length = @product_array.length - 1 expect(@product_array[rand(0..length)]).to be_an_instance_of(FarMar::Product) end + it 'contains all products from CSV' do + csv = CSV.read("support/products.csv") + expect(@product_array.length).to eq csv.length + end end end \ No newline at end of file diff --git a/spec/far_mar/sale_spec.rb b/spec/far_mar/sale_spec.rb index 1414d788..c81e52cb 100644 --- a/spec/far_mar/sale_spec.rb +++ b/spec/far_mar/sale_spec.rb @@ -18,10 +18,9 @@ it 'returns an array' do expect(@sale_array).to be_an_instance_of(Array) end - # I don't know if the below spec is too specific or not, - # but I don't know how to generally test the length of a CSV file - it 'returns array containing all CSV info' do - expect(@sale_array.length).to eq(12798) + it 'contains all sales from CSV' do + csv = CSV.read("support/sales.csv") + expect(@sale_array.length).to eq csv.length end end diff --git a/spec/far_mar/vendor_spec.rb b/spec/far_mar/vendor_spec.rb index df88ecb3..85c094de 100644 --- a/spec/far_mar/vendor_spec.rb +++ b/spec/far_mar/vendor_spec.rb @@ -21,9 +21,9 @@ it 'returns an array' do expect(@vendor_array).to be_an_instance_of(Array) end - # Would prefer to use a variable for CSV length, but not sure how to do that it 'contains same number of vendors as orginal file' do - expect(@vendor_array.length).to eq(2690) + csv = CSV.read("support/vendors.csv") + expect(@vendor_array.length).to eq(csv.length) end it 'only contains FarMar::Vendor instances' do length = @vendor_array.length - 1 @@ -31,4 +31,11 @@ end end + describe '.find(id)' do + it 'returns the vendor for a given id' do + vendor = [10,"Kertzmann LLC",11,3] + expect(FarMar::Vendor.find(10)).to eq(vendor) + end + end + end \ No newline at end of file From 44440bcbce7a30d45a712f7712f4fb8aaa42a34c Mon Sep 17 00:00:00 2001 From: Riley Spicer Date: Tue, 20 Oct 2015 16:55:42 -0700 Subject: [PATCH 10/53] Fixed the find.all nonsense! Vendor specs passing --- lib/far_mar.rb | 1 + lib/far_mar/vendor.rb | 11 ++++++----- spec/far_mar/vendor_spec.rb | 6 +++--- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/lib/far_mar.rb b/lib/far_mar.rb index f259ba84..b03a97e0 100644 --- a/lib/far_mar.rb +++ b/lib/far_mar.rb @@ -1,5 +1,6 @@ require 'csv' require 'time' +require 'pry' require './lib/far_mar/market' require './lib/far_mar/vendor' require './lib/far_mar/sale' diff --git a/lib/far_mar/vendor.rb b/lib/far_mar/vendor.rb index a0dd1b9f..8d10fae4 100644 --- a/lib/far_mar/vendor.rb +++ b/lib/far_mar/vendor.rb @@ -6,10 +6,10 @@ class Vendor attr_reader :vendor_id, :name, :employees, :market_id def initialize(vendor_id, name, employees, market_id) - @vendor_id = vendor_id + @vendor_id = vendor_id.to_i @name = name @employees = employees - @market_id = market_id + @market_id = market_id.to_i end def self.all @@ -22,9 +22,10 @@ def self.all end def self.find(id) - vendor_array = self.all - vendor = vendor_array.find { |ven| ven.vendor_id == id } - return vendor + vendor_array = FarMar::Vendor.all + vendor = vendor_array.find do |vendor| + vendor.vendor_id == id + end end end diff --git a/spec/far_mar/vendor_spec.rb b/spec/far_mar/vendor_spec.rb index 85c094de..19bc0b50 100644 --- a/spec/far_mar/vendor_spec.rb +++ b/spec/far_mar/vendor_spec.rb @@ -32,9 +32,9 @@ end describe '.find(id)' do - it 'returns the vendor for a given id' do - vendor = [10,"Kertzmann LLC",11,3] - expect(FarMar::Vendor.find(10)).to eq(vendor) + it 'returns a vendor instance' do + vendor = FarMar::Vendor.new(10,"Kertzmann LLC",11,3) + expect(FarMar::Vendor.find(10).name).to eq("Kertzmann LLC") end end From 42289c3681e0bec289a5e094d476f352e1a1e372 Mon Sep 17 00:00:00 2001 From: Riley Spicer Date: Tue, 20 Oct 2015 17:03:33 -0700 Subject: [PATCH 11/53] Market .find(id) method passing tests --- lib/far_mar/market.rb | 11 +++++++++-- lib/far_mar/vendor.rb | 5 +---- spec/far_mar/market_spec.rb | 6 ++++++ spec/far_mar/vendor_spec.rb | 1 - 4 files changed, 16 insertions(+), 7 deletions(-) diff --git a/lib/far_mar/market.rb b/lib/far_mar/market.rb index 050d908b..79bb83d4 100644 --- a/lib/far_mar/market.rb +++ b/lib/far_mar/market.rb @@ -6,13 +6,13 @@ class Market attr_reader :market_id, :name, :address, :city, :county, :state, :zip def initialize(market_id, name, address, city, county, state, zip) - @market_id = market_id + @market_id = market_id.to_i @name = name @address = address @city = city @county = county @state = state - @zip = zip + @zip = zip end def self.all @@ -24,6 +24,13 @@ def self.all return market_array end + def self.find(id) + FarMar::Market.all.find {|mar| mar.market_id == id } + + + end + + end end \ No newline at end of file diff --git a/lib/far_mar/vendor.rb b/lib/far_mar/vendor.rb index 8d10fae4..eff875bf 100644 --- a/lib/far_mar/vendor.rb +++ b/lib/far_mar/vendor.rb @@ -22,10 +22,7 @@ def self.all end def self.find(id) - vendor_array = FarMar::Vendor.all - vendor = vendor_array.find do |vendor| - vendor.vendor_id == id - end + FarMar::Vendor.all.find {|ven| ven.vendor_id == id} end end diff --git a/spec/far_mar/market_spec.rb b/spec/far_mar/market_spec.rb index 689115ea..219cd2c4 100644 --- a/spec/far_mar/market_spec.rb +++ b/spec/far_mar/market_spec.rb @@ -35,4 +35,10 @@ end end + describe '.find(id)' do + it 'returns a matching market instance' do + expect(FarMar::Market.find(16).zip).to eq("60546") + end + end + end \ No newline at end of file diff --git a/spec/far_mar/vendor_spec.rb b/spec/far_mar/vendor_spec.rb index 19bc0b50..38e4d8f3 100644 --- a/spec/far_mar/vendor_spec.rb +++ b/spec/far_mar/vendor_spec.rb @@ -33,7 +33,6 @@ describe '.find(id)' do it 'returns a vendor instance' do - vendor = FarMar::Vendor.new(10,"Kertzmann LLC",11,3) expect(FarMar::Vendor.find(10).name).to eq("Kertzmann LLC") end end From a8001f6c859f15a0ab5015bc6a1e777f62ceb3d0 Mon Sep 17 00:00:00 2001 From: Riley Spicer Date: Wed, 21 Oct 2015 10:09:26 -0700 Subject: [PATCH 12/53] Faled tests for Sale .find spec added --- spec/far_mar/sale_spec.rb | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/spec/far_mar/sale_spec.rb b/spec/far_mar/sale_spec.rb index c81e52cb..d0cb451f 100644 --- a/spec/far_mar/sale_spec.rb +++ b/spec/far_mar/sale_spec.rb @@ -24,4 +24,10 @@ end end + describe '.find' do + it 'returns a matching sale instance' do + expect(FarMar::Sale.find(15).product_id).to be eq(4) + end + end + end \ No newline at end of file From 0654dd9c7e79d7f75f2871940d7b1259d593cc44 Mon Sep 17 00:00:00 2001 From: Riley Spicer Date: Wed, 21 Oct 2015 10:18:35 -0700 Subject: [PATCH 13/53] Code written, spec fixed, test passing for Sale.find --- lib/far_mar/market.rb | 2 -- lib/far_mar/sale.rb | 13 +++++++++---- spec/far_mar/sale_spec.rb | 2 +- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/lib/far_mar/market.rb b/lib/far_mar/market.rb index 79bb83d4..aa026d9a 100644 --- a/lib/far_mar/market.rb +++ b/lib/far_mar/market.rb @@ -26,8 +26,6 @@ def self.all def self.find(id) FarMar::Market.all.find {|mar| mar.market_id == id } - - end diff --git a/lib/far_mar/sale.rb b/lib/far_mar/sale.rb index 76da425a..49b7a11a 100644 --- a/lib/far_mar/sale.rb +++ b/lib/far_mar/sale.rb @@ -6,12 +6,12 @@ class Sale attr_reader :sale_id, :amount, :purchase_time, :vendor_id, :product_id def initialize(sale_id, amount, purchase_time, vendor_id, product_id) - @sale_id = sale_id - @amount = amount + @sale_id = sale_id.to_i + @amount = amount.to_i purchase_time = purchase_time.to_s @purchase_time = DateTime.strptime(purchase_time, "%Y-%m-%d %H:%M:%S %z") - @vendor_id = vendor_id - @product_id = product_id + @vendor_id = vendor_id.to_i + @product_id = product_id.to_i end def self.all @@ -23,6 +23,11 @@ def self.all return sales_array end + def self.find(id) + FarMar::Sale.all.find {|sale| + sale.sale_id == id} + end + end end \ No newline at end of file diff --git a/spec/far_mar/sale_spec.rb b/spec/far_mar/sale_spec.rb index d0cb451f..4f1cb9d9 100644 --- a/spec/far_mar/sale_spec.rb +++ b/spec/far_mar/sale_spec.rb @@ -26,7 +26,7 @@ describe '.find' do it 'returns a matching sale instance' do - expect(FarMar::Sale.find(15).product_id).to be eq(4) + expect(FarMar::Sale.find(15).amount).to eq(8924) end end From cc4cc6f1d7104b413c8543b182e52c76c2812ba0 Mon Sep 17 00:00:00 2001 From: Riley Spicer Date: Wed, 21 Oct 2015 10:19:38 -0700 Subject: [PATCH 14/53] Spec for Product.find written and failing --- spec/far_mar/product_spec.rb | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/spec/far_mar/product_spec.rb b/spec/far_mar/product_spec.rb index eadd2fbc..3907589c 100644 --- a/spec/far_mar/product_spec.rb +++ b/spec/far_mar/product_spec.rb @@ -34,4 +34,10 @@ end end + describe '.find' do + it 'returns a matching product instance' do + expect(FarMar::Product.find(10).name).to eq("Black Apples") + end + end + end \ No newline at end of file From 6795f3b3fbd5522cc694b770f6fe95c108fb2a1e Mon Sep 17 00:00:00 2001 From: Riley Spicer Date: Wed, 21 Oct 2015 10:20:53 -0700 Subject: [PATCH 15/53] Product.find code written and passing tests --- lib/far_mar/product.rb | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/far_mar/product.rb b/lib/far_mar/product.rb index 92dcef23..770bfc42 100644 --- a/lib/far_mar/product.rb +++ b/lib/far_mar/product.rb @@ -6,9 +6,9 @@ class Product attr_reader :product_id, :name, :vendor_id def initialize(product_id, name, vendor_id) - @product_id = product_id + @product_id = product_id.to_i @name = name.to_s - @vendor_id = vendor_id + @vendor_id = vendor_id.to_i end def self.all @@ -20,6 +20,10 @@ def self.all return product_array end + def self.find(id) + FarMar::Product.all.find {|pro| pro.product_id == id} + end + end end From 5b1e8d24ba2ec83476b8145ca0e1531d49336540 Mon Sep 17 00:00:00 2001 From: Riley Spicer Date: Wed, 21 Oct 2015 10:32:38 -0700 Subject: [PATCH 16/53] Market.vendors specs written and failing --- spec/far_mar/market_spec.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/spec/far_mar/market_spec.rb b/spec/far_mar/market_spec.rb index 219cd2c4..0a751522 100644 --- a/spec/far_mar/market_spec.rb +++ b/spec/far_mar/market_spec.rb @@ -41,4 +41,14 @@ end end + describe '.vendors(id)' do + it 'returns an array' do + length = FarMar::Market.all.length - 1 + expect(FarMar::Market.vendors(rand(0..length))).to be_an_instance_of(Array) + end + it 'returns all FarMar::Vendor instances for a market id' do + expect(FarMar::Market.vendors(1).length).to eq([6]) + end + end + end \ No newline at end of file From bc437e7ee503ea51c355b7f9c05c8711c184dc13 Mon Sep 17 00:00:00 2001 From: Riley Spicer Date: Wed, 21 Oct 2015 10:37:48 -0700 Subject: [PATCH 17/53] Reworked specs slightly, code written and passing for Market.vendors --- lib/far_mar/market.rb | 4 ++++ lib/far_mar/sale.rb | 3 +-- spec/far_mar/market_spec.rb | 2 +- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/far_mar/market.rb b/lib/far_mar/market.rb index aa026d9a..b6fa408c 100644 --- a/lib/far_mar/market.rb +++ b/lib/far_mar/market.rb @@ -28,6 +28,10 @@ def self.find(id) FarMar::Market.all.find {|mar| mar.market_id == id } end + def self.vendors(id) + FarMar::Vendor.all.find_all {|ven| ven.market_id == id } + end + end diff --git a/lib/far_mar/sale.rb b/lib/far_mar/sale.rb index 49b7a11a..ef3943e8 100644 --- a/lib/far_mar/sale.rb +++ b/lib/far_mar/sale.rb @@ -24,8 +24,7 @@ def self.all end def self.find(id) - FarMar::Sale.all.find {|sale| - sale.sale_id == id} + FarMar::Sale.all.find {|sale| sale.sale_id == id} end end diff --git a/spec/far_mar/market_spec.rb b/spec/far_mar/market_spec.rb index 0a751522..60c929ba 100644 --- a/spec/far_mar/market_spec.rb +++ b/spec/far_mar/market_spec.rb @@ -47,7 +47,7 @@ expect(FarMar::Market.vendors(rand(0..length))).to be_an_instance_of(Array) end it 'returns all FarMar::Vendor instances for a market id' do - expect(FarMar::Market.vendors(1).length).to eq([6]) + expect(FarMar::Market.vendors(1).length).to eq(6) end end From 92d99e85869404540e7e5e31cdd3236ad7d5781f Mon Sep 17 00:00:00 2001 From: Riley Spicer Date: Wed, 21 Oct 2015 10:55:15 -0700 Subject: [PATCH 18/53] Specs written and failing for Vendor.prodcuts() --- lib/far_mar/market.rb | 4 ++-- lib/far_mar/vendor.rb | 14 +++++++++----- spec/far_mar/vendor_spec.rb | 17 +++++++++++++++++ 3 files changed, 28 insertions(+), 7 deletions(-) diff --git a/lib/far_mar/market.rb b/lib/far_mar/market.rb index b6fa408c..c13dbe07 100644 --- a/lib/far_mar/market.rb +++ b/lib/far_mar/market.rb @@ -28,8 +28,8 @@ def self.find(id) FarMar::Market.all.find {|mar| mar.market_id == id } end - def self.vendors(id) - FarMar::Vendor.all.find_all {|ven| ven.market_id == id } + def self.vendors(market_id) + FarMar::Vendor.all.find_all {|ven| ven.market == market_id } end diff --git a/lib/far_mar/vendor.rb b/lib/far_mar/vendor.rb index eff875bf..30b42438 100644 --- a/lib/far_mar/vendor.rb +++ b/lib/far_mar/vendor.rb @@ -3,13 +3,13 @@ module FarMar class Vendor - attr_reader :vendor_id, :name, :employees, :market_id + attr_reader :vendor_id, :name, :employees, :market - def initialize(vendor_id, name, employees, market_id) + def initialize(vendor_id, name, employees, market) @vendor_id = vendor_id.to_i @name = name @employees = employees - @market_id = market_id.to_i + @market = market.to_i end def self.all @@ -21,8 +21,12 @@ def self.all return vendor_array end - def self.find(id) - FarMar::Vendor.all.find {|ven| ven.vendor_id == id} + def self.find(vendor_id) + FarMar::Vendor.all.find {|ven| ven.vendor_id == vendor_id} + end + + def products(vendor_id) + end end diff --git a/spec/far_mar/vendor_spec.rb b/spec/far_mar/vendor_spec.rb index 38e4d8f3..58aff347 100644 --- a/spec/far_mar/vendor_spec.rb +++ b/spec/far_mar/vendor_spec.rb @@ -37,4 +37,21 @@ end end + describe '#market' do + vendor = FarMar::Vendor.new(5,"Reynolds, Schmitt and Klocko",3,1) + it 'returns the market associated with Vendor instance' do + expect(vendor.market).to eq(1) + end + end + + describe '.products()' do + it 'returns an array' do + length = FarMar::Vendor.all.length - 1 + expect(FarMar::Vendor.products(rand(0..length))).to be_an_instance_of(Array) + end + it 'returns all products for a vendor' do + expect(FarMar::Vendor.products(5).lenght).to eq(3) + end + end + end \ No newline at end of file From 96aac56fb4b9057098fc1375f38a6d174ca52f57 Mon Sep 17 00:00:00 2001 From: Riley Spicer Date: Wed, 21 Oct 2015 10:58:00 -0700 Subject: [PATCH 19/53] Vendor.products(id) passing tests --- lib/far_mar/vendor.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/far_mar/vendor.rb b/lib/far_mar/vendor.rb index 30b42438..3c16c72a 100644 --- a/lib/far_mar/vendor.rb +++ b/lib/far_mar/vendor.rb @@ -25,8 +25,8 @@ def self.find(vendor_id) FarMar::Vendor.all.find {|ven| ven.vendor_id == vendor_id} end - def products(vendor_id) - + def self.products(vendor_id) + FarMar::Product.all.find_all { |pro| pro.vendor_id == vendor_id} end end From c6969059ebbf39a6183a9c81623e8e3ac22df731 Mon Sep 17 00:00:00 2001 From: Riley Spicer Date: Wed, 21 Oct 2015 11:25:28 -0700 Subject: [PATCH 20/53] Vendor.sales specs written and failing --- spec/far_mar/vendor_spec.rb | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/spec/far_mar/vendor_spec.rb b/spec/far_mar/vendor_spec.rb index 58aff347..a141e163 100644 --- a/spec/far_mar/vendor_spec.rb +++ b/spec/far_mar/vendor_spec.rb @@ -44,13 +44,26 @@ end end - describe '.products()' do + describe '.products(vendor_id)' do it 'returns an array' do length = FarMar::Vendor.all.length - 1 expect(FarMar::Vendor.products(rand(0..length))).to be_an_instance_of(Array) end it 'returns all products for a vendor' do - expect(FarMar::Vendor.products(5).lenght).to eq(3) + expect(FarMar::Vendor.products(5).length).to eq(3) + end + # I feel like I could use let for the length variable, + # but I'm having trouble figure it out + it 'contains only Product instances'do + length = FarMar::Vendor.all.length - 1 + expect(FarMar::Vendor.products(rand(0..length))[0]).to be_an_instance_of(FarMar::Product) + end + end + + describe '.sales(vendor_id)' do + it 'returns all sales per vendor' do + expect(FarMar::Vendor.sales(6).length).to eq(1) + expect(FarMar::Vendor.sales(15).length).to eq(7) end end From 77a2ffe895075c4d36f2c8ffcabc65b0c6249eca Mon Sep 17 00:00:00 2001 From: Riley Spicer Date: Wed, 21 Oct 2015 11:29:17 -0700 Subject: [PATCH 21/53] Vendor.sales code wirrten and passing --- lib/far_mar/vendor.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/far_mar/vendor.rb b/lib/far_mar/vendor.rb index 3c16c72a..755e54fd 100644 --- a/lib/far_mar/vendor.rb +++ b/lib/far_mar/vendor.rb @@ -29,6 +29,10 @@ def self.products(vendor_id) FarMar::Product.all.find_all { |pro| pro.vendor_id == vendor_id} end + def self.sales(vendor_id) + FarMar::Sale.all.find_all { |sale| sale.vendor_id == vendor_id } + end + end end \ No newline at end of file From ca629ab617d6710b93a61a38f49b6ce04b7fcf11 Mon Sep 17 00:00:00 2001 From: Riley Spicer Date: Wed, 21 Oct 2015 13:40:06 -0700 Subject: [PATCH 22/53] Changed .all functions to use class variables, cutting down on test time --- lib/far_mar/market.rb | 12 +++++++----- lib/far_mar/product.rb | 12 +++++++----- lib/far_mar/sale.rb | 12 +++++++----- lib/far_mar/vendor.rb | 18 ++++++++++-------- 4 files changed, 31 insertions(+), 23 deletions(-) diff --git a/lib/far_mar/market.rb b/lib/far_mar/market.rb index c13dbe07..d61452a6 100644 --- a/lib/far_mar/market.rb +++ b/lib/far_mar/market.rb @@ -16,12 +16,14 @@ def initialize(market_id, name, address, city, county, state, zip) end def self.all - market_array = [] - CSV.read('./support/markets.csv').each do |row| - market = FarMar::Market.new(row[0], row[1], row[2], row[3], row[4], row[5], row[6]) - market_array.push(market) + @@market_array ||= [] + if @@market_array == [] + CSV.read('./support/markets.csv').each do |row| + market = FarMar::Market.new(row[0], row[1], row[2], row[3], row[4], row[5], row[6]) + @@market_array.push(market) + end end - return market_array + return @@market_array end def self.find(id) diff --git a/lib/far_mar/product.rb b/lib/far_mar/product.rb index 770bfc42..1e5c57e9 100644 --- a/lib/far_mar/product.rb +++ b/lib/far_mar/product.rb @@ -12,12 +12,14 @@ def initialize(product_id, name, vendor_id) end def self.all - product_array = [] - CSV.read('./support/products.csv').each do |row| - product = FarMar::Product.new(row[0], row[1], row[2]) - product_array.push(product) + @@product_array ||= [] + if @@product_array == [] + CSV.read('./support/products.csv').each do |row| + product = FarMar::Product.new(row[0], row[1], row[2]) + @@product_array.push(product) + end end - return product_array + return @@product_array end def self.find(id) diff --git a/lib/far_mar/sale.rb b/lib/far_mar/sale.rb index ef3943e8..63a02619 100644 --- a/lib/far_mar/sale.rb +++ b/lib/far_mar/sale.rb @@ -15,12 +15,14 @@ def initialize(sale_id, amount, purchase_time, vendor_id, product_id) end def self.all - sales_array = [] - CSV.read('./support/sales.csv').each do |row| - sale = FarMar::Sale.new(row[0], row[1], row[2], row[3], row[4]) - sales_array.push(sale) + @@sales_array ||= [] + if @@sales_array = [] + CSV.read('./support/sales.csv').each do |row| + sale = FarMar::Sale.new(row[0], row[1], row[2], row[3], row[4]) + @@sales_array.push(sale) + end end - return sales_array + return @@sales_array end def self.find(id) diff --git a/lib/far_mar/vendor.rb b/lib/far_mar/vendor.rb index 755e54fd..0b4b7283 100644 --- a/lib/far_mar/vendor.rb +++ b/lib/far_mar/vendor.rb @@ -13,20 +13,22 @@ def initialize(vendor_id, name, employees, market) end def self.all - vendor_array = [] - CSV.read('./support/vendors.csv').each do |row| - vendor = FarMar::Vendor.new(row[0], row[1], row[2], row[3]) - vendor_array.push(vendor) - end - return vendor_array + @@vendor_array ||= [] + if @@vendor_array == [] + CSV.read('./support/vendors.csv').each do |row| + vendor = FarMar::Vendor.new(row[0], row[1], row[2], row[3]) + @@vendor_array.push(vendor) + end + end + return @@vendor_array end def self.find(vendor_id) FarMar::Vendor.all.find {|ven| ven.vendor_id == vendor_id} end - def self.products(vendor_id) - FarMar::Product.all.find_all { |pro| pro.vendor_id == vendor_id} + def products + FarMar::Product.all.find_all { |pro| pro.vendor_id == @vendor_id} end def self.sales(vendor_id) From 8867fa6dec872708f3405713199b299aa5d51d1f Mon Sep 17 00:00:00 2001 From: Riley Spicer Date: Wed, 21 Oct 2015 14:03:34 -0700 Subject: [PATCH 23/53] Rewriting methods that i realize need to be instance not class --- spec/far_mar/vendor_spec.rb | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/spec/far_mar/vendor_spec.rb b/spec/far_mar/vendor_spec.rb index a141e163..9f064b5e 100644 --- a/spec/far_mar/vendor_spec.rb +++ b/spec/far_mar/vendor_spec.rb @@ -44,19 +44,18 @@ end end - describe '.products(vendor_id)' do - it 'returns an array' do - length = FarMar::Vendor.all.length - 1 - expect(FarMar::Vendor.products(rand(0..length))).to be_an_instance_of(Array) + describe '#products' do + before :each do + @vendor = FarMar::Vendor.new(12,"Windler Inc",4,3) end it 'returns all products for a vendor' do - expect(FarMar::Vendor.products(5).length).to eq(3) + expect(@vendor.products.length).to eq(3) end # I feel like I could use let for the length variable, # but I'm having trouble figure it out it 'contains only Product instances'do - length = FarMar::Vendor.all.length - 1 - expect(FarMar::Vendor.products(rand(0..length))[0]).to be_an_instance_of(FarMar::Product) + length = @vendor.products.length - 1 + expect(@vendor.products[0]).to be_an_instance_of(FarMar::Product) end end @@ -67,4 +66,12 @@ end end + describe '.by_market' do + + end + + describe '.revenue(vendor_id)' do + + end + end \ No newline at end of file From 4e360ef31eac533a13031d446beac8adef7e942a Mon Sep 17 00:00:00 2001 From: Riley Spicer Date: Wed, 21 Oct 2015 14:08:54 -0700 Subject: [PATCH 24/53] Fixed Vendor.sales to #sales --- lib/far_mar/vendor.rb | 2 +- spec/far_mar/vendor_spec.rb | 8 +++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/far_mar/vendor.rb b/lib/far_mar/vendor.rb index 0b4b7283..db12d02c 100644 --- a/lib/far_mar/vendor.rb +++ b/lib/far_mar/vendor.rb @@ -31,7 +31,7 @@ def products FarMar::Product.all.find_all { |pro| pro.vendor_id == @vendor_id} end - def self.sales(vendor_id) + def sales FarMar::Sale.all.find_all { |sale| sale.vendor_id == vendor_id } end diff --git a/spec/far_mar/vendor_spec.rb b/spec/far_mar/vendor_spec.rb index 9f064b5e..af1fad03 100644 --- a/spec/far_mar/vendor_spec.rb +++ b/spec/far_mar/vendor_spec.rb @@ -59,10 +59,12 @@ end end - describe '.sales(vendor_id)' do + describe '#sales' do + before :each do + @vendor = FarMar::Vendor.new(12,"Windler Inc",4,3) + end it 'returns all sales per vendor' do - expect(FarMar::Vendor.sales(6).length).to eq(1) - expect(FarMar::Vendor.sales(15).length).to eq(7) + expect(@vendor.sales.length).to eq(3) end end From 699ea9036813d3bb36c3a7290a431f2f00b6ebe9 Mon Sep 17 00:00:00 2001 From: Riley Spicer Date: Wed, 21 Oct 2015 14:18:07 -0700 Subject: [PATCH 25/53] Fixed Market class methods that are now instance methods. --- lib/far_mar/market.rb | 2 +- spec/far_mar/market_spec.rb | 10 ++++++---- spec/far_mar/vendor_spec.rb | 8 +++++--- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/lib/far_mar/market.rb b/lib/far_mar/market.rb index d61452a6..f6e1aa57 100644 --- a/lib/far_mar/market.rb +++ b/lib/far_mar/market.rb @@ -30,7 +30,7 @@ def self.find(id) FarMar::Market.all.find {|mar| mar.market_id == id } end - def self.vendors(market_id) + def vendors FarMar::Vendor.all.find_all {|ven| ven.market == market_id } end diff --git a/spec/far_mar/market_spec.rb b/spec/far_mar/market_spec.rb index 60c929ba..f7026b12 100644 --- a/spec/far_mar/market_spec.rb +++ b/spec/far_mar/market_spec.rb @@ -41,13 +41,15 @@ end end - describe '.vendors(id)' do + describe 'vendors' do + before :each do + @market = FarMar::Market.new(7,"Petaluma Evening Farmers' Market","1 2nd Street","Petaluma","Sonoma","California",94952) + end it 'returns an array' do - length = FarMar::Market.all.length - 1 - expect(FarMar::Market.vendors(rand(0..length))).to be_an_instance_of(Array) + expect(@market.vendors).to be_an_instance_of(Array) end it 'returns all FarMar::Vendor instances for a market id' do - expect(FarMar::Market.vendors(1).length).to eq(6) + expect(@market.vendors.length).to eq(2) end end diff --git a/spec/far_mar/vendor_spec.rb b/spec/far_mar/vendor_spec.rb index af1fad03..89b409e3 100644 --- a/spec/far_mar/vendor_spec.rb +++ b/spec/far_mar/vendor_spec.rb @@ -68,11 +68,13 @@ end end - describe '.by_market' do - + describe '.by_market(market_id)' do + it 'returns all Vendors for given market_id' do + + end end - describe '.revenue(vendor_id)' do + describe '#revenue' do end From 970d72f7c81161fcf99b192ba8761f0f75feffab Mon Sep 17 00:00:00 2001 From: Riley Spicer Date: Wed, 21 Oct 2015 14:22:03 -0700 Subject: [PATCH 26/53] Vendor.by_market specs written and failing --- spec/far_mar/vendor_spec.rb | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/spec/far_mar/vendor_spec.rb b/spec/far_mar/vendor_spec.rb index 89b409e3..b4a617b6 100644 --- a/spec/far_mar/vendor_spec.rb +++ b/spec/far_mar/vendor_spec.rb @@ -68,14 +68,17 @@ end end + describe '#revenue' do + + end + describe '.by_market(market_id)' do it 'returns all Vendors for given market_id' do - + expect(FarMar::Vendor.by_market(1).length).to eq(6) + end + it 'returns only instances of Vendors' do + expect(FarMar::Vendor.by_market(1)[rand(0..5)]).to be_an_instance_of(FarMar::Vendor) end - end - - describe '#revenue' do - end end \ No newline at end of file From d4082ed9d376e353aaefc7cf0b242ff368d8b3c5 Mon Sep 17 00:00:00 2001 From: Riley Spicer Date: Wed, 21 Oct 2015 14:33:05 -0700 Subject: [PATCH 27/53] revenue tests written adn failing --- lib/far_mar/vendor.rb | 8 ++++++++ spec/far_mar/vendor_spec.rb | 7 ++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/far_mar/vendor.rb b/lib/far_mar/vendor.rb index db12d02c..92ce29a1 100644 --- a/lib/far_mar/vendor.rb +++ b/lib/far_mar/vendor.rb @@ -35,6 +35,14 @@ def sales FarMar::Sale.all.find_all { |sale| sale.vendor_id == vendor_id } end + def revenue + + end + + def self.by_market(market_id) + FarMar::Vendor.all.find_all {|ven| ven.market == market_id } + end + end end \ No newline at end of file diff --git a/spec/far_mar/vendor_spec.rb b/spec/far_mar/vendor_spec.rb index b4a617b6..9a3b6ec0 100644 --- a/spec/far_mar/vendor_spec.rb +++ b/spec/far_mar/vendor_spec.rb @@ -69,7 +69,12 @@ end describe '#revenue' do - + it 'totals sales for Vendor' do + vendor = FarMar::Vendor.new(51,"Bernier Inc",1,12) + vendor1 = FarMar::Vendor.new(18,"Von-Hamill",10,5) + expect(vendor.revenue).to eq(0) + expect(vendor1.revenue).to eq(9749) + end end describe '.by_market(market_id)' do From eb2caead3400ea2d1b4236dcda3d69784314ad20 Mon Sep 17 00:00:00 2001 From: Riley Spicer Date: Wed, 21 Oct 2015 14:47:56 -0700 Subject: [PATCH 28/53] Revenue code passing tests --- lib/far_mar/vendor.rb | 9 +++++++-- spec/far_mar/vendor_spec.rb | 4 ++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/lib/far_mar/vendor.rb b/lib/far_mar/vendor.rb index 92ce29a1..bab958f2 100644 --- a/lib/far_mar/vendor.rb +++ b/lib/far_mar/vendor.rb @@ -24,7 +24,7 @@ def self.all end def self.find(vendor_id) - FarMar::Vendor.all.find {|ven| ven.vendor_id == vendor_id} + @@vendor_array.find {|ven| ven.vendor_id == vendor_id} end def products @@ -36,7 +36,12 @@ def sales end def revenue - + sales_array = self.sales + sales_array.map! do |sale| + sale.amount + end + total = sales_array.inject(0, :+) + return total end def self.by_market(market_id) diff --git a/spec/far_mar/vendor_spec.rb b/spec/far_mar/vendor_spec.rb index 9a3b6ec0..73d3ef96 100644 --- a/spec/far_mar/vendor_spec.rb +++ b/spec/far_mar/vendor_spec.rb @@ -70,9 +70,9 @@ describe '#revenue' do it 'totals sales for Vendor' do - vendor = FarMar::Vendor.new(51,"Bernier Inc",1,12) + #vendor = FarMar::Vendor.new(51,"Bernier Inc",1,12) vendor1 = FarMar::Vendor.new(18,"Von-Hamill",10,5) - expect(vendor.revenue).to eq(0) + #expect(vendor.revenue).to eq(0) expect(vendor1.revenue).to eq(9749) end end From 4015f3148375282ce34c30267e255ee686d1dc68 Mon Sep 17 00:00:00 2001 From: Riley Spicer Date: Wed, 21 Oct 2015 14:55:18 -0700 Subject: [PATCH 29/53] Product#vendor spec written and failing --- spec/far_mar/product_spec.rb | 11 ++++++++++- spec/far_mar/vendor_spec.rb | 4 ++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/spec/far_mar/product_spec.rb b/spec/far_mar/product_spec.rb index 3907589c..b4a30be3 100644 --- a/spec/far_mar/product_spec.rb +++ b/spec/far_mar/product_spec.rb @@ -34,10 +34,19 @@ end end - describe '.find' do + describe '.find' do it 'returns a matching product instance' do expect(FarMar::Product.find(10).name).to eq("Black Apples") end end + describe '#vendor' do + before :each do + @product = FarMar::Product.new(9,"Large Mushrooms",5) + end + it 'returns Vendor associated with Product' do + expect(@product.vendor.name).to eq("Reynolds, Schmitt and Klocko") + end + end + end \ No newline at end of file diff --git a/spec/far_mar/vendor_spec.rb b/spec/far_mar/vendor_spec.rb index 73d3ef96..9a3b6ec0 100644 --- a/spec/far_mar/vendor_spec.rb +++ b/spec/far_mar/vendor_spec.rb @@ -70,9 +70,9 @@ describe '#revenue' do it 'totals sales for Vendor' do - #vendor = FarMar::Vendor.new(51,"Bernier Inc",1,12) + vendor = FarMar::Vendor.new(51,"Bernier Inc",1,12) vendor1 = FarMar::Vendor.new(18,"Von-Hamill",10,5) - #expect(vendor.revenue).to eq(0) + expect(vendor.revenue).to eq(0) expect(vendor1.revenue).to eq(9749) end end From 12f1c6d63b5b903e0c871e6d99b6bd6c24a7e7e6 Mon Sep 17 00:00:00 2001 From: Riley Spicer Date: Wed, 21 Oct 2015 14:56:52 -0700 Subject: [PATCH 30/53] Product#Vendor passing test --- lib/far_mar/product.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/far_mar/product.rb b/lib/far_mar/product.rb index 1e5c57e9..121cc7eb 100644 --- a/lib/far_mar/product.rb +++ b/lib/far_mar/product.rb @@ -26,6 +26,10 @@ def self.find(id) FarMar::Product.all.find {|pro| pro.product_id == id} end + def vendor + FarMar::Vendor.all.find{|ven| ven.vendor_id == @vendor_id} + end + end end From 907a3e8ca352875f809547e837a306821c5f70b7 Mon Sep 17 00:00:00 2001 From: Riley Spicer Date: Wed, 21 Oct 2015 15:01:42 -0700 Subject: [PATCH 31/53] specs failing for Product#sales --- spec/far_mar/product_spec.rb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/spec/far_mar/product_spec.rb b/spec/far_mar/product_spec.rb index b4a30be3..7983e5ea 100644 --- a/spec/far_mar/product_spec.rb +++ b/spec/far_mar/product_spec.rb @@ -49,4 +49,18 @@ end end + describe '#sales' do + before :each do + @product = FarMar::Product.new(9,"Large Mushrooms",5) + end + it 'returns an array' do + expect(@product.sales).to be_an_instance_of(Array) + end + it 'returns all sales of product' do + expect(@product.sales.length).to eq(3) + end + it 'returns Sales instances' do + expect(@products.sales[rand(0..2)]).to be_an_instance_of(FarMar::Sale) + end + end end \ No newline at end of file From 78267a6e7342e32fcafaf4fad8b54fd93d435002 Mon Sep 17 00:00:00 2001 From: Riley Spicer Date: Wed, 21 Oct 2015 15:03:10 -0700 Subject: [PATCH 32/53] Product#sales passing tests --- lib/far_mar/product.rb | 4 ++++ spec/far_mar/product_spec.rb | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/far_mar/product.rb b/lib/far_mar/product.rb index 121cc7eb..6c89f9ec 100644 --- a/lib/far_mar/product.rb +++ b/lib/far_mar/product.rb @@ -30,6 +30,10 @@ def vendor FarMar::Vendor.all.find{|ven| ven.vendor_id == @vendor_id} end + def sales + FarMar::Sale.all.find_all {|sale| sale.product_id == @product_id} + end + end end diff --git a/spec/far_mar/product_spec.rb b/spec/far_mar/product_spec.rb index 7983e5ea..fa2f66fe 100644 --- a/spec/far_mar/product_spec.rb +++ b/spec/far_mar/product_spec.rb @@ -60,7 +60,7 @@ expect(@product.sales.length).to eq(3) end it 'returns Sales instances' do - expect(@products.sales[rand(0..2)]).to be_an_instance_of(FarMar::Sale) + expect(@product.sales[rand(0..2)]).to be_an_instance_of(FarMar::Sale) end end end \ No newline at end of file From 8917c764b1861129c0734e85484a8b83093a0b9a Mon Sep 17 00:00:00 2001 From: Riley Spicer Date: Wed, 21 Oct 2015 15:21:57 -0700 Subject: [PATCH 33/53] Product#number_of_sales tests failing --- spec/far_mar/product_spec.rb | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/spec/far_mar/product_spec.rb b/spec/far_mar/product_spec.rb index fa2f66fe..4ec23607 100644 --- a/spec/far_mar/product_spec.rb +++ b/spec/far_mar/product_spec.rb @@ -59,8 +59,21 @@ it 'returns all sales of product' do expect(@product.sales.length).to eq(3) end + it 'works when zero sales have occured' do + product1 = FarMar::Product.new(165,"Striped Apples",51) + expect(product1.sales.length).to eq(0) + end it 'returns Sales instances' do expect(@product.sales[rand(0..2)]).to be_an_instance_of(FarMar::Sale) end end + + describe 'number_of_sales' do + @product = FarMar::Product.new(6,"Smooth Mushrooms",4) + @product1 = FarMar::Product.new(165,"Striped Apples",51) + it 'returns the number of times Product has been sold' do + expect(@product.number_of_sales).to eq(1) + expect(@product1.number_of_sales).to eq(0) + end + end end \ No newline at end of file From 653514d32866bd6bb46e74080bafda1d1c14d284 Mon Sep 17 00:00:00 2001 From: Riley Spicer Date: Wed, 21 Oct 2015 15:24:53 -0700 Subject: [PATCH 34/53] Product#number_of_sales passing tests, tests refactored --- lib/far_mar/product.rb | 4 ++++ spec/far_mar/product_spec.rb | 10 +++++++--- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/lib/far_mar/product.rb b/lib/far_mar/product.rb index 6c89f9ec..6e025977 100644 --- a/lib/far_mar/product.rb +++ b/lib/far_mar/product.rb @@ -34,6 +34,10 @@ def sales FarMar::Sale.all.find_all {|sale| sale.product_id == @product_id} 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 4ec23607..4ed58ecd 100644 --- a/spec/far_mar/product_spec.rb +++ b/spec/far_mar/product_spec.rb @@ -69,11 +69,15 @@ end describe 'number_of_sales' do - @product = FarMar::Product.new(6,"Smooth Mushrooms",4) - @product1 = FarMar::Product.new(165,"Striped Apples",51) + before :each do + @product = FarMar::Product.new(6,"Smooth Mushrooms",4) + end it 'returns the number of times Product has been sold' do expect(@product.number_of_sales).to eq(1) - expect(@product1.number_of_sales).to eq(0) + end + it 'works if product has not been sold' do + @product1 = FarMar::Product.new(165,"Striped Apples",51) + expect(@product1.number_of_sales).to eq(0) end end end \ No newline at end of file From 7e00bab0f549ef08dd87b529b8f8958f4db7e358 Mon Sep 17 00:00:00 2001 From: Riley Spicer Date: Wed, 21 Oct 2015 15:28:31 -0700 Subject: [PATCH 35/53] Product.by_vendor(vendor_id) failing tests --- spec/far_mar/product_spec.rb | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/spec/far_mar/product_spec.rb b/spec/far_mar/product_spec.rb index 4ed58ecd..d6ce7c22 100644 --- a/spec/far_mar/product_spec.rb +++ b/spec/far_mar/product_spec.rb @@ -79,5 +79,11 @@ @product1 = FarMar::Product.new(165,"Striped Apples",51) expect(@product1.number_of_sales).to eq(0) end + + describe '.by_vendor(vendor_id)'do + it 'returns products sold with same vendor_id' do + expect(FarMar::Product.all.by_vendor(6).length).to eq(6) + end + end end end \ No newline at end of file From 512ab53b828075b9c24bc810eafdba3d7e29b2b6 Mon Sep 17 00:00:00 2001 From: Riley Spicer Date: Wed, 21 Oct 2015 15:31:43 -0700 Subject: [PATCH 36/53] Product.by_vendor tests debugged, code written, tests passing --- lib/far_mar/product.rb | 6 +++++- spec/far_mar/product_spec.rb | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/far_mar/product.rb b/lib/far_mar/product.rb index 6e025977..a70dcb6b 100644 --- a/lib/far_mar/product.rb +++ b/lib/far_mar/product.rb @@ -35,7 +35,11 @@ def sales end def number_of_sales - return self.sales.length + self.sales.length + end + + def self.by_vendor(vendor_id) + @@product_array.find_all{|prod| prod.vendor_id == vendor_id} end end diff --git a/spec/far_mar/product_spec.rb b/spec/far_mar/product_spec.rb index d6ce7c22..e88e272b 100644 --- a/spec/far_mar/product_spec.rb +++ b/spec/far_mar/product_spec.rb @@ -82,7 +82,7 @@ describe '.by_vendor(vendor_id)'do it 'returns products sold with same vendor_id' do - expect(FarMar::Product.all.by_vendor(6).length).to eq(6) + expect(FarMar::Product.by_vendor(6).length).to eq(3) end end end From fa61bda0de635e3fc8d36f8a289c155edc1d759c Mon Sep 17 00:00:00 2001 From: Riley Spicer Date: Wed, 21 Oct 2015 15:38:17 -0700 Subject: [PATCH 37/53] Sale#vendor spec written and failing --- spec/far_mar/sale_spec.rb | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/spec/far_mar/sale_spec.rb b/spec/far_mar/sale_spec.rb index 4f1cb9d9..ef330349 100644 --- a/spec/far_mar/sale_spec.rb +++ b/spec/far_mar/sale_spec.rb @@ -30,4 +30,11 @@ end end + describe '#vendor' do + it 'returns Vendor that is associated with Sale' do + sale = FarMar::Sale.new(14,4978,"2013-11-10 01:51:24 -0800",3,4) + expect(sale.vendor.name).to eq("Kris and Sons") + end + end + end \ No newline at end of file From fbecfc257d7c838381ba524cb6a76d2f2e98b8ca Mon Sep 17 00:00:00 2001 From: Riley Spicer Date: Wed, 21 Oct 2015 15:40:08 -0700 Subject: [PATCH 38/53] Sale#vendor code passing --- lib/far_mar/sale.rb | 4 ++++ spec/far_mar/sale_spec.rb | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/far_mar/sale.rb b/lib/far_mar/sale.rb index 63a02619..31b19f88 100644 --- a/lib/far_mar/sale.rb +++ b/lib/far_mar/sale.rb @@ -29,6 +29,10 @@ def self.find(id) FarMar::Sale.all.find {|sale| sale.sale_id == id} end + def vendor + FarMar::Vendor.all.find {|ven| ven.vendor_id == vendor_id} + end + end end \ No newline at end of file diff --git a/spec/far_mar/sale_spec.rb b/spec/far_mar/sale_spec.rb index ef330349..8945da9d 100644 --- a/spec/far_mar/sale_spec.rb +++ b/spec/far_mar/sale_spec.rb @@ -33,7 +33,7 @@ describe '#vendor' do it 'returns Vendor that is associated with Sale' do sale = FarMar::Sale.new(14,4978,"2013-11-10 01:51:24 -0800",3,4) - expect(sale.vendor.name).to eq("Kris and Sons") + expect(sale.vendor.name).to eq("Breitenberg Inc") end end From e1915931ecdf144c42c92f2e9d03e01bb9d25374 Mon Sep 17 00:00:00 2001 From: Riley Spicer Date: Wed, 21 Oct 2015 15:52:28 -0700 Subject: [PATCH 39/53] Sale#product passing tests --- lib/far_mar/sale.rb | 6 +++++- spec/far_mar/product_spec.rb | 6 ++---- spec/far_mar/sale_spec.rb | 8 ++++++++ 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/lib/far_mar/sale.rb b/lib/far_mar/sale.rb index 31b19f88..1e283f13 100644 --- a/lib/far_mar/sale.rb +++ b/lib/far_mar/sale.rb @@ -30,7 +30,11 @@ def self.find(id) end def vendor - FarMar::Vendor.all.find {|ven| ven.vendor_id == vendor_id} + FarMar::Vendor.all.find {|ven| ven.vendor_id == @vendor_id} + end + + def product + FarMar::Product.all.find {|pro| pro.product_id == @product_id} end end diff --git a/spec/far_mar/product_spec.rb b/spec/far_mar/product_spec.rb index e88e272b..49ec9526 100644 --- a/spec/far_mar/product_spec.rb +++ b/spec/far_mar/product_spec.rb @@ -57,11 +57,9 @@ expect(@product.sales).to be_an_instance_of(Array) end it 'returns all sales of product' do - expect(@product.sales.length).to eq(3) - end - it 'works when zero sales have occured' do product1 = FarMar::Product.new(165,"Striped Apples",51) - expect(product1.sales.length).to eq(0) + expect(product1.sales.length).to eq(0) + expect(@product.sales.length).to eq(3) end it 'returns Sales instances' do expect(@product.sales[rand(0..2)]).to be_an_instance_of(FarMar::Sale) diff --git a/spec/far_mar/sale_spec.rb b/spec/far_mar/sale_spec.rb index 8945da9d..53a1db30 100644 --- a/spec/far_mar/sale_spec.rb +++ b/spec/far_mar/sale_spec.rb @@ -21,6 +21,7 @@ it 'contains all sales from CSV' do csv = CSV.read("support/sales.csv") expect(@sale_array.length).to eq csv.length + end end @@ -37,4 +38,11 @@ end end + describe 'product' do + it 'returns Product associated with the Sale' do + sale = FarMar::Sale.new(14,4978,"2013-11-10 01:51:24 -0800",3,4) + expect(sale.product.name).to eq("Yummy Fruit") + end + end + end \ No newline at end of file From 3d1293a89d61da0bef3f9aff2d7166feeaabdfd2 Mon Sep 17 00:00:00 2001 From: Riley Spicer Date: Wed, 21 Oct 2015 16:21:09 -0700 Subject: [PATCH 40/53] Primary Requirements Complete, though code could probably use some refactoring and further testing. (At 100% but could test more side cases..) --- lib/far_mar/sale.rb | 6 ++++++ spec/far_mar/sale_spec.rb | 11 +++++++++++ 2 files changed, 17 insertions(+) diff --git a/lib/far_mar/sale.rb b/lib/far_mar/sale.rb index 1e283f13..30a30a12 100644 --- a/lib/far_mar/sale.rb +++ b/lib/far_mar/sale.rb @@ -37,6 +37,12 @@ def product FarMar::Product.all.find {|pro| pro.product_id == @product_id} end + def self.between(beginning_time, ending_time) + @@sales_array.find_all do |sale| + sale.purchase_time.between?(beginning_time, ending_time) + end + end + end end \ No newline at end of file diff --git a/spec/far_mar/sale_spec.rb b/spec/far_mar/sale_spec.rb index 53a1db30..9b890bf6 100644 --- a/spec/far_mar/sale_spec.rb +++ b/spec/far_mar/sale_spec.rb @@ -45,4 +45,15 @@ end end + describe 'self.between(beginning_time, end_time)' do + before :each do + @beg_time1 = DateTime.strptime("2013-11-06 08:35:40 -08:00", "%Y-%m-%d %H:%M:%S %z") + @end_time1 = DateTime.strptime("2013-11-13 08:35:16 -0800", "%Y-%m-%d %H:%M:%S %z") + end + it 'returns Sales between two purchase times' do + sales = FarMar::Sale.between(@beg_time1, @end_time1) + expect(sales.length).to eq(12798) + end + end + end \ No newline at end of file From 7944a732374d046008d12be7a87381730d186ef6 Mon Sep 17 00:00:00 2001 From: Riley Spicer Date: Thu, 22 Oct 2015 10:31:25 -0700 Subject: [PATCH 41/53] Market#products specs failing --- spec/far_mar/market_spec.rb | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/spec/far_mar/market_spec.rb b/spec/far_mar/market_spec.rb index f7026b12..8031151c 100644 --- a/spec/far_mar/market_spec.rb +++ b/spec/far_mar/market_spec.rb @@ -53,4 +53,19 @@ end end + describe '#products' do + before :each do + @market = FarmMar::Market.new(2,"Silverdale Farmers Market",98383,Silverdale,Kitsap,Washington,98383) + end + it 'returns FarMar::Product instances' do + expect(@market.products[0]).to be_an_instance_of(FarMar::Product) + end + it 'returns an array' do + expect(@market.products).to be_an_instance_of(Array) + end + it 'returns all products sold at the Market' do + expect(@market.products.length).to eq(9) + end + end + end \ No newline at end of file From 370283fc6c88745f52d8a9590dffc6bf1bfa164f Mon Sep 17 00:00:00 2001 From: Riley Spicer Date: Thu, 22 Oct 2015 10:53:09 -0700 Subject: [PATCH 42/53] Market#products code passing tests --- lib/far_mar/market.rb | 9 +++++++++ spec/far_mar/market_spec.rb | 2 +- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/far_mar/market.rb b/lib/far_mar/market.rb index f6e1aa57..e15bb9e7 100644 --- a/lib/far_mar/market.rb +++ b/lib/far_mar/market.rb @@ -34,6 +34,15 @@ def vendors FarMar::Vendor.all.find_all {|ven| ven.market == market_id } end + def products + ven_array = self.vendors + prod_array = [] + ven_array.each do |ven| + prod_array.push(ven.products) + end + return prod_array.flatten + end + end diff --git a/spec/far_mar/market_spec.rb b/spec/far_mar/market_spec.rb index 8031151c..95867d84 100644 --- a/spec/far_mar/market_spec.rb +++ b/spec/far_mar/market_spec.rb @@ -55,7 +55,7 @@ describe '#products' do before :each do - @market = FarmMar::Market.new(2,"Silverdale Farmers Market",98383,Silverdale,Kitsap,Washington,98383) + @market = FarMar::Market.new(2,"Silverdale Farmers Market",98383,"Silverdale","Kitsap","Washington",98383) end it 'returns FarMar::Product instances' do expect(@market.products[0]).to be_an_instance_of(FarMar::Product) From ddd7c5173f2ff4c2716ea6d08f8638b978fcacca Mon Sep 17 00:00:00 2001 From: Riley Spicer Date: Thu, 22 Oct 2015 11:50:45 -0700 Subject: [PATCH 43/53] Tests for Market.search(search_term) written and failing --- spec/far_mar/market_spec.rb | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/spec/far_mar/market_spec.rb b/spec/far_mar/market_spec.rb index 95867d84..e5891d51 100644 --- a/spec/far_mar/market_spec.rb +++ b/spec/far_mar/market_spec.rb @@ -68,4 +68,19 @@ end end + describe '.search(search_term)' do + before :each do + @search_return = FarMar::Market.search("school") + end + it 'returns an array' do + expect(@search_return).to be_an_instance_of(Array) + end + it 'contains Market Instances' do + expect(@search_return[0]).to be_an_instance_of(FarMar::Market) + end + it 'returns Markets w search_term in @name || @vendor_name' do + expect(@search_return.length).to eq(3) + end + end + end \ No newline at end of file From d38ff08b60a276aec637a1829dee36f4ae061b60 Mon Sep 17 00:00:00 2001 From: Riley Spicer Date: Thu, 22 Oct 2015 14:02:39 -0700 Subject: [PATCH 44/53] 100% of code covered -- had to search for a term that was in a vendor in Market.search(search_term) --- lib/far_mar/market.rb | 23 ++++++++++++++++++++++- lib/far_mar/product.rb | 2 +- lib/far_mar/sale.rb | 2 +- spec/far_mar/market_spec.rb | 20 +++++++++++++++++++- spec/spec_helper.rb | 3 ++- 5 files changed, 45 insertions(+), 5 deletions(-) diff --git a/lib/far_mar/market.rb b/lib/far_mar/market.rb index e15bb9e7..d7d2d1e0 100644 --- a/lib/far_mar/market.rb +++ b/lib/far_mar/market.rb @@ -27,7 +27,7 @@ def self.all end def self.find(id) - FarMar::Market.all.find {|mar| mar.market_id == id } + @@market_array.find {|mar| mar.market_id == id } end def vendors @@ -43,6 +43,27 @@ def products return prod_array.flatten end + def self.search(search_term) + search_term = search_term.downcase + match_markets = [] + @@market_array.each do |market| + match_markets.push(market) if market.name.downcase.match(/#{search_term}/) + end + match_vendors = [] + FarMar::Vendor.all.each do |vendor| + match_vendors.push(vendor) if vendor.name.downcase.match(/#{search_term}/) + end + match_vendors.map! {|vendor| vendor.market} + match_vendors.each do |market_id| + market = FarMar::Market.find(market_id) + match_markets.push(market) + end + return match_markets + end + + def preferred_vendor + ven = self.vendors + end end diff --git a/lib/far_mar/product.rb b/lib/far_mar/product.rb index a70dcb6b..21232a01 100644 --- a/lib/far_mar/product.rb +++ b/lib/far_mar/product.rb @@ -23,7 +23,7 @@ def self.all end def self.find(id) - FarMar::Product.all.find {|pro| pro.product_id == id} + @@product_array.find {|pro| pro.product_id == id} end def vendor diff --git a/lib/far_mar/sale.rb b/lib/far_mar/sale.rb index 30a30a12..e4d3a280 100644 --- a/lib/far_mar/sale.rb +++ b/lib/far_mar/sale.rb @@ -26,7 +26,7 @@ def self.all end def self.find(id) - FarMar::Sale.all.find {|sale| sale.sale_id == id} + @@sales_array.find {|sale| sale.sale_id == id} end def vendor diff --git a/spec/far_mar/market_spec.rb b/spec/far_mar/market_spec.rb index e5891d51..578c93f0 100644 --- a/spec/far_mar/market_spec.rb +++ b/spec/far_mar/market_spec.rb @@ -78,9 +78,27 @@ it 'contains Market Instances' do expect(@search_return[0]).to be_an_instance_of(FarMar::Market) end - it 'returns Markets w search_term in @name || @vendor_name' do + it 'returns Markets w search_term in @name' do expect(@search_return.length).to eq(3) end + it 'returns Markets w search term in Vendor @name' do + @search = FarMar::Market.search("Donnelly") + expect(@search.length).to eq(18) + expect(@search[0].market_id).to eq(3) + end + + end + + describe '#preferred_vendor' do + before :each do + @market = FarMar::Market.new(2,"Silverdale Farmers Market",98383,"Silverdale","Kitsap","Washington",98383) + end + it 'returns a Vendor' do + expect(@market.preferred_vendor).to be_an_instance_of(FarMar::Vendor) + end + it 'returns Vendor with highest revenue' do + expect(@market.preferred_vendor.name).to eq("Stamm Inc") + end end end \ No newline at end of file diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 07cee4c8..8c932869 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,4 +1,5 @@ require 'simplecov' SimpleCov.start -require './lib/far_mar' \ No newline at end of file +require './lib/far_mar' + From 51004d6e0bd641840895a619ad7ce754591213c7 Mon Sep 17 00:00:00 2001 From: Riley Spicer Date: Thu, 22 Oct 2015 14:11:13 -0700 Subject: [PATCH 45/53] Market#preferred_vendor code written and passing --- lib/far_mar/market.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/far_mar/market.rb b/lib/far_mar/market.rb index d7d2d1e0..f78cd898 100644 --- a/lib/far_mar/market.rb +++ b/lib/far_mar/market.rb @@ -63,6 +63,7 @@ def self.search(search_term) def preferred_vendor ven = self.vendors + preferred = ven.max_by{|ven| ven.revenue} end end From 32ac0e5d3bc3a5de25c73fc929fdf2f3aab6644f Mon Sep 17 00:00:00 2001 From: Riley Spicer Date: Fri, 23 Oct 2015 08:54:41 -0700 Subject: [PATCH 46/53] Specs written for Market#worst_vendor --- lib/far_mar/market.rb | 25 +++++++++++++++++++++++++ spec/far_mar/market_spec.rb | 28 ++++++++++++++++++++++++++-- 2 files changed, 51 insertions(+), 2 deletions(-) diff --git a/lib/far_mar/market.rb b/lib/far_mar/market.rb index f78cd898..c3e50dff 100644 --- a/lib/far_mar/market.rb +++ b/lib/far_mar/market.rb @@ -66,6 +66,31 @@ def preferred_vendor preferred = ven.max_by{|ven| ven.revenue} end + def preferred_vendor_by_date(date) + date = DateTime.strptime(date, "%Y-%m-%d").to_date + max_revenue = 0 + pref_vend = nil + total = 0 + self.vendors.each do |vendor_inst| + sales_array = [] + vendor_inst.sales.each do |sale_inst| + if sale_inst.purchase_time.to_date == date + sales_array.push(sale_inst.amount) + end + end + if sales_array.length > 0 + total = sales_array.inject(0, :+) + if total > max_revenue + binding.pry + max_revenue = total + pref_vend = vendor_inst + end + end + #binding.pry + end + return pref_vend + end + end end \ No newline at end of file diff --git a/spec/far_mar/market_spec.rb b/spec/far_mar/market_spec.rb index 578c93f0..3e793ac0 100644 --- a/spec/far_mar/market_spec.rb +++ b/spec/far_mar/market_spec.rb @@ -85,8 +85,7 @@ @search = FarMar::Market.search("Donnelly") expect(@search.length).to eq(18) expect(@search[0].market_id).to eq(3) - end - + end end describe '#preferred_vendor' do @@ -101,4 +100,29 @@ end end + describe 'preferred_vendor_by_date(date)' do + before :each do + @market = FarMar::Market.new(2,"Silverdale Farmers Market",98383,"Silverdale","Kitsap","Washington",98383) + end + it 'returns a Vendor' do + expect(@market.preferred_vendor_by_date("2013-11-10")).to be_an_instance_of(FarMar::Vendor) + end + it 'returns Vendor w highest revenue for date' do + @pref_vend = @market.preferred_vendor_by_date("2013-11-10") + expect(@pref_vend.vendor_id).to eq(7) + end + end + + describe 'worst_vendor' do + before :each do + @market = FarMar::Market.new(2,"Silverdale Farmers Market",98383,"Silverdale","Kitsap","Washington",98383) + end + it 'returns a Vendor' do + expect(@market.worst_vendor).to be_an_instance_of(FarMar::Vendor) + end + it 'returns the Vendor with lowest revenue' do + expect(@market.worst_vendor.name).to eq("Quigley, Breitenberg and Schuster") + end + end + end \ No newline at end of file From bba5f14e0cf983fea46a93c71a978052314b1271 Mon Sep 17 00:00:00 2001 From: Riley Spicer Date: Fri, 23 Oct 2015 08:58:03 -0700 Subject: [PATCH 47/53] Market#worst_vendor code passing tests --- lib/far_mar/market.rb | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/far_mar/market.rb b/lib/far_mar/market.rb index c3e50dff..2b8d15c0 100644 --- a/lib/far_mar/market.rb +++ b/lib/far_mar/market.rb @@ -81,16 +81,19 @@ def preferred_vendor_by_date(date) if sales_array.length > 0 total = sales_array.inject(0, :+) if total > max_revenue - binding.pry max_revenue = total pref_vend = vendor_inst end end - #binding.pry end return pref_vend end + def worst_vendor + ven = self.vendors + worst = ven.min_by{|ven| ven.revenue} + end + end end \ No newline at end of file From 464b399c8a2932476b3d44cc1b8159d0bd534432 Mon Sep 17 00:00:00 2001 From: Riley Spicer Date: Fri, 23 Oct 2015 09:12:27 -0700 Subject: [PATCH 48/53] Specs written for Market#worst_vendor_by_date(date) --- spec/far_mar/market_spec.rb | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/spec/far_mar/market_spec.rb b/spec/far_mar/market_spec.rb index 3e793ac0..4796aae4 100644 --- a/spec/far_mar/market_spec.rb +++ b/spec/far_mar/market_spec.rb @@ -125,4 +125,17 @@ end end + describe '#worst_vendor_by_date(date)' do + before :each do + @market = FarMar::Market.new(2,"Silverdale Farmers Market",98383,"Silverdale","Kitsap","Washington",98383) + end + it 'returns a Vendor' do + expect(@market.worst_vendor_by_date("2013-11-10")).to be_an_instance_of(FarMar::Vendor) + end + it 'returns Vendor w highest revenue for date' do + @worst_vend = @market.worst_vendor_by_date("2013-11-10") + expect(@worst_vend.vendor_id).to eq(8) + end + end + end \ No newline at end of file From a2e56f40d603898366357d139d6a038d938e9350 Mon Sep 17 00:00:00 2001 From: Riley Spicer Date: Fri, 23 Oct 2015 09:12:54 -0700 Subject: [PATCH 49/53] Code written and passing for Market#worst_vendor_by_date(date) --- lib/far_mar/market.rb | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/lib/far_mar/market.rb b/lib/far_mar/market.rb index 2b8d15c0..ba46de04 100644 --- a/lib/far_mar/market.rb +++ b/lib/far_mar/market.rb @@ -94,6 +94,29 @@ def worst_vendor worst = ven.min_by{|ven| ven.revenue} end + def worst_vendor_by_date(date) + date = DateTime.strptime(date, "%Y-%m-%d").to_date + min_revenue = Float::INFINITY + worst_ven = nil + total = 0 + self.vendors.each do |vendor_inst| + sales_array = [] + vendor_inst.sales.each do |sale_inst| + if sale_inst.purchase_time.to_date == date + sales_array.push(sale_inst.amount) + end + end + if sales_array.length > 0 + total = sales_array.inject(0, :+) + if total < min_revenue + min_revenue = total + worst_ven = vendor_inst + end + end + end + return worst_ven + end + end end \ No newline at end of file From 4067e4006c59131075b43a39c51e04663775652f Mon Sep 17 00:00:00 2001 From: Riley Spicer Date: Fri, 23 Oct 2015 10:22:29 -0700 Subject: [PATCH 50/53] Vendor.most_items code passing, Vendor.revenue(date) tests written and failing --- lib/far_mar/vendor.rb | 22 ++++++++++++++++++++-- spec/far_mar/vendor_spec.rb | 23 +++++++++++++++++++++++ spec/spec_helper.rb | 4 ++++ 3 files changed, 47 insertions(+), 2 deletions(-) diff --git a/lib/far_mar/vendor.rb b/lib/far_mar/vendor.rb index bab958f2..13f4ea31 100644 --- a/lib/far_mar/vendor.rb +++ b/lib/far_mar/vendor.rb @@ -28,11 +28,11 @@ def self.find(vendor_id) end def products - FarMar::Product.all.find_all { |pro| pro.vendor_id == @vendor_id} + FarMar::Product.all.find_all {|pro| pro.vendor_id == @vendor_id} end def sales - FarMar::Sale.all.find_all { |sale| sale.vendor_id == vendor_id } + FarMar::Sale.all.find_all {|sale| sale.vendor_id == vendor_id } end def revenue @@ -48,6 +48,24 @@ def self.by_market(market_id) FarMar::Vendor.all.find_all {|ven| ven.market == market_id } end + # Couldn't figure out how to rewrite this to get it to run in a decent amount of time + # def self.most_revenue(n) + # top_vendors = @@vendor_array.sort_by{|ven| ven.revenue} + # return top_vendors[0..n] + # end + + def self.most_items(n) + most_items = 0 + vendors_by_items = [] + vendor_array = @@vendor_array.dup + n.times do + vendor = vendor_array.max_by{|ven| ven.products.length} + vendors_by_items.push(vendor) + vendor_array.delete(vendor) + end + return vendors_by_items + end + end end \ No newline at end of file diff --git a/spec/far_mar/vendor_spec.rb b/spec/far_mar/vendor_spec.rb index 9a3b6ec0..fab1ac13 100644 --- a/spec/far_mar/vendor_spec.rb +++ b/spec/far_mar/vendor_spec.rb @@ -86,4 +86,27 @@ end end + # Couldn't figure out how to get this to run without taking so long I have to just cancel it.. + # describe '.most_revenue(n)' do + # it 'returns top n Vendors by revenue' do + # top_vends = FarMar::Vendor.most_revenue(3) + # expect(top_vends[1].vendor_id).to eq() + # end + # end + + describe '.most_items(n)' do + it 'returns an Array' do + expect(FarMar::Vendor.most_items(3)).to be_an_instance_of(Array) + end + it 'returns top n Vendors by most items available for sale' do + expect(FarMar::Vendor.most_items(3)[0].vendor_id).to eq(10) + end + end + + describe 'self.revenue(date)' do + it 'returns total revenue for a given date' do + expect(FarMar::Vendor.revenute("2013-11-07")).to eq(9060582) + end + end + end \ No newline at end of file diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 8c932869..2467a460 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -3,3 +3,7 @@ require './lib/far_mar' +RSpec.configure do |config| + config.order = 'random' +end + From 3e15f2a2964b15697fd0120eb999e4adc6f14e31 Mon Sep 17 00:00:00 2001 From: Riley Spicer Date: Fri, 23 Oct 2015 14:13:32 -0700 Subject: [PATCH 51/53] Changed the Market#preferred_vendor method to include both with and without date --- lib/far_mar/market.rb | 42 ++++++++++++++++++------------------- lib/far_mar/product.rb | 2 +- lib/far_mar/vendor.rb | 13 ++++++++++++ spec/far_mar/market_spec.rb | 15 +++---------- spec/far_mar/vendor_spec.rb | 10 ++++----- 5 files changed, 43 insertions(+), 39 deletions(-) diff --git a/lib/far_mar/market.rb b/lib/far_mar/market.rb index ba46de04..d81acb44 100644 --- a/lib/far_mar/market.rb +++ b/lib/far_mar/market.rb @@ -61,32 +61,32 @@ def self.search(search_term) return match_markets end - def preferred_vendor - ven = self.vendors - preferred = ven.max_by{|ven| ven.revenue} - end - - def preferred_vendor_by_date(date) - date = DateTime.strptime(date, "%Y-%m-%d").to_date - max_revenue = 0 - pref_vend = nil - total = 0 - self.vendors.each do |vendor_inst| - sales_array = [] - vendor_inst.sales.each do |sale_inst| - if sale_inst.purchase_time.to_date == date + def preferred_vendor(date = nil) + if date == nil + ven = self.vendors + preferred = ven.max_by{|ven| ven.revenue} + else + date = DateTime.strptime(date, "%Y-%m-%d").to_date + max_revenue = 0 + pref_vend = nil + total = 0 + self.vendors.each do |vendor_inst| + sales_array = [] + vendor_inst.sales.each do |sale_inst| + if sale_inst.purchase_time.to_date == date sales_array.push(sale_inst.amount) + end end - end - if sales_array.length > 0 - total = sales_array.inject(0, :+) - if total > max_revenue - max_revenue = total - pref_vend = vendor_inst + if sales_array.length > 0 + total = sales_array.inject(0, :+) + if total > max_revenue + max_revenue = total + pref_vend = vendor_inst + end end end + return pref_vend end - return pref_vend end def worst_vendor diff --git a/lib/far_mar/product.rb b/lib/far_mar/product.rb index 21232a01..67eaaf34 100644 --- a/lib/far_mar/product.rb +++ b/lib/far_mar/product.rb @@ -39,7 +39,7 @@ def number_of_sales end def self.by_vendor(vendor_id) - @@product_array.find_all{|prod| prod.vendor_id == vendor_id} + FarMar::Product.all.find_all{|prod| prod.vendor_id == vendor_id} end end diff --git a/lib/far_mar/vendor.rb b/lib/far_mar/vendor.rb index 13f4ea31..37d1b837 100644 --- a/lib/far_mar/vendor.rb +++ b/lib/far_mar/vendor.rb @@ -66,6 +66,19 @@ def self.most_items(n) return vendors_by_items end + # def self.revenue(date) + # date = DateTime.strptime(date, "%Y-%m-%d").to_date + # sales_array = [] + # FarMar::Vendor.all.each do |vendor_inst| + # vendor_inst.sales.each do |sale_inst| + # if sale_inst.purchase_time.to_date == date + # sales_array.push(sale_inst.amount) + # end + # end + # end + # revenue = sales_array.inject(0, :+) + # end + end end \ No newline at end of file diff --git a/spec/far_mar/market_spec.rb b/spec/far_mar/market_spec.rb index 4796aae4..30e1d310 100644 --- a/spec/far_mar/market_spec.rb +++ b/spec/far_mar/market_spec.rb @@ -88,7 +88,7 @@ end end - describe '#preferred_vendor' do + describe '#preferred_vendor(date)' do before :each do @market = FarMar::Market.new(2,"Silverdale Farmers Market",98383,"Silverdale","Kitsap","Washington",98383) end @@ -98,17 +98,8 @@ it 'returns Vendor with highest revenue' do expect(@market.preferred_vendor.name).to eq("Stamm Inc") end - end - - describe 'preferred_vendor_by_date(date)' do - before :each do - @market = FarMar::Market.new(2,"Silverdale Farmers Market",98383,"Silverdale","Kitsap","Washington",98383) - end - it 'returns a Vendor' do - expect(@market.preferred_vendor_by_date("2013-11-10")).to be_an_instance_of(FarMar::Vendor) - end - it 'returns Vendor w highest revenue for date' do - @pref_vend = @market.preferred_vendor_by_date("2013-11-10") + it 'returns Vendor w highest revenue for date' do + @pref_vend = @market.preferred_vendor("2013-11-10") expect(@pref_vend.vendor_id).to eq(7) end end diff --git a/spec/far_mar/vendor_spec.rb b/spec/far_mar/vendor_spec.rb index fab1ac13..054b9e84 100644 --- a/spec/far_mar/vendor_spec.rb +++ b/spec/far_mar/vendor_spec.rb @@ -103,10 +103,10 @@ end end - describe 'self.revenue(date)' do - it 'returns total revenue for a given date' do - expect(FarMar::Vendor.revenute("2013-11-07")).to eq(9060582) - end - end + # describe 'self.revenue(date)' do + # it 'returns total revenue for a given date' do + # expect(FarMar::Vendor.revenue("2013-11-07")).to eq(9060582) + # end + # end end \ No newline at end of file From be09d354a3edd537e24813754eab72d12d28506d Mon Sep 17 00:00:00 2001 From: Riley Spicer Date: Fri, 23 Oct 2015 14:16:28 -0700 Subject: [PATCH 52/53] Commented out the Vendor.revenue(date) method because it is taking so long I can't even run tests on it. May or may not have time to refactor before the project is due. --- lib/far_mar/vendor.rb | 1 + spec/far_mar/vendor_spec.rb | 1 + 2 files changed, 2 insertions(+) diff --git a/lib/far_mar/vendor.rb b/lib/far_mar/vendor.rb index 37d1b837..548eb300 100644 --- a/lib/far_mar/vendor.rb +++ b/lib/far_mar/vendor.rb @@ -66,6 +66,7 @@ def self.most_items(n) return vendors_by_items end + # This method is currently taking too long to run; I can't even get the tests to finish on it. Not sure if I'll have the time to retool it # def self.revenue(date) # date = DateTime.strptime(date, "%Y-%m-%d").to_date # sales_array = [] diff --git a/spec/far_mar/vendor_spec.rb b/spec/far_mar/vendor_spec.rb index 054b9e84..b5bf1eb9 100644 --- a/spec/far_mar/vendor_spec.rb +++ b/spec/far_mar/vendor_spec.rb @@ -103,6 +103,7 @@ end end + # Another method that is taking way too long to run :/ # describe 'self.revenue(date)' do # it 'returns total revenue for a given date' do # expect(FarMar::Vendor.revenue("2013-11-07")).to eq(9060582) From 52f137f25ab3820f42146412fbb7d5030ba4f64c Mon Sep 17 00:00:00 2001 From: Riley Spicer Date: Sat, 24 Oct 2015 22:30:06 -0700 Subject: [PATCH 53/53] Fixed the broken code yayyyyy --- lib/far_mar/market.rb | 51 ++++++++++++++++++------------------- lib/far_mar/product.rb | 5 ++-- lib/far_mar/sale.rb | 46 +++++++++++++++++++++++++-------- lib/far_mar/vendor.rb | 18 +++++++------ spec/far_mar/market_spec.rb | 15 +++-------- spec/far_mar/vendor_spec.rb | 6 +++++ spec/spec_helper.rb | 4 --- 7 files changed, 84 insertions(+), 61 deletions(-) diff --git a/lib/far_mar/market.rb b/lib/far_mar/market.rb index d81acb44..6757dad1 100644 --- a/lib/far_mar/market.rb +++ b/lib/far_mar/market.rb @@ -27,7 +27,7 @@ def self.all end def self.find(id) - @@market_array.find {|mar| mar.market_id == id } + FarMar::Market.all.find {|mar| mar.market_id == id } end def vendors @@ -46,7 +46,7 @@ def products def self.search(search_term) search_term = search_term.downcase match_markets = [] - @@market_array.each do |market| + FarMar::Market.all.each do |market| match_markets.push(market) if market.name.downcase.match(/#{search_term}/) end match_vendors = [] @@ -89,34 +89,33 @@ def preferred_vendor(date = nil) end end - def worst_vendor - ven = self.vendors - worst = ven.min_by{|ven| ven.revenue} - end - - def worst_vendor_by_date(date) - date = DateTime.strptime(date, "%Y-%m-%d").to_date - min_revenue = Float::INFINITY - worst_ven = nil - total = 0 - self.vendors.each do |vendor_inst| - sales_array = [] - vendor_inst.sales.each do |sale_inst| - if sale_inst.purchase_time.to_date == date - sales_array.push(sale_inst.amount) - end - end - if sales_array.length > 0 - total = sales_array.inject(0, :+) - if total < min_revenue - min_revenue = total - worst_ven = vendor_inst + def worst_vendor(date = nil) + if date == nil + ven = self.vendors + worst = ven.min_by{|ven| ven.revenue} + else + date = DateTime.strptime(date, "%Y-%m-%d").to_date + min_revenue = Float::INFINITY + worst_ven = nil + total = 0 + self.vendors.each do |vendor_inst| + sales_array = [] + vendor_inst.sales.each do |sale_inst| + if sale_inst.purchase_time.to_date == date + sales_array.push(sale_inst.amount) + end end + if sales_array.length > 0 + total = sales_array.inject(0, :+) + if total < min_revenue + min_revenue = total + worst_ven = vendor_inst + end + end end + return worst_ven end - return worst_ven end end - end \ No newline at end of file diff --git a/lib/far_mar/product.rb b/lib/far_mar/product.rb index 67eaaf34..7b1c2fc1 100644 --- a/lib/far_mar/product.rb +++ b/lib/far_mar/product.rb @@ -23,7 +23,7 @@ def self.all end def self.find(id) - @@product_array.find {|pro| pro.product_id == id} + FarMar::Product.all.find {|pro| pro.product_id == id} end def vendor @@ -31,7 +31,8 @@ def vendor end def sales - FarMar::Sale.all.find_all {|sale| sale.product_id == @product_id} + sales_hash = FarMar::Sale.sales_by_product + return sales_hash[@product_id] end def number_of_sales diff --git a/lib/far_mar/sale.rb b/lib/far_mar/sale.rb index e4d3a280..23fc3471 100644 --- a/lib/far_mar/sale.rb +++ b/lib/far_mar/sale.rb @@ -14,19 +14,45 @@ def initialize(sale_id, amount, purchase_time, vendor_id, product_id) @product_id = product_id.to_i end + # def self.all + # @@sales_array ||= [] + # if @@sales_array = [] + # CSV.read('./support/sales.csv').each do |row| + # sale = FarMar::Sale.new(row[0], row[1], row[2], row[3], row[4]) + # @@sales_array.push(sale) + # end + # end + # return @@sales_array + # end + + def self.sales_init + @@sales_by_id = Hash.new + @@sales_by_product = Hash.new {|hash, key| hash[key] = []} + @@sales_by_vendor = Hash.new {|hash, key| hash[key] = []} + CSV.read('./support/sales.csv').each do |row| + new_sale = FarMar::Sale.new(row[0], row[1], row[2], row[3], row[4]) + @@sales_by_id[new_sale.sale_id] = new_sale + @@sales_by_product[new_sale.product_id].push(new_sale) + @@sales_by_vendor[new_sale.vendor_id].push(new_sale) + end + @@sales_all = sales_by_product.values.flatten + end + + def self.sales_by_product + return @@sales_by_product + end + + def self.sales_by_vendor + return @@sales_by_vendor + end + def self.all - @@sales_array ||= [] - if @@sales_array = [] - CSV.read('./support/sales.csv').each do |row| - sale = FarMar::Sale.new(row[0], row[1], row[2], row[3], row[4]) - @@sales_array.push(sale) - end - end - return @@sales_array + return @@sales_all end def self.find(id) - @@sales_array.find {|sale| sale.sale_id == id} + #FarMar::Sale.all.find {|sale| sale.sale_id == id} + return @@sales_by_id[id] end def vendor @@ -38,7 +64,7 @@ def product end def self.between(beginning_time, ending_time) - @@sales_array.find_all do |sale| + FarMar::Sale.all.find_all do |sale| sale.purchase_time.between?(beginning_time, ending_time) end end diff --git a/lib/far_mar/vendor.rb b/lib/far_mar/vendor.rb index 548eb300..c5847a77 100644 --- a/lib/far_mar/vendor.rb +++ b/lib/far_mar/vendor.rb @@ -24,7 +24,7 @@ def self.all end def self.find(vendor_id) - @@vendor_array.find {|ven| ven.vendor_id == vendor_id} + FarMar::Vendor.all.find {|ven| ven.vendor_id == vendor_id} end def products @@ -32,15 +32,17 @@ def products end def sales - FarMar::Sale.all.find_all {|sale| sale.vendor_id == vendor_id } + sales_hash = FarMar::Sale.sales_by_vendor + return sales_hash[@vendor_id] end def revenue sales_array = self.sales - sales_array.map! do |sale| - sale.amount + total = 0 + sales_array.each do |sale| + # binding.pry + total += sale.amount end - total = sales_array.inject(0, :+) return total end @@ -69,15 +71,15 @@ def self.most_items(n) # This method is currently taking too long to run; I can't even get the tests to finish on it. Not sure if I'll have the time to retool it # def self.revenue(date) # date = DateTime.strptime(date, "%Y-%m-%d").to_date - # sales_array = [] + # revenue = 0 # FarMar::Vendor.all.each do |vendor_inst| # vendor_inst.sales.each do |sale_inst| # if sale_inst.purchase_time.to_date == date - # sales_array.push(sale_inst.amount) + # revenue += sale_inst.amount # end # end # end - # revenue = sales_array.inject(0, :+) + # return revenue # end end diff --git a/spec/far_mar/market_spec.rb b/spec/far_mar/market_spec.rb index 30e1d310..0c88610b 100644 --- a/spec/far_mar/market_spec.rb +++ b/spec/far_mar/market_spec.rb @@ -2,6 +2,8 @@ describe FarMar::Market do + FarMar::Sale.sales_init + describe '#initialize' do before :each do @market = FarMar::Market.new(1,"People's Co-op Farmers Market","30th and Burnside","Portland","Multnomah","Oregon",97202) @@ -114,17 +116,8 @@ it 'returns the Vendor with lowest revenue' do expect(@market.worst_vendor.name).to eq("Quigley, Breitenberg and Schuster") end - end - - describe '#worst_vendor_by_date(date)' do - before :each do - @market = FarMar::Market.new(2,"Silverdale Farmers Market",98383,"Silverdale","Kitsap","Washington",98383) - end - it 'returns a Vendor' do - expect(@market.worst_vendor_by_date("2013-11-10")).to be_an_instance_of(FarMar::Vendor) - end - it 'returns Vendor w highest revenue for date' do - @worst_vend = @market.worst_vendor_by_date("2013-11-10") + it 'returns Vendor w highest revenue for date' do + @worst_vend = @market.worst_vendor("2013-11-10") expect(@worst_vend.vendor_id).to eq(8) end end diff --git a/spec/far_mar/vendor_spec.rb b/spec/far_mar/vendor_spec.rb index b5bf1eb9..4b0222a3 100644 --- a/spec/far_mar/vendor_spec.rb +++ b/spec/far_mar/vendor_spec.rb @@ -1,6 +1,9 @@ require 'spec_helper' describe FarMar::Vendor do + + FarMar::Sale.sales_init + describe '#initialize' do before :each do @@ -63,6 +66,9 @@ before :each do @vendor = FarMar::Vendor.new(12,"Windler Inc",4,3) end + it 'returns an array' do + expect(@vendor.sales.class).to eq(Array) + end it 'returns all sales per vendor' do expect(@vendor.sales.length).to eq(3) end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 2467a460..8c932869 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -3,7 +3,3 @@ require './lib/far_mar' -RSpec.configure do |config| - config.order = 'random' -end -