From 81876241a55e064f6f5e308dc4ccfeeb79790fc3 Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Mon, 19 Oct 2015 15:40:04 -0700 Subject: [PATCH 001/107] create test for Market.all --- spec/far_mar/market_spec.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/spec/far_mar/market_spec.rb b/spec/far_mar/market_spec.rb index 97f0833b..52b16b80 100644 --- a/spec/far_mar/market_spec.rb +++ b/spec/far_mar/market_spec.rb @@ -7,5 +7,17 @@ expect(FarMar::Market.new()).to be_an_instance_of(FarMar::Market) end end + + describe ".all" do + before :each do + all_markets = FarMar::Market.all + end + it "returns an array of Market objects" do + expect(all_markets).to be_an(Array) + expect(all_markets[0]).to be_an_instance_of(FarMar::Market) + expect(all_markets[-1]).to be_an_instance_of(FarMar::Market) + expect(all_markets.length).to eq(500) + end + end end end From 96aca23e3f1e8e93c8f173db3e79f43255055b32 Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Mon, 19 Oct 2015 16:12:24 -0700 Subject: [PATCH 002/107] complete market.all method --- lib/far_mar/market.rb | 37 +++++++++++++++++++++++++++++++++++++ spec/far_mar/market_spec.rb | 20 ++++++++++++++++---- 2 files changed, 53 insertions(+), 4 deletions(-) diff --git a/lib/far_mar/market.rb b/lib/far_mar/market.rb index 2515aaf6..0fed7f21 100644 --- a/lib/far_mar/market.rb +++ b/lib/far_mar/market.rb @@ -1,5 +1,42 @@ module FarMar class Market + attr_reader :id, :name, :address, :city, :county, :state, :zip + def initialize(market_hash) + @id = market_hash[:id] + @name = market_hash[:name] + @address = market_hash[:address] + @city = market_hash[:city] + @county = market_hash[:county] + @state = market_hash[:state] + @zip = market_hash[:zip] + end + + # Converts an array to a hash for passing to owner instantiation + def self.convert_to_market_hash(market_array) + market_hash = {} + market_hash[:id] = market_array[0].to_i + market_hash[:name] = market_array[1] + market_hash[:address] = market_array[2] + market_hash[:city] = market_array[3] + market_hash[:county] = market_array[4] + market_hash[:state] = market_array[5] + return market_hash + end + + def self.all + csv_file = CSV.read("./support/markets.csv") + # Create empty array which will hold all the market objects + markets = [] + csv_file.each do |row| + # Convert the array to a hash + market_hash = self.convert_to_market_hash(row) + # Create a market object from each row-hash in the csv file + temp = FarMar::Market.new(market_hash) + # Push account object to array of accounts + markets.push(temp) + end + return markets + end end end diff --git a/spec/far_mar/market_spec.rb b/spec/far_mar/market_spec.rb index 52b16b80..2dee8d9c 100644 --- a/spec/far_mar/market_spec.rb +++ b/spec/far_mar/market_spec.rb @@ -4,18 +4,30 @@ describe FarMar::Market do describe "#initialize" do it "creates a new Market instance" do - expect(FarMar::Market.new()).to be_an_instance_of(FarMar::Market) + fake_hash = { + :id => 1, + :name => "Holiday", + :address => "118 N 180th St", + :city => "Shoreline", + :county => "King County", + :state => "WA", + :zip => "98133" + } + expect(FarMar::Market.new(fake_hash)).to be_an_instance_of(FarMar::Market) end end describe ".all" do - before :each do + # before :each do + # + # end + it "returns an array of 500 Market objects" do all_markets = FarMar::Market.all - end - it "returns an array of Market objects" do expect(all_markets).to be_an(Array) expect(all_markets[0]).to be_an_instance_of(FarMar::Market) expect(all_markets[-1]).to be_an_instance_of(FarMar::Market) + expect(all_markets[0].id).to eq(1) + expect(all_markets[-1].id).to eq(500) expect(all_markets.length).to eq(500) end end From 905ce1b5352e2f9c69ab32129d605f3c5c1dfc18 Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Mon, 19 Oct 2015 16:35:40 -0700 Subject: [PATCH 003/107] update market initialize test --- lib/far_mar/market.rb | 11 ++++------- spec/far_mar/market_spec.rb | 14 ++++++++++---- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/lib/far_mar/market.rb b/lib/far_mar/market.rb index 0fed7f21..154f1e16 100644 --- a/lib/far_mar/market.rb +++ b/lib/far_mar/market.rb @@ -3,13 +3,10 @@ class Market attr_reader :id, :name, :address, :city, :county, :state, :zip def initialize(market_hash) - @id = market_hash[:id] - @name = market_hash[:name] - @address = market_hash[:address] - @city = market_hash[:city] - @county = market_hash[:county] - @state = market_hash[:state] - @zip = market_hash[:zip] + # This block sets each key to an instance variable + market_hash.each do |k,v| + instance_variable_set("@#{k}", v) unless v.nil? + end end # Converts an array to a hash for passing to owner instantiation diff --git a/spec/far_mar/market_spec.rb b/spec/far_mar/market_spec.rb index 2dee8d9c..786f84a5 100644 --- a/spec/far_mar/market_spec.rb +++ b/spec/far_mar/market_spec.rb @@ -13,14 +13,20 @@ :state => "WA", :zip => "98133" } - expect(FarMar::Market.new(fake_hash)).to be_an_instance_of(FarMar::Market) + new_market = FarMar::Market.new(fake_hash) + expect(new_market).to be_an_instance_of(FarMar::Market) + expect(new_market.id).to eq(1) + expect(new_market.name).to eq("Holiday") + expect(new_market.zip).to eq("98133") + + # I tried to iterate over the original hash but that was a fail. + # fake_hash.each do |k, v| + # expect(new_market.k.to_s).to eq(v) + # end end end describe ".all" do - # before :each do - # - # end it "returns an array of 500 Market objects" do all_markets = FarMar::Market.all expect(all_markets).to be_an(Array) From 7501c43d1fc2d21d67f683bf8079c2b282e8ec42 Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Tue, 20 Oct 2015 08:45:15 -0700 Subject: [PATCH 004/107] create init tests for product class --- lib/far_mar/market.rb | 2 +- lib/far_mar/product.rb | 3 +++ spec/far_mar/product_spec.rb | 22 +++++++++++++++++++++- 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/lib/far_mar/market.rb b/lib/far_mar/market.rb index 154f1e16..c92143e8 100644 --- a/lib/far_mar/market.rb +++ b/lib/far_mar/market.rb @@ -9,7 +9,7 @@ def initialize(market_hash) end end - # Converts an array to a hash for passing to owner instantiation + # Converts an array to a hash for passing to market instantiation def self.convert_to_market_hash(market_array) market_hash = {} market_hash[:id] = market_array[0].to_i diff --git a/lib/far_mar/product.rb b/lib/far_mar/product.rb index 8efc48d2..ebcb94d3 100644 --- a/lib/far_mar/product.rb +++ b/lib/far_mar/product.rb @@ -1,5 +1,8 @@ module FarMar class Product + def self.convert_to_product_hash + + end end end diff --git a/spec/far_mar/product_spec.rb b/spec/far_mar/product_spec.rb index c11eb829..482171c4 100644 --- a/spec/far_mar/product_spec.rb +++ b/spec/far_mar/product_spec.rb @@ -4,7 +4,27 @@ describe FarMar::Product do describe "#initialize" do it "creates a new Product instance" do - expect(FarMar::Product.new()).to be_an_instance_of(FarMar::Product) + fake_hash = { + :id => 1, + :name => "Crispy Spiders", + :vendor_id => 5555 + } + product = FarMar::Product.new(fake_hash) + expect(product).to be_an_instance_of(FarMar::Product) + expect(product.id).to eq(1) + expect(product.name).to eq("Crispy Spiders") + expect(vendor_id).to eq(5555) + end + + describe ".all" do + it "returns an array of 8193 Product objects" do + all_products = FarMar::Product.all + expect(all_products).to be_an(Array) + expect(all_products.length).to eq(8193) + expect(all_products[0]).to be_an_instance_of(Product) + expect(all_products[-1]).to be_an_instance_of(Product) + expect(all_products[-1].name).to eq("Cruel Beef") + end end end end From 0b1d39f411e92d35408f99ebad37777d97845e41 Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Tue, 20 Oct 2015 08:58:34 -0700 Subject: [PATCH 005/107] create product initialize method --- lib/far_mar/market.rb | 2 +- lib/far_mar/product.rb | 12 +++++++++++- spec/far_mar/product_spec.rb | 2 +- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/lib/far_mar/market.rb b/lib/far_mar/market.rb index c92143e8..66953315 100644 --- a/lib/far_mar/market.rb +++ b/lib/far_mar/market.rb @@ -3,7 +3,7 @@ class Market attr_reader :id, :name, :address, :city, :county, :state, :zip def initialize(market_hash) - # This block sets each key to an instance variable + # This block sets each key to an instance variable and assigns the value market_hash.each do |k,v| instance_variable_set("@#{k}", v) unless v.nil? end diff --git a/lib/far_mar/product.rb b/lib/far_mar/product.rb index ebcb94d3..fd2cfee1 100644 --- a/lib/far_mar/product.rb +++ b/lib/far_mar/product.rb @@ -1,8 +1,18 @@ module FarMar class Product + attr_reader :id, :name, :vendor_id - def self.convert_to_product_hash + def initialize(product_hash) + product_hash.each do |k,v| + instance_variable_set("@#{k}", v) unless v.nil? + end + end + def self.convert_to_product_hash(product_array) + product_hash = {} + product_hash[:id] = product_array[0].to_i + product_hash[:name] = product_array[1] + product_hash[:vendor_id] = product_array[2].to_i end end end diff --git a/spec/far_mar/product_spec.rb b/spec/far_mar/product_spec.rb index 482171c4..0c9943b4 100644 --- a/spec/far_mar/product_spec.rb +++ b/spec/far_mar/product_spec.rb @@ -13,7 +13,7 @@ expect(product).to be_an_instance_of(FarMar::Product) expect(product.id).to eq(1) expect(product.name).to eq("Crispy Spiders") - expect(vendor_id).to eq(5555) + expect(product.vendor_id).to eq(5555) end describe ".all" do From 0257c5855f6b3118122ad2262604b5bb3f2e49fd Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Tue, 20 Oct 2015 09:26:32 -0700 Subject: [PATCH 006/107] create product all method --- lib/far_mar/market.rb | 6 +++--- lib/far_mar/product.rb | 19 ++++++++++++++++++- spec/far_mar/product_spec.rb | 19 +++++++++---------- 3 files changed, 30 insertions(+), 14 deletions(-) diff --git a/lib/far_mar/market.rb b/lib/far_mar/market.rb index 66953315..0f7fb9c5 100644 --- a/lib/far_mar/market.rb +++ b/lib/far_mar/market.rb @@ -10,7 +10,7 @@ def initialize(market_hash) end # Converts an array to a hash for passing to market instantiation - def self.convert_to_market_hash(market_array) + def self.convert_to_hash(market_array) market_hash = {} market_hash[:id] = market_array[0].to_i market_hash[:name] = market_array[1] @@ -27,10 +27,10 @@ def self.all markets = [] csv_file.each do |row| # Convert the array to a hash - market_hash = self.convert_to_market_hash(row) + market_hash = convert_to_hash(row) # Create a market object from each row-hash in the csv file temp = FarMar::Market.new(market_hash) - # Push account object to array of accounts + # Push market object to array of markets markets.push(temp) end return markets diff --git a/lib/far_mar/product.rb b/lib/far_mar/product.rb index fd2cfee1..91bf8fd8 100644 --- a/lib/far_mar/product.rb +++ b/lib/far_mar/product.rb @@ -8,11 +8,28 @@ def initialize(product_hash) end end - def self.convert_to_product_hash(product_array) + # Converts an array to a hash for passing to product instantiation + def self.convert_to_hash(product_array) product_hash = {} product_hash[:id] = product_array[0].to_i product_hash[:name] = product_array[1] product_hash[:vendor_id] = product_array[2].to_i + return product_hash + end + + def self.all + csv_file = CSV.read("./support/products.csv") + # Create empty array which will hold all the product objects + products = [] + csv_file.each do |row| + # Convert the array to a hash + product_hash = convert_to_hash(row) + # Create a product object from each row-hash in the csv file + temp = FarMar::Product.new(product_hash) + # Push product object to array of products + products.push(temp) + end + return products end end end diff --git a/spec/far_mar/product_spec.rb b/spec/far_mar/product_spec.rb index 0c9943b4..5aa40e14 100644 --- a/spec/far_mar/product_spec.rb +++ b/spec/far_mar/product_spec.rb @@ -15,16 +15,15 @@ expect(product.name).to eq("Crispy Spiders") expect(product.vendor_id).to eq(5555) end - - describe ".all" do - it "returns an array of 8193 Product objects" do - all_products = FarMar::Product.all - expect(all_products).to be_an(Array) - expect(all_products.length).to eq(8193) - expect(all_products[0]).to be_an_instance_of(Product) - expect(all_products[-1]).to be_an_instance_of(Product) - expect(all_products[-1].name).to eq("Cruel Beef") - end + end + describe ".all" do + it "returns an array of 8193 Product objects" do + all_products = FarMar::Product.all + expect(all_products).to be_an(Array) + expect(all_products.length).to eq(8193) + expect(all_products[0]).to be_an_instance_of(FarMar::Product) + expect(all_products[-1]).to be_an_instance_of(FarMar::Product) + expect(all_products[-1].name).to eq("Cruel Beef") end end end From 26d719fa7911486fbfaf364e1646716e6f931d1c Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Tue, 20 Oct 2015 09:43:05 -0700 Subject: [PATCH 007/107] refactor to include farmar base class --- lib/far_mar.rb | 1 + lib/far_mar/far_mar_base.rb | 10 ++++++++++ lib/far_mar/market.rb | 7 ++----- lib/far_mar/product.rb | 6 ++---- 4 files changed, 15 insertions(+), 9 deletions(-) create mode 100644 lib/far_mar/far_mar_base.rb diff --git a/lib/far_mar.rb b/lib/far_mar.rb index b5b6336f..8474a209 100644 --- a/lib/far_mar.rb +++ b/lib/far_mar.rb @@ -2,6 +2,7 @@ require 'time' # Order matters here so I might have to change it +require './lib/far_mar/far_mar_base' require './lib/far_mar/market' require './lib/far_mar/vendor' require './lib/far_mar/product' diff --git a/lib/far_mar/far_mar_base.rb b/lib/far_mar/far_mar_base.rb new file mode 100644 index 00000000..ae943aef --- /dev/null +++ b/lib/far_mar/far_mar_base.rb @@ -0,0 +1,10 @@ +module FarMar + class FarMar_Base + def initialize(init_hash) + # This block sets each key to an instance variable and assigns the value + init_hash.each do |k,v| + instance_variable_set("@#{k}", v) unless v.nil? + end + end + end +end diff --git a/lib/far_mar/market.rb b/lib/far_mar/market.rb index 0f7fb9c5..c0fd86e4 100644 --- a/lib/far_mar/market.rb +++ b/lib/far_mar/market.rb @@ -1,12 +1,9 @@ module FarMar - class Market + class Market < FarMar_Base attr_reader :id, :name, :address, :city, :county, :state, :zip def initialize(market_hash) - # This block sets each key to an instance variable and assigns the value - market_hash.each do |k,v| - instance_variable_set("@#{k}", v) unless v.nil? - end + super(market_hash) end # Converts an array to a hash for passing to market instantiation diff --git a/lib/far_mar/product.rb b/lib/far_mar/product.rb index 91bf8fd8..c7d280c6 100644 --- a/lib/far_mar/product.rb +++ b/lib/far_mar/product.rb @@ -1,11 +1,9 @@ module FarMar - class Product + class Product < FarMar_Base attr_reader :id, :name, :vendor_id def initialize(product_hash) - product_hash.each do |k,v| - instance_variable_set("@#{k}", v) unless v.nil? - end + super(product_hash) end # Converts an array to a hash for passing to product instantiation From 3b90a36fce48e417136acb00345467a89d58088c Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Tue, 20 Oct 2015 09:56:36 -0700 Subject: [PATCH 008/107] refactor init methods to use base class --- lib/far_mar.rb | 1 + lib/far_mar/far_mar_base.rb | 16 ++++++++++++++++ lib/far_mar/market.rb | 13 +------------ lib/far_mar/product.rb | 13 +------------ 4 files changed, 19 insertions(+), 24 deletions(-) diff --git a/lib/far_mar.rb b/lib/far_mar.rb index 8474a209..587f4257 100644 --- a/lib/far_mar.rb +++ b/lib/far_mar.rb @@ -1,5 +1,6 @@ require 'csv' require 'time' +require 'pry' # Order matters here so I might have to change it require './lib/far_mar/far_mar_base' diff --git a/lib/far_mar/far_mar_base.rb b/lib/far_mar/far_mar_base.rb index ae943aef..ba2c01a5 100644 --- a/lib/far_mar/far_mar_base.rb +++ b/lib/far_mar/far_mar_base.rb @@ -6,5 +6,21 @@ def initialize(init_hash) instance_variable_set("@#{k}", v) unless v.nil? end end + + def self.all(file) + csv_file = CSV.read(file) + # Create empty array which will hold all the objects + objects = [] + csv_file.each do |row| + # Convert the array to a hash + # binding.pry + csv_hash = self.convert_to_hash(row) + # Create a product object from each row-hash in the csv file + temp = self.new(csv_hash) + # Push product object to array of products + objects.push(temp) + end + return objects + end end end diff --git a/lib/far_mar/market.rb b/lib/far_mar/market.rb index c0fd86e4..3e82dfa2 100644 --- a/lib/far_mar/market.rb +++ b/lib/far_mar/market.rb @@ -19,18 +19,7 @@ def self.convert_to_hash(market_array) end def self.all - csv_file = CSV.read("./support/markets.csv") - # Create empty array which will hold all the market objects - markets = [] - csv_file.each do |row| - # Convert the array to a hash - market_hash = convert_to_hash(row) - # Create a market object from each row-hash in the csv file - temp = FarMar::Market.new(market_hash) - # Push market object to array of markets - markets.push(temp) - end - return markets + super('./support/markets.csv') end end end diff --git a/lib/far_mar/product.rb b/lib/far_mar/product.rb index c7d280c6..eb417173 100644 --- a/lib/far_mar/product.rb +++ b/lib/far_mar/product.rb @@ -16,18 +16,7 @@ def self.convert_to_hash(product_array) end def self.all - csv_file = CSV.read("./support/products.csv") - # Create empty array which will hold all the product objects - products = [] - csv_file.each do |row| - # Convert the array to a hash - product_hash = convert_to_hash(row) - # Create a product object from each row-hash in the csv file - temp = FarMar::Product.new(product_hash) - # Push product object to array of products - products.push(temp) - end - return products + super('./support/products.csv') end end end From 7d2ec0af1fe0ae7390ca22418a8cbf33258e73aa Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Tue, 20 Oct 2015 13:52:12 -0700 Subject: [PATCH 009/107] create sale tests --- spec/far_mar/sale_spec.rb | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/spec/far_mar/sale_spec.rb b/spec/far_mar/sale_spec.rb index 0168ee5f..5fb8b49d 100644 --- a/spec/far_mar/sale_spec.rb +++ b/spec/far_mar/sale_spec.rb @@ -4,7 +4,30 @@ describe FarMar::Sale do describe "#initialize" do it "creates a new Sale instance" do - expect(FarMar::Sale.new()).to be_an_instance_of(FarMar::Sale) + fake_hash = { + :id => 1, + :amount => 9290, + :purchase_time => "2013-11-12 04:36:56 -0800", + :vendor_id => 5, + :product_id => 12 + } + sale = FarMar::Sale.new(fake_hash) + expect(sale).to be_an_instance_of(FarMar::Sale) + expect(sale.id).to eq(1) + expect(sale.amount).to eq(9290) + expect(sale.purchase_time.class).to be(DateTime) + expect(sale.vendor_id).to eq(5) + expect(sale.product_id).to eq(12) + end + end + describe ".all" do + it "returns an array of 12798 Sale objects" do + all_sales = FarMar::Sale.all + expect(all_sales).to be_an(Array) + expect(all_sales[0].id).to eq(1) + expect(all_sales[0].product_id).to eq(1) + expect(all_sales[-1].id).to eq(12001) + expect(all_sales[-1].product_id).to eq(8192) end end end From c3c9aac7e84551a0997f089abd392ad88b7dec68 Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Tue, 20 Oct 2015 14:18:52 -0700 Subject: [PATCH 010/107] complete sale init and all methods --- lib/far_mar/sale.rb | 21 ++++++++++++++++++++- spec/far_mar/sale_spec.rb | 10 +++------- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/lib/far_mar/sale.rb b/lib/far_mar/sale.rb index d68f9ded..ababf278 100644 --- a/lib/far_mar/sale.rb +++ b/lib/far_mar/sale.rb @@ -1,5 +1,24 @@ module FarMar - class Sale + class Sale < FarMar_Base + attr_reader :id, :amount, :purchase_time, :vendor_id, :product_id + + def initialize(sale_hash) + super(sale_hash) + end + + def self.convert_to_hash(sale_array) + sale_hash = {} + sale_hash[:id] = sale_array[0].to_i + sale_hash[:amount] = sale_array[1].to_i + sale_hash[:purchase_time] = DateTime.parse(sale_array[2]) + sale_hash[:vendor_id] = sale_array[3].to_i + sale_hash[:product_id] = sale_array[4].to_i + return sale_hash + end + + def self.all + super('./support/sales.csv') + end end end diff --git a/spec/far_mar/sale_spec.rb b/spec/far_mar/sale_spec.rb index 5fb8b49d..00226e89 100644 --- a/spec/far_mar/sale_spec.rb +++ b/spec/far_mar/sale_spec.rb @@ -4,13 +4,8 @@ describe FarMar::Sale do describe "#initialize" do it "creates a new Sale instance" do - fake_hash = { - :id => 1, - :amount => 9290, - :purchase_time => "2013-11-12 04:36:56 -0800", - :vendor_id => 5, - :product_id => 12 - } + fake_array = ["1", "9290", "2013-11-12 04:36:56 -0800", "5", "12"] + fake_hash = FarMar::Sale.convert_to_hash(fake_array) sale = FarMar::Sale.new(fake_hash) expect(sale).to be_an_instance_of(FarMar::Sale) expect(sale.id).to eq(1) @@ -28,6 +23,7 @@ expect(all_sales[0].product_id).to eq(1) expect(all_sales[-1].id).to eq(12001) expect(all_sales[-1].product_id).to eq(8192) + expect(all_sales.length).to eq(12798) end end end From 26c773311e876ab309ddcd96a509bac1da5f8526 Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Tue, 20 Oct 2015 14:28:50 -0700 Subject: [PATCH 011/107] create vendor initialize test --- spec/far_mar/vendor_spec.rb | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/spec/far_mar/vendor_spec.rb b/spec/far_mar/vendor_spec.rb index 04290bd9..c49494c3 100644 --- a/spec/far_mar/vendor_spec.rb +++ b/spec/far_mar/vendor_spec.rb @@ -4,7 +4,14 @@ describe FarMar::Vendor do describe "#initialize" do it "creates a new Vendor instance" do - expect(FarMar::Vendor.new()).to be_an_instance_of(FarMar::Vendor) + v_array = %w(1 QFC 10 2) + v_hash = FarMar::Vendor.convert_to_hash(v_array) + vendor = FarMar::Vendor.new(v_hash) + expect(vendor).to be_an_instance_of(FarMar::Vendor) + expect(vendor.id).to eq(1) + expect(vendor.name).to eq("QFC") + expect(vendor.emp_num).to eq(10) + expect(vendor.market_id).to eq(2) end end end From c3e1f210dc7db39c70f4860904eb5cbdcedd1da3 Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Tue, 20 Oct 2015 14:32:09 -0700 Subject: [PATCH 012/107] create vendor initialize and convert_to_hash methods --- lib/far_mar/vendor.rb | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/far_mar/vendor.rb b/lib/far_mar/vendor.rb index bfe19c33..f72939ce 100644 --- a/lib/far_mar/vendor.rb +++ b/lib/far_mar/vendor.rb @@ -1,5 +1,18 @@ module FarMar - class Vendor + class Vendor < FarMar_Base + attr_reader :id, :name, :emp_num, :market_id + def initialize(vendor_hash) + super(vendor_hash) + end + + def self.convert_to_hash(array) + vendor = {} + vendor[:id] = array[0].to_i + vendor[:name] = array[1] + vendor[:emp_num] = array[2].to_i + vendor[:market_id] = array[3].to_i + return vendor + end end end From 811c56f2a83ba85a9a4ad27f011a21319c063860 Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Tue, 20 Oct 2015 14:35:51 -0700 Subject: [PATCH 013/107] create test for vendor all method --- lib/far_mar/vendor.rb | 2 ++ spec/far_mar/vendor_spec.rb | 8 ++++++++ 2 files changed, 10 insertions(+) diff --git a/lib/far_mar/vendor.rb b/lib/far_mar/vendor.rb index f72939ce..ec8d3e31 100644 --- a/lib/far_mar/vendor.rb +++ b/lib/far_mar/vendor.rb @@ -14,5 +14,7 @@ def self.convert_to_hash(array) vendor[:market_id] = array[3].to_i return vendor end + + end end diff --git a/spec/far_mar/vendor_spec.rb b/spec/far_mar/vendor_spec.rb index c49494c3..a1e8c2ba 100644 --- a/spec/far_mar/vendor_spec.rb +++ b/spec/far_mar/vendor_spec.rb @@ -13,6 +13,14 @@ expect(vendor.emp_num).to eq(10) expect(vendor.market_id).to eq(2) end + it "returns an array of 2690 Vendor objects" do + vendors = FarMar::Vendor.all + expect(vendors).to be_an(Array) + expect(vendors.length).to eq(2690) + expect(vendors[0].id).to eq(1) + expect(vendors[-1].id).to eq(2690) + end + end end end From d7c0791bf72867d9bf70d550dff14fc9c8eca5b5 Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Tue, 20 Oct 2015 14:36:50 -0700 Subject: [PATCH 014/107] create vendor all method --- lib/far_mar/vendor.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/far_mar/vendor.rb b/lib/far_mar/vendor.rb index ec8d3e31..c7e19244 100644 --- a/lib/far_mar/vendor.rb +++ b/lib/far_mar/vendor.rb @@ -15,6 +15,9 @@ def self.convert_to_hash(array) return vendor end - + def self.all + super('./support/vendors.csv') + end + end end From 19a397e53f03d3838adbf13d4000f18291919206 Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Tue, 20 Oct 2015 14:49:33 -0700 Subject: [PATCH 015/107] refactor self method to use filename constant --- lib/far_mar/far_mar_base.rb | 4 ++-- lib/far_mar/market.rb | 5 +---- lib/far_mar/product.rb | 5 +---- lib/far_mar/sale.rb | 6 +----- lib/far_mar/vendor.rb | 6 +----- 5 files changed, 6 insertions(+), 20 deletions(-) diff --git a/lib/far_mar/far_mar_base.rb b/lib/far_mar/far_mar_base.rb index ba2c01a5..a58d8327 100644 --- a/lib/far_mar/far_mar_base.rb +++ b/lib/far_mar/far_mar_base.rb @@ -7,8 +7,8 @@ def initialize(init_hash) end end - def self.all(file) - csv_file = CSV.read(file) + def self.all + csv_file = CSV.read(self::FILENAME) # Create empty array which will hold all the objects objects = [] csv_file.each do |row| diff --git a/lib/far_mar/market.rb b/lib/far_mar/market.rb index 3e82dfa2..f104fb80 100644 --- a/lib/far_mar/market.rb +++ b/lib/far_mar/market.rb @@ -1,6 +1,7 @@ module FarMar class Market < FarMar_Base attr_reader :id, :name, :address, :city, :county, :state, :zip + FILENAME = './support/markets.csv' def initialize(market_hash) super(market_hash) @@ -17,9 +18,5 @@ def self.convert_to_hash(market_array) market_hash[:state] = market_array[5] return market_hash end - - def self.all - super('./support/markets.csv') - end end end diff --git a/lib/far_mar/product.rb b/lib/far_mar/product.rb index eb417173..078befd7 100644 --- a/lib/far_mar/product.rb +++ b/lib/far_mar/product.rb @@ -1,6 +1,7 @@ module FarMar class Product < FarMar_Base attr_reader :id, :name, :vendor_id + FILENAME = './support/products.csv' def initialize(product_hash) super(product_hash) @@ -14,9 +15,5 @@ def self.convert_to_hash(product_array) product_hash[:vendor_id] = product_array[2].to_i return product_hash end - - def self.all - super('./support/products.csv') - end end end diff --git a/lib/far_mar/sale.rb b/lib/far_mar/sale.rb index ababf278..e9f75381 100644 --- a/lib/far_mar/sale.rb +++ b/lib/far_mar/sale.rb @@ -1,6 +1,7 @@ module FarMar class Sale < FarMar_Base attr_reader :id, :amount, :purchase_time, :vendor_id, :product_id + FILENAME = './support/sales.csv' def initialize(sale_hash) super(sale_hash) @@ -15,10 +16,5 @@ def self.convert_to_hash(sale_array) sale_hash[:product_id] = sale_array[4].to_i return sale_hash end - - def self.all - super('./support/sales.csv') - end - end end diff --git a/lib/far_mar/vendor.rb b/lib/far_mar/vendor.rb index c7e19244..e327d41e 100644 --- a/lib/far_mar/vendor.rb +++ b/lib/far_mar/vendor.rb @@ -1,6 +1,7 @@ module FarMar class Vendor < FarMar_Base attr_reader :id, :name, :emp_num, :market_id + FILENAME = './support/vendors.csv' def initialize(vendor_hash) super(vendor_hash) @@ -14,10 +15,5 @@ def self.convert_to_hash(array) vendor[:market_id] = array[3].to_i return vendor end - - def self.all - super('./support/vendors.csv') - end - end end From e7c523477c9a57c394dad6fc1ef2cf0da905702a Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Tue, 20 Oct 2015 14:56:58 -0700 Subject: [PATCH 016/107] create tests for market find method --- spec/far_mar/market_spec.rb | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/spec/far_mar/market_spec.rb b/spec/far_mar/market_spec.rb index 786f84a5..596eb228 100644 --- a/spec/far_mar/market_spec.rb +++ b/spec/far_mar/market_spec.rb @@ -37,5 +37,23 @@ expect(all_markets.length).to eq(500) end end + + describe ".find(id)" do + it "returns nil when id is not an integer" do + expect FarMar::Market.find('a').to be_nil + expect FarMar::Market.find([1, 2]).to be_nil + end + it "returns nil for no matching id" do + expect FarMar::Market.find(9999999999999).to be_nil + expect FarMar::Market.find(-1).to be_nil + end + + it "returns Market object for matching id" do + result = FarMar::Market.find(18) + expect(result).to be_an_instance_of(FarMar::Market) + expect(result.name).to eq("Grand Valley State University Farmers Market") + end + + end end end From 91d633066fea9f8619b0f07b8dc40d1fb94e1350 Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Tue, 20 Oct 2015 15:02:23 -0700 Subject: [PATCH 017/107] complete beginning of market match method --- lib/far_mar/market.rb | 8 ++++++++ spec/far_mar/market_spec.rb | 10 +++++----- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/lib/far_mar/market.rb b/lib/far_mar/market.rb index f104fb80..28aefc6a 100644 --- a/lib/far_mar/market.rb +++ b/lib/far_mar/market.rb @@ -18,5 +18,13 @@ def self.convert_to_hash(market_array) market_hash[:state] = market_array[5] return market_hash end + + def self.find(id) + # binding.pry + if id.class != Fixnum + return nil + end + end + end end diff --git a/spec/far_mar/market_spec.rb b/spec/far_mar/market_spec.rb index 596eb228..6c1d329c 100644 --- a/spec/far_mar/market_spec.rb +++ b/spec/far_mar/market_spec.rb @@ -40,18 +40,18 @@ describe ".find(id)" do it "returns nil when id is not an integer" do - expect FarMar::Market.find('a').to be_nil - expect FarMar::Market.find([1, 2]).to be_nil + expect(FarMar::Market.find('a')).to be_nil + expect(FarMar::Market.find([1, 2])).to be_nil end it "returns nil for no matching id" do - expect FarMar::Market.find(9999999999999).to be_nil - expect FarMar::Market.find(-1).to be_nil + expect(FarMar::Market.find(9999999999999)).to be_nil + expect(FarMar::Market.find(-1)).to be_nil end it "returns Market object for matching id" do result = FarMar::Market.find(18) expect(result).to be_an_instance_of(FarMar::Market) - expect(result.name).to eq("Grand Valley State University Farmers Market") + expect(result.name).to eq("Grand Valley State University Farmers Market") end end From ae15ff7c2554f3fd4b4c226122486284a84f2e46 Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Tue, 20 Oct 2015 15:59:25 -0700 Subject: [PATCH 018/107] create find method for market class --- lib/far_mar/market.rb | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/far_mar/market.rb b/lib/far_mar/market.rb index 28aefc6a..5776ef28 100644 --- a/lib/far_mar/market.rb +++ b/lib/far_mar/market.rb @@ -20,10 +20,17 @@ def self.convert_to_hash(market_array) end def self.find(id) - # binding.pry if id.class != Fixnum return nil end + csv_file = CSV.read(FILENAME) + match_record = csv_file.find { |a| a[0].to_i == id} + if match_record == nil + return nil + else + hash = self.convert_to_hash(match_record) + return FarMar::Market.new(hash) + end end end From 45e2048331671a4b6b7580de01c925e72027c976 Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Tue, 20 Oct 2015 16:01:09 -0700 Subject: [PATCH 019/107] refactor find method to be in FarMar_Base class --- lib/far_mar/far_mar_base.rb | 15 +++++++++++++++ lib/far_mar/market.rb | 14 -------------- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/lib/far_mar/far_mar_base.rb b/lib/far_mar/far_mar_base.rb index a58d8327..a1dfa3d6 100644 --- a/lib/far_mar/far_mar_base.rb +++ b/lib/far_mar/far_mar_base.rb @@ -22,5 +22,20 @@ def self.all end return objects end + + def self.find(id) + if id.class != Fixnum + return nil + end + csv_file = CSV.read(self::FILENAME) + match_record = csv_file.find { |a| a[0].to_i == id} + if match_record == nil + return nil + else + hash = self.convert_to_hash(match_record) + return self.new(hash) + end + end + end end diff --git a/lib/far_mar/market.rb b/lib/far_mar/market.rb index 5776ef28..3ceed61b 100644 --- a/lib/far_mar/market.rb +++ b/lib/far_mar/market.rb @@ -19,19 +19,5 @@ def self.convert_to_hash(market_array) return market_hash end - def self.find(id) - if id.class != Fixnum - return nil - end - csv_file = CSV.read(FILENAME) - match_record = csv_file.find { |a| a[0].to_i == id} - if match_record == nil - return nil - else - hash = self.convert_to_hash(match_record) - return FarMar::Market.new(hash) - end - end - end end From 82b1f4f2435a4dadf3280ad471f8706caaa770f6 Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Tue, 20 Oct 2015 16:03:46 -0700 Subject: [PATCH 020/107] delete redundant code in far_mar_base find method --- lib/far_mar/far_mar_base.rb | 3 --- 1 file changed, 3 deletions(-) diff --git a/lib/far_mar/far_mar_base.rb b/lib/far_mar/far_mar_base.rb index a1dfa3d6..17964be1 100644 --- a/lib/far_mar/far_mar_base.rb +++ b/lib/far_mar/far_mar_base.rb @@ -24,9 +24,6 @@ def self.all end def self.find(id) - if id.class != Fixnum - return nil - end csv_file = CSV.read(self::FILENAME) match_record = csv_file.find { |a| a[0].to_i == id} if match_record == nil From 7693522818fb37a79a6712b479390cf2adcafd06 Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Tue, 20 Oct 2015 16:10:27 -0700 Subject: [PATCH 021/107] create find test for product class --- spec/far_mar/product_spec.rb | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/spec/far_mar/product_spec.rb b/spec/far_mar/product_spec.rb index 5aa40e14..c5a64694 100644 --- a/spec/far_mar/product_spec.rb +++ b/spec/far_mar/product_spec.rb @@ -2,6 +2,7 @@ describe FarMar do describe FarMar::Product do + describe "#initialize" do it "creates a new Product instance" do fake_hash = { @@ -16,6 +17,7 @@ expect(product.vendor_id).to eq(5555) end end + describe ".all" do it "returns an array of 8193 Product objects" do all_products = FarMar::Product.all @@ -26,5 +28,14 @@ expect(all_products[-1].name).to eq("Cruel Beef") end end + + describe ".find(id)" do + it "returns Product object for matching ID" do + result = FarMar::Product.find(11) + expect(result).to be_an_instance_of(FarMar::Product) + expect(result.name).to eq("Gigantic Bread") + expect(result.vendor_id).to eq(6) + end + end end end From 92599661d20a8cee3c0034a3d0a88dfe32cf0185 Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Tue, 20 Oct 2015 16:13:14 -0700 Subject: [PATCH 022/107] create find test for sale spec --- spec/far_mar/sale_spec.rb | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/spec/far_mar/sale_spec.rb b/spec/far_mar/sale_spec.rb index 00226e89..b9926d75 100644 --- a/spec/far_mar/sale_spec.rb +++ b/spec/far_mar/sale_spec.rb @@ -26,5 +26,16 @@ expect(all_sales.length).to eq(12798) end end + + describe ".find(id)" do + it "returns Sale object for matching ID" do + result = FarMar::Sale.find(19) + expect(result).to be_an_instance_of(FarMar::Sale) + expect(result.id).to eq(19) + expect(result.amount).to eq(9035) + expect(result.vendor_id).to eq(4) + expect(result.product_id).to eq(6) + end + end end end From 8f381d966729d49651c7e2482452bd92c6dd1b14 Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Tue, 20 Oct 2015 16:15:15 -0700 Subject: [PATCH 023/107] create test for vendor find method --- spec/far_mar/vendor_spec.rb | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/spec/far_mar/vendor_spec.rb b/spec/far_mar/vendor_spec.rb index a1e8c2ba..eaeb0dea 100644 --- a/spec/far_mar/vendor_spec.rb +++ b/spec/far_mar/vendor_spec.rb @@ -21,6 +21,17 @@ expect(vendors[-1].id).to eq(2690) end + describe ".find(id)" do + it "returns Vendor object for matching ID" do + result = FarMar::Vendor.find(2690) + expect(result).to be_an_instance_of(FarMar::Vendor) + expect(result.id).to eq(2690) + expect(result.name).to eq("Mann-Lueilwitz") + expect(result.emp_num).to eq(4) + expect(result.market_id).to eq(500) + end + end + end end end From 350c3b74c0f123d034fd854050cb3c57ec10e143 Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Tue, 20 Oct 2015 21:13:07 -0700 Subject: [PATCH 024/107] create test for vendors method of Market class --- spec/far_mar/market_spec.rb | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/spec/far_mar/market_spec.rb b/spec/far_mar/market_spec.rb index 6c1d329c..26eb888d 100644 --- a/spec/far_mar/market_spec.rb +++ b/spec/far_mar/market_spec.rb @@ -53,7 +53,25 @@ expect(result).to be_an_instance_of(FarMar::Market) expect(result.name).to eq("Grand Valley State University Farmers Market") end + end + describe "#vendors" do + let(:market) {FarMar::Market.new({ + :id => 20, + :name => "Scottdale Farmers Market", + :address => "1 Centennial Way", + :city => "Scottdale", + :county => "Westmoreland", + :state => "Pennsylvania", + :zip => "15683" + })} + it "returns array of vendors for the market" do + vendors = :market.vendors + expect(vendors).to be_an(array) + expect(vendors.length).to eq(7) + expect(vendors[-1]).to be_an_instance_of(FarMar::Vendor) + expect(vendors[0].name).to eq("Davis Group") + end end end end From 6574074a2705ba6b4aef379f04b7a3eeb36418f4 Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Tue, 20 Oct 2015 21:31:00 -0700 Subject: [PATCH 025/107] bugfix syntax issues in vendor method test --- spec/far_mar/market_spec.rb | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/spec/far_mar/market_spec.rb b/spec/far_mar/market_spec.rb index 26eb888d..e870513e 100644 --- a/spec/far_mar/market_spec.rb +++ b/spec/far_mar/market_spec.rb @@ -56,18 +56,19 @@ end describe "#vendors" do - let(:market) {FarMar::Market.new({ - :id => 20, - :name => "Scottdale Farmers Market", - :address => "1 Centennial Way", - :city => "Scottdale", - :county => "Westmoreland", - :state => "Pennsylvania", - :zip => "15683" - })} + let(:sample_market) { + FarMar::Market.new({ + :id => 20, + :name => "Scottdale Farmers Market", + :address => "1 Centennial Way", + :city => "Scottdale", + :county => "Westmoreland", + :state => "Pennsylvania", + :zip => "15683" }) + } it "returns array of vendors for the market" do - vendors = :market.vendors - expect(vendors).to be_an(array) + vendors = sample_market.vendors + expect(vendors).to be_an(Array) expect(vendors.length).to eq(7) expect(vendors[-1]).to be_an_instance_of(FarMar::Vendor) expect(vendors[0].name).to eq("Davis Group") From b2c706e93d4bc530403f82e7171839bebefcf825 Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Tue, 20 Oct 2015 21:31:22 -0700 Subject: [PATCH 026/107] create vendor method --- lib/far_mar/market.rb | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/lib/far_mar/market.rb b/lib/far_mar/market.rb index 3ceed61b..bfb5130b 100644 --- a/lib/far_mar/market.rb +++ b/lib/far_mar/market.rb @@ -19,5 +19,16 @@ def self.convert_to_hash(market_array) return market_hash end + def vendors + csv_file = CSV.read(FarMar::Vendor::FILENAME) + matches = csv_file.find_all { |row| row[3].to_i == @id } + vendors = [] + matches.each do |vendor_array| + vendor_hash = FarMar::Vendor.convert_to_hash(vendor_array) + vendors.push(FarMar::Vendor.new(vendor_hash)) + end + return vendors + end + end end From 70fd21b46b7bbbe931c1ea129c46a2ce47317ff8 Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Wed, 21 Oct 2015 10:34:20 -0700 Subject: [PATCH 027/107] create vendor self.by_market method --- lib/far_mar/vendor.rb | 11 +++++++++++ spec/far_mar/vendor_spec.rb | 34 +++++++++++++++++++++++++--------- 2 files changed, 36 insertions(+), 9 deletions(-) diff --git a/lib/far_mar/vendor.rb b/lib/far_mar/vendor.rb index e327d41e..d2960fd1 100644 --- a/lib/far_mar/vendor.rb +++ b/lib/far_mar/vendor.rb @@ -15,5 +15,16 @@ def self.convert_to_hash(array) vendor[:market_id] = array[3].to_i return vendor end + + def self.by_market(market_id) + csv_file = CSV.read(FILENAME) + matches = csv_file.find_all { |row| row[3].to_i == market_id } + vendors = [] + matches.each do |vendor_array| + vendor_hash = convert_to_hash(vendor_array) + vendors.push(FarMar::Vendor.new(vendor_hash)) + end + return vendors + end end end diff --git a/spec/far_mar/vendor_spec.rb b/spec/far_mar/vendor_spec.rb index eaeb0dea..83adac54 100644 --- a/spec/far_mar/vendor_spec.rb +++ b/spec/far_mar/vendor_spec.rb @@ -13,6 +13,8 @@ expect(vendor.emp_num).to eq(10) expect(vendor.market_id).to eq(2) end + end + describe "self.all" do it "returns an array of 2690 Vendor objects" do vendors = FarMar::Vendor.all expect(vendors).to be_an(Array) @@ -20,18 +22,32 @@ expect(vendors[0].id).to eq(1) expect(vendors[-1].id).to eq(2690) end + end - describe ".find(id)" do - it "returns Vendor object for matching ID" do - result = FarMar::Vendor.find(2690) - expect(result).to be_an_instance_of(FarMar::Vendor) - expect(result.id).to eq(2690) - expect(result.name).to eq("Mann-Lueilwitz") - expect(result.emp_num).to eq(4) - expect(result.market_id).to eq(500) - end + describe ".find(id)" do + it "returns Vendor object for matching ID" do + result = FarMar::Vendor.find(2690) + expect(result).to be_an_instance_of(FarMar::Vendor) + expect(result.id).to eq(2690) + expect(result.name).to eq("Mann-Lueilwitz") + expect(result.emp_num).to eq(4) + expect(result.market_id).to eq(500) end + end + describe ".by_market(market_id)" do + it "returns nil for an invalid market_id" do + expect(FarMar::Vendor.by_market('a')).to eq([]) + expect(FarMar::Vendor.by_market([1, 2])).to eq([]) + expect(FarMar::Vendor.by_market(-5)).to eq([]) + expect(FarMar::Vendor.by_market(999999999999)).to eq([]) + end + it "returns all the vendors with the given market_id" do + vendors = FarMar::Vendor.by_market(12) + expect(vendors).to be_an_instance_of(Array) + expect(vendors.length).to eq(3) + expect(vendors[0].id).to eq(51) + end end end end From cc59e4607438c8f61521d843962d6f6e805a12f8 Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Wed, 21 Oct 2015 10:35:33 -0700 Subject: [PATCH 028/107] refactor market vendors method --- lib/far_mar/market.rb | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/lib/far_mar/market.rb b/lib/far_mar/market.rb index bfb5130b..5d53f0eb 100644 --- a/lib/far_mar/market.rb +++ b/lib/far_mar/market.rb @@ -20,14 +20,7 @@ def self.convert_to_hash(market_array) end def vendors - csv_file = CSV.read(FarMar::Vendor::FILENAME) - matches = csv_file.find_all { |row| row[3].to_i == @id } - vendors = [] - matches.each do |vendor_array| - vendor_hash = FarMar::Vendor.convert_to_hash(vendor_array) - vendors.push(FarMar::Vendor.new(vendor_hash)) - end - return vendors + return FarMar::Vendor.by_market(@id) end end From ae1fac74abf96804fc6b0c599e7b79556d5a76e9 Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Wed, 21 Oct 2015 10:40:35 -0700 Subject: [PATCH 029/107] create tests for product.by_vendor --- spec/far_mar/product_spec.rb | 16 ++++++++++++++++ spec/far_mar/vendor_spec.rb | 2 +- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/spec/far_mar/product_spec.rb b/spec/far_mar/product_spec.rb index c5a64694..7bbaf144 100644 --- a/spec/far_mar/product_spec.rb +++ b/spec/far_mar/product_spec.rb @@ -37,5 +37,21 @@ expect(result.vendor_id).to eq(6) end end + + describe ".by_vendor(vendor_id)" do + it "returns empty array for an invalid vendor_id" do + expect(FarMar::Product.by_vendor('a')).to eq([]) + expect(FarMar::Product.by_vendor([1, 2])).to eq([]) + expect(FarMar::Product.by_vendor(-5)).to eq([]) + expect(FarMar::Product.by_vendor(999999999999)).to eq([]) + end + + it "returns all the products with the given vendor_id" do + products = FarMar::Product.by_vendor(91) + expect(products).to be_an_instance_of(Array) + expect(products.length).to eq(3) + expect(products[-1].id).to eq(285) + end + end end end diff --git a/spec/far_mar/vendor_spec.rb b/spec/far_mar/vendor_spec.rb index 83adac54..a2002b2e 100644 --- a/spec/far_mar/vendor_spec.rb +++ b/spec/far_mar/vendor_spec.rb @@ -36,7 +36,7 @@ end describe ".by_market(market_id)" do - it "returns nil for an invalid market_id" do + it "returns empty array for an invalid market_id" do expect(FarMar::Vendor.by_market('a')).to eq([]) expect(FarMar::Vendor.by_market([1, 2])).to eq([]) expect(FarMar::Vendor.by_market(-5)).to eq([]) From b0d9433b3d498e900a0422f0852a0759fd4a3b38 Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Wed, 21 Oct 2015 10:44:11 -0700 Subject: [PATCH 030/107] create product.by_vendor method --- lib/far_mar/product.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/lib/far_mar/product.rb b/lib/far_mar/product.rb index 078befd7..0ab2aed2 100644 --- a/lib/far_mar/product.rb +++ b/lib/far_mar/product.rb @@ -15,5 +15,17 @@ def self.convert_to_hash(product_array) product_hash[:vendor_id] = product_array[2].to_i return product_hash end + + # returns all of the products with the given vendor_id + def self.by_vendor(product_id) + csv_file = CSV.read(FILENAME) + matches = csv_file.find_all { |row| row[2].to_i == product_id } + products = [] + matches.each do |product_array| + product_hash = convert_to_hash(product_array) + products.push(FarMar::Product.new(product_hash)) + end + return products + end end end From 2d11ee22a347a6ad8ad071625edc463bfcd9e837 Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Wed, 21 Oct 2015 10:59:13 -0700 Subject: [PATCH 031/107] create test for vendor.products --- lib/far_mar/vendor.rb | 7 +++++++ spec/far_mar/vendor_spec.rb | 25 +++++++++++++++++-------- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/lib/far_mar/vendor.rb b/lib/far_mar/vendor.rb index d2960fd1..04547259 100644 --- a/lib/far_mar/vendor.rb +++ b/lib/far_mar/vendor.rb @@ -16,6 +16,7 @@ def self.convert_to_hash(array) return vendor end + # returns all of the vendors with the given market_id def self.by_market(market_id) csv_file = CSV.read(FILENAME) matches = csv_file.find_all { |row| row[3].to_i == market_id } @@ -26,5 +27,11 @@ def self.by_market(market_id) end return vendors end + + # returns a collection of FarMar::Product instances that are + # associated by the FarMar::Product vendor_id field + def products + + end end end diff --git a/spec/far_mar/vendor_spec.rb b/spec/far_mar/vendor_spec.rb index a2002b2e..a460e0a1 100644 --- a/spec/far_mar/vendor_spec.rb +++ b/spec/far_mar/vendor_spec.rb @@ -2,16 +2,15 @@ describe FarMar do describe FarMar::Vendor do + vendor_hash = FarMar::Vendor.convert_to_hash(["70","Eichmann Group","8","16"]) + let(:sample_vendor) { FarMar::Vendor.new(vendor_hash) } describe "#initialize" do it "creates a new Vendor instance" do - v_array = %w(1 QFC 10 2) - v_hash = FarMar::Vendor.convert_to_hash(v_array) - vendor = FarMar::Vendor.new(v_hash) - expect(vendor).to be_an_instance_of(FarMar::Vendor) - expect(vendor.id).to eq(1) - expect(vendor.name).to eq("QFC") - expect(vendor.emp_num).to eq(10) - expect(vendor.market_id).to eq(2) + expect(sample_vendor).to be_an_instance_of(FarMar::Vendor) + expect(sample_vendor.id).to eq(70) + expect(sample_vendor.name).to eq("Eichmann Group") + expect(sample_vendor.emp_num).to eq(8) + expect(sample_vendor.market_id).to eq(16) end end describe "self.all" do @@ -49,5 +48,15 @@ expect(vendors[0].id).to eq(51) end end + + describe "#products" do + it "returns a collection of Products with the vendor's ID" do + products = sample_vendor.products + expect(products).to be_an(Array) + expect(products.length).to eq(5) + expect(products[0].id).to eq(221) + expect(products[-1].name).to eq("Cheerful Bread") + end + end end end From 352eb9524b3869d0428b8ba52092251aac0e4ca0 Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Wed, 21 Oct 2015 11:00:58 -0700 Subject: [PATCH 032/107] create vendor.products method --- lib/far_mar/vendor.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/far_mar/vendor.rb b/lib/far_mar/vendor.rb index 04547259..217a8bbd 100644 --- a/lib/far_mar/vendor.rb +++ b/lib/far_mar/vendor.rb @@ -31,7 +31,7 @@ def self.by_market(market_id) # returns a collection of FarMar::Product instances that are # associated by the FarMar::Product vendor_id field def products - + return FarMar::Product.by_vendor(@id) end end end From a6181882bcad584ed5bd053862a141f1609cbaa2 Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Wed, 21 Oct 2015 11:16:53 -0700 Subject: [PATCH 033/107] create Sale.by_product(product_id)tests --- lib/far_mar/sale.rb | 5 +++++ spec/far_mar/sale_spec.rb | 16 ++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/lib/far_mar/sale.rb b/lib/far_mar/sale.rb index e9f75381..c6265a6a 100644 --- a/lib/far_mar/sale.rb +++ b/lib/far_mar/sale.rb @@ -16,5 +16,10 @@ def self.convert_to_hash(sale_array) sale_hash[:product_id] = sale_array[4].to_i return sale_hash end + + # returns all of the sales with the given product id + def self.by_product(product_id) + + end end end diff --git a/spec/far_mar/sale_spec.rb b/spec/far_mar/sale_spec.rb index b9926d75..437db99b 100644 --- a/spec/far_mar/sale_spec.rb +++ b/spec/far_mar/sale_spec.rb @@ -37,5 +37,21 @@ expect(result.product_id).to eq(6) end end + + describe ".by_product(product_id)" do + it "returns empty array for an invalid product_id" do + expect(FarMar::Sale.by_product('a')).to eq([]) + expect(FarMar::Sale.by_product([1, 2])).to eq([]) + expect(FarMar::Sale.by_product(-5)).to eq([]) + expect(FarMar::Sale.by_product(999999999999)).to eq([]) + end + it "returns all the products with the given product_id" do + products = FarMar::Sale.by_product(14) + expect(products).to be_an_instance_of(Array) + expect(products.length).to eq(7) + expect(products[0].id).to eq(32) + expect(products[-1].id).to eq(38) + end + end end end From e8a36d1e2fb01225ede838ac21c448e3f3250dd8 Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Wed, 21 Oct 2015 11:19:11 -0700 Subject: [PATCH 034/107] create Sale.by_product method --- lib/far_mar/sale.rb | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/far_mar/sale.rb b/lib/far_mar/sale.rb index c6265a6a..6ea14a81 100644 --- a/lib/far_mar/sale.rb +++ b/lib/far_mar/sale.rb @@ -19,7 +19,14 @@ def self.convert_to_hash(sale_array) # returns all of the sales with the given product id def self.by_product(product_id) - + csv_file = CSV.read(FILENAME) + matches = csv_file.find_all { |row| row[4].to_i == product_id } + sales = [] + matches.each do |sales_array| + sales_hash = convert_to_hash(sales_array) + sales.push(FarMar::Sale.new(sales_hash)) + end + return sales end end end From 65f9e1969f5cd97e86927294f8077607631791a9 Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Wed, 21 Oct 2015 11:27:45 -0700 Subject: [PATCH 035/107] create test for product.sales --- spec/far_mar/product_spec.rb | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/spec/far_mar/product_spec.rb b/spec/far_mar/product_spec.rb index 7bbaf144..fe2a076d 100644 --- a/spec/far_mar/product_spec.rb +++ b/spec/far_mar/product_spec.rb @@ -2,19 +2,14 @@ describe FarMar do describe FarMar::Product do - + product_hash = FarMar::Product.convert_to_hash(["14","Stupendous Carrots","7"]) + let(:sample_product) { FarMar::Product.new(product_hash) } describe "#initialize" do it "creates a new Product instance" do - fake_hash = { - :id => 1, - :name => "Crispy Spiders", - :vendor_id => 5555 - } - product = FarMar::Product.new(fake_hash) - expect(product).to be_an_instance_of(FarMar::Product) - expect(product.id).to eq(1) - expect(product.name).to eq("Crispy Spiders") - expect(product.vendor_id).to eq(5555) + expect(sample_product).to be_an_instance_of(FarMar::Product) + expect(sample_product.id).to eq(14) + expect(sample_product.name).to eq("Stupendous Carrots") + expect(sample_product.vendor_id).to eq(7) end end @@ -53,5 +48,15 @@ expect(products[-1].id).to eq(285) end end + + describe "#sales" do + it "returns array of Sales that match the product id" do + sales = sample.product.sales + expect(sales).to be_an_instance_of(Array) + expect(sales.length).to eq(7) + expect(sales[0].id).to eq(32) + expect(sales[-1].id).to eq(38) + end + end end end From 011d60d8570d705040290f7e31c9f33b02835e67 Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Wed, 21 Oct 2015 11:33:05 -0700 Subject: [PATCH 036/107] create product.sales method --- 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 0ab2aed2..4cc8ef8d 100644 --- a/lib/far_mar/product.rb +++ b/lib/far_mar/product.rb @@ -27,5 +27,9 @@ def self.by_vendor(product_id) end return products end + + def sales + return FarMar::Sale.by_product(@id) + end end end diff --git a/spec/far_mar/product_spec.rb b/spec/far_mar/product_spec.rb index fe2a076d..196ac2fc 100644 --- a/spec/far_mar/product_spec.rb +++ b/spec/far_mar/product_spec.rb @@ -51,7 +51,7 @@ describe "#sales" do it "returns array of Sales that match the product id" do - sales = sample.product.sales + sales = sample_product.sales expect(sales).to be_an_instance_of(Array) expect(sales.length).to eq(7) expect(sales[0].id).to eq(32) From 1776259b11d9919e58c7c59858afd0cb7b23dc31 Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Wed, 21 Oct 2015 13:31:59 -0700 Subject: [PATCH 037/107] trying to create class variables --- lib/far_mar/market.rb | 5 +++++ lib/far_mar/product.rb | 1 + lib/far_mar/sale.rb | 1 + lib/far_mar/vendor.rb | 1 + 4 files changed, 8 insertions(+) diff --git a/lib/far_mar/market.rb b/lib/far_mar/market.rb index 5d53f0eb..91bb9e44 100644 --- a/lib/far_mar/market.rb +++ b/lib/far_mar/market.rb @@ -19,6 +19,11 @@ def self.convert_to_hash(market_array) return market_hash end + def self.all + @@all_objects ||= super + return @@all_objects + end + def vendors return FarMar::Vendor.by_market(@id) end diff --git a/lib/far_mar/product.rb b/lib/far_mar/product.rb index 4cc8ef8d..d0a32150 100644 --- a/lib/far_mar/product.rb +++ b/lib/far_mar/product.rb @@ -2,6 +2,7 @@ module FarMar class Product < FarMar_Base attr_reader :id, :name, :vendor_id FILENAME = './support/products.csv' + @@all_objects = [] def initialize(product_hash) super(product_hash) diff --git a/lib/far_mar/sale.rb b/lib/far_mar/sale.rb index 6ea14a81..9b97c95e 100644 --- a/lib/far_mar/sale.rb +++ b/lib/far_mar/sale.rb @@ -2,6 +2,7 @@ module FarMar class Sale < FarMar_Base attr_reader :id, :amount, :purchase_time, :vendor_id, :product_id FILENAME = './support/sales.csv' + @@all_objects = [] def initialize(sale_hash) super(sale_hash) diff --git a/lib/far_mar/vendor.rb b/lib/far_mar/vendor.rb index 217a8bbd..3918367f 100644 --- a/lib/far_mar/vendor.rb +++ b/lib/far_mar/vendor.rb @@ -2,6 +2,7 @@ module FarMar class Vendor < FarMar_Base attr_reader :id, :name, :emp_num, :market_id FILENAME = './support/vendors.csv' + @@all_objects = [] def initialize(vendor_hash) super(vendor_hash) From 6c60140ec24eeb6f7a1413921ed17a5cf1a40341 Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Wed, 21 Oct 2015 14:03:24 -0700 Subject: [PATCH 038/107] refactor base find and all methods --- lib/far_mar/far_mar_base.rb | 43 ++++++++++++++++++++----------------- 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/lib/far_mar/far_mar_base.rb b/lib/far_mar/far_mar_base.rb index 17964be1..6c67caa2 100644 --- a/lib/far_mar/far_mar_base.rb +++ b/lib/far_mar/far_mar_base.rb @@ -8,30 +8,33 @@ def initialize(init_hash) end def self.all - csv_file = CSV.read(self::FILENAME) - # Create empty array which will hold all the objects - objects = [] - csv_file.each do |row| - # Convert the array to a hash - # binding.pry - csv_hash = self.convert_to_hash(row) - # Create a product object from each row-hash in the csv file - temp = self.new(csv_hash) - # Push product object to array of products - objects.push(temp) + if !self.class_variable_defined?(:@@all_objects) + csv_file = CSV.read(self::FILENAME) + # Create empty array which will hold all the objects + objects = [] + csv_file.each do |row| + # Convert the array to a hash + csv_hash = self.convert_to_hash(row) + # Create a product object from each row-hash in the csv file + temp = self.new(csv_hash) + # Push product object to array of products + objects.push(temp) + end + self.class_variable_set(:@@all_objects, objects) end - return objects end def self.find(id) - csv_file = CSV.read(self::FILENAME) - match_record = csv_file.find { |a| a[0].to_i == id} - if match_record == nil - return nil - else - hash = self.convert_to_hash(match_record) - return self.new(hash) - end + # binding.pry + # csv_file = CSV.read(self::FILENAME) + # match_record = csv_file.find { |a| a[0].to_i == id} + # if match_record == nil + # return nil + # else + # hash = self.convert_to_hash(match_record) + # return self.new(hash) + # end + self.all_objects.find { |obj| obj.id == id } end end From fb55184a57caf58f63a8b21358bc6da5e186d3bc Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Wed, 21 Oct 2015 14:04:14 -0700 Subject: [PATCH 039/107] refactor market all_objects class variable --- lib/far_mar/market.rb | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/lib/far_mar/market.rb b/lib/far_mar/market.rb index 91bb9e44..08790550 100644 --- a/lib/far_mar/market.rb +++ b/lib/far_mar/market.rb @@ -7,6 +7,10 @@ def initialize(market_hash) super(market_hash) end + def self.all_objects + @@all_objects ||= self.all + end + # Converts an array to a hash for passing to market instantiation def self.convert_to_hash(market_array) market_hash = {} @@ -19,11 +23,6 @@ def self.convert_to_hash(market_array) return market_hash end - def self.all - @@all_objects ||= super - return @@all_objects - end - def vendors return FarMar::Vendor.by_market(@id) end From cd3c06a31707d0f92e2ec9c670cd210668bbadb7 Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Wed, 21 Oct 2015 14:06:55 -0700 Subject: [PATCH 040/107] refactor classes to have class variable --- lib/far_mar/product.rb | 5 ++++- lib/far_mar/sale.rb | 5 ++++- lib/far_mar/vendor.rb | 5 ++++- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/lib/far_mar/product.rb b/lib/far_mar/product.rb index d0a32150..9ccae9dc 100644 --- a/lib/far_mar/product.rb +++ b/lib/far_mar/product.rb @@ -2,12 +2,15 @@ module FarMar class Product < FarMar_Base attr_reader :id, :name, :vendor_id FILENAME = './support/products.csv' - @@all_objects = [] def initialize(product_hash) super(product_hash) end + def self.all_objects + @@all_objects ||= self.all + end + # Converts an array to a hash for passing to product instantiation def self.convert_to_hash(product_array) product_hash = {} diff --git a/lib/far_mar/sale.rb b/lib/far_mar/sale.rb index 9b97c95e..05b0e645 100644 --- a/lib/far_mar/sale.rb +++ b/lib/far_mar/sale.rb @@ -2,12 +2,15 @@ module FarMar class Sale < FarMar_Base attr_reader :id, :amount, :purchase_time, :vendor_id, :product_id FILENAME = './support/sales.csv' - @@all_objects = [] def initialize(sale_hash) super(sale_hash) end + def self.all_objects + @@all_objects ||= self.all + end + def self.convert_to_hash(sale_array) sale_hash = {} sale_hash[:id] = sale_array[0].to_i diff --git a/lib/far_mar/vendor.rb b/lib/far_mar/vendor.rb index 3918367f..2ac9ac2b 100644 --- a/lib/far_mar/vendor.rb +++ b/lib/far_mar/vendor.rb @@ -2,12 +2,15 @@ module FarMar class Vendor < FarMar_Base attr_reader :id, :name, :emp_num, :market_id FILENAME = './support/vendors.csv' - @@all_objects = [] def initialize(vendor_hash) super(vendor_hash) end + def self.all_objects + @@all_objects ||= self.all + end + def self.convert_to_hash(array) vendor = {} vendor[:id] = array[0].to_i From 9ab5752c3c74e95bffcf534ba0612353b582ca5b Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Wed, 21 Oct 2015 14:24:10 -0700 Subject: [PATCH 041/107] fix return in base self.all method --- lib/far_mar/far_mar_base.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/far_mar/far_mar_base.rb b/lib/far_mar/far_mar_base.rb index 6c67caa2..8c707582 100644 --- a/lib/far_mar/far_mar_base.rb +++ b/lib/far_mar/far_mar_base.rb @@ -22,6 +22,7 @@ def self.all end self.class_variable_set(:@@all_objects, objects) end + self.all_objects end def self.find(id) From 8d925be67ae6907fb679bcc820513f79eb2d8bf2 Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Wed, 21 Oct 2015 14:25:17 -0700 Subject: [PATCH 042/107] refactor vendor.by_market method --- lib/far_mar/vendor.rb | 9 +-------- spec/far_mar/vendor_spec.rb | 10 +++++----- 2 files changed, 6 insertions(+), 13 deletions(-) diff --git a/lib/far_mar/vendor.rb b/lib/far_mar/vendor.rb index 2ac9ac2b..123ca9d7 100644 --- a/lib/far_mar/vendor.rb +++ b/lib/far_mar/vendor.rb @@ -22,14 +22,7 @@ def self.convert_to_hash(array) # returns all of the vendors with the given market_id def self.by_market(market_id) - csv_file = CSV.read(FILENAME) - matches = csv_file.find_all { |row| row[3].to_i == market_id } - vendors = [] - matches.each do |vendor_array| - vendor_hash = convert_to_hash(vendor_array) - vendors.push(FarMar::Vendor.new(vendor_hash)) - end - return vendors + FarMar::Vendor.all_objects.find_all { |vendor| vendor.market_id == market_id } end # returns a collection of FarMar::Product instances that are diff --git a/spec/far_mar/vendor_spec.rb b/spec/far_mar/vendor_spec.rb index a460e0a1..ba8fa245 100644 --- a/spec/far_mar/vendor_spec.rb +++ b/spec/far_mar/vendor_spec.rb @@ -15,11 +15,11 @@ end describe "self.all" do it "returns an array of 2690 Vendor objects" do - vendors = FarMar::Vendor.all - expect(vendors).to be_an(Array) - expect(vendors.length).to eq(2690) - expect(vendors[0].id).to eq(1) - expect(vendors[-1].id).to eq(2690) + FarMar::Vendor.all + expect(FarMar::Vendor.all_objects).to be_an(Array) + expect(FarMar::Vendor.all_objects.length).to eq(2690) + expect(FarMar::Vendor.all_objects[0].id).to eq(1) + expect(FarMar::Vendor.all_objects[-1].id).to eq(2690) end end From 0704ad670abeaa59960fbd9b836bb2bcd5dd3be2 Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Wed, 21 Oct 2015 14:28:07 -0700 Subject: [PATCH 043/107] refactor product.by_vendor method --- lib/far_mar/product.rb | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/lib/far_mar/product.rb b/lib/far_mar/product.rb index 9ccae9dc..b5b3b153 100644 --- a/lib/far_mar/product.rb +++ b/lib/far_mar/product.rb @@ -21,15 +21,8 @@ def self.convert_to_hash(product_array) end # returns all of the products with the given vendor_id - def self.by_vendor(product_id) - csv_file = CSV.read(FILENAME) - matches = csv_file.find_all { |row| row[2].to_i == product_id } - products = [] - matches.each do |product_array| - product_hash = convert_to_hash(product_array) - products.push(FarMar::Product.new(product_hash)) - end - return products + def self.by_vendor(vendor_id) + FarMar::Product.all_objects.find_all { |product| product.vendor_id == vendor_id } end def sales From aac3714b773034d9a491adacfb8bb96bb830afea Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Wed, 21 Oct 2015 14:31:04 -0700 Subject: [PATCH 044/107] refactor Sale.by_product method --- lib/far_mar/sale.rb | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/lib/far_mar/sale.rb b/lib/far_mar/sale.rb index 05b0e645..c894e5a0 100644 --- a/lib/far_mar/sale.rb +++ b/lib/far_mar/sale.rb @@ -23,14 +23,7 @@ def self.convert_to_hash(sale_array) # returns all of the sales with the given product id def self.by_product(product_id) - csv_file = CSV.read(FILENAME) - matches = csv_file.find_all { |row| row[4].to_i == product_id } - sales = [] - matches.each do |sales_array| - sales_hash = convert_to_hash(sales_array) - sales.push(FarMar::Sale.new(sales_hash)) - end - return sales + FarMar::Sale.all_objects.find_all { |sale| sale.product_id == product_id } end end end From a2a9feb09a592831de6b70aa1c753203aad8ded8 Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Wed, 21 Oct 2015 15:30:03 -0700 Subject: [PATCH 045/107] create tests for vendor.sales --- lib/far_mar/far_mar_base.rb | 9 --------- lib/far_mar/vendor.rb | 6 +++--- spec/far_mar/vendor_spec.rb | 10 ++++++++++ 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/lib/far_mar/far_mar_base.rb b/lib/far_mar/far_mar_base.rb index 8c707582..dea6034f 100644 --- a/lib/far_mar/far_mar_base.rb +++ b/lib/far_mar/far_mar_base.rb @@ -26,15 +26,6 @@ def self.all end def self.find(id) - # binding.pry - # csv_file = CSV.read(self::FILENAME) - # match_record = csv_file.find { |a| a[0].to_i == id} - # if match_record == nil - # return nil - # else - # hash = self.convert_to_hash(match_record) - # return self.new(hash) - # end self.all_objects.find { |obj| obj.id == id } end diff --git a/lib/far_mar/vendor.rb b/lib/far_mar/vendor.rb index 123ca9d7..78cbbae8 100644 --- a/lib/far_mar/vendor.rb +++ b/lib/far_mar/vendor.rb @@ -27,8 +27,8 @@ def self.by_market(market_id) # returns a collection of FarMar::Product instances that are # associated by the FarMar::Product vendor_id field - def products - return FarMar::Product.by_vendor(@id) - end + def products + return FarMar::Product.by_vendor(@id) + end end end diff --git a/spec/far_mar/vendor_spec.rb b/spec/far_mar/vendor_spec.rb index ba8fa245..9db9d163 100644 --- a/spec/far_mar/vendor_spec.rb +++ b/spec/far_mar/vendor_spec.rb @@ -58,5 +58,15 @@ expect(products[-1].name).to eq("Cheerful Bread") end end + + describe "#sales" do + it "returns a collection of Sales with the vendor's ID" do + sales = sample_vendor.sales + expect(sales).to be_an(Array) + expect(sales.length).to eq(7) + expect(sales[0].id).to eq(313) + expect(sales[-1].id).to eq(319) + end + end end end From a0540b06269aaf701d0dfd36afa81f8749964cbb Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Wed, 21 Oct 2015 15:33:11 -0700 Subject: [PATCH 046/107] create vendor.sales method --- lib/far_mar/sale.rb | 4 ++++ lib/far_mar/vendor.rb | 6 ++++++ 2 files changed, 10 insertions(+) diff --git a/lib/far_mar/sale.rb b/lib/far_mar/sale.rb index c894e5a0..e0f05801 100644 --- a/lib/far_mar/sale.rb +++ b/lib/far_mar/sale.rb @@ -25,5 +25,9 @@ def self.convert_to_hash(sale_array) def self.by_product(product_id) FarMar::Sale.all_objects.find_all { |sale| sale.product_id == product_id } end + + def self.by_vendor(vendor_id) + FarMar::Sale.all_objects.find_all { |sale| sale.vendor_id == vendor_id } + end end end diff --git a/lib/far_mar/vendor.rb b/lib/far_mar/vendor.rb index 78cbbae8..ad457591 100644 --- a/lib/far_mar/vendor.rb +++ b/lib/far_mar/vendor.rb @@ -30,5 +30,11 @@ def self.by_market(market_id) def products return FarMar::Product.by_vendor(@id) end + + # returns a collection of FarMar::Sale instances that are + # associated by the vendor_id + def sales + return FarMar::Sale.by_vendor(@id) + end end end From dffc50fd7f598fbc0f3730258aaf9916d123a99b Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Wed, 21 Oct 2015 15:38:54 -0700 Subject: [PATCH 047/107] create vendor.market test --- spec/far_mar/vendor_spec.rb | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/spec/far_mar/vendor_spec.rb b/spec/far_mar/vendor_spec.rb index 9db9d163..439ea59f 100644 --- a/spec/far_mar/vendor_spec.rb +++ b/spec/far_mar/vendor_spec.rb @@ -68,5 +68,14 @@ expect(sales[-1].id).to eq(319) end end + + describe "#market" do + it "returns the market associated with the vendor" do + sample_market = sample_vendor.market + expect(sample_market).to be_an_instance_of(FarMar::Market) + expect(sample_market.id).to eq(sample_vendor.market_id) + expect(sample_market.name).to eq("Riverside Farmers' Market") + end + end end end From f078cfaca3d08c111dd537606af70d89e788f5d3 Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Wed, 21 Oct 2015 15:41:26 -0700 Subject: [PATCH 048/107] create vendor.market method --- lib/far_mar/vendor.rb | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/far_mar/vendor.rb b/lib/far_mar/vendor.rb index ad457591..c12d3670 100644 --- a/lib/far_mar/vendor.rb +++ b/lib/far_mar/vendor.rb @@ -36,5 +36,11 @@ def products def sales return FarMar::Sale.by_vendor(@id) end + + # returns the FarMar::Market instance that is associate with this vendor + # using the FarMar::Vendor market_id field + def market + return FarMar::Market.find(@market_id) + end end end From 488bae73193ca1f786357384097321fbca38828e Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Wed, 21 Oct 2015 15:44:44 -0700 Subject: [PATCH 049/107] create test for Product.vendor --- spec/far_mar/product_spec.rb | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/spec/far_mar/product_spec.rb b/spec/far_mar/product_spec.rb index 196ac2fc..39921f54 100644 --- a/spec/far_mar/product_spec.rb +++ b/spec/far_mar/product_spec.rb @@ -58,5 +58,14 @@ expect(sales[-1].id).to eq(38) end end + + describe "#vendor" do + it "returns the Vendor associated with this Product" do + sample_vendor = sample_product.vendor + expect(sample_vendor).to be_an_instance_of(Vendor) + expect(sample_vendor.id).to eq(sample_product.vendor_id) + expect(sample_vendor.name).to eq("Bechtelar Inc") + end + end end end From b96f568379226e9da0fe24e0cfac0221f2b23580 Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Wed, 21 Oct 2015 15:47:35 -0700 Subject: [PATCH 050/107] create Product.vendor method --- lib/far_mar/product.rb | 8 ++++++++ spec/far_mar/product_spec.rb | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/far_mar/product.rb b/lib/far_mar/product.rb index b5b3b153..76165a2e 100644 --- a/lib/far_mar/product.rb +++ b/lib/far_mar/product.rb @@ -25,6 +25,14 @@ def self.by_vendor(vendor_id) FarMar::Product.all_objects.find_all { |product| product.vendor_id == vendor_id } end + # returns the FarMar::Vendor instance that is associated with this + # vendor using the vendor_id field + def vendor + return FarMar::Vendor.find(@vendor_id) + end + + # returns a collection of FarMar::Sale instances that are associated using + # the product_id field def sales return FarMar::Sale.by_product(@id) end diff --git a/spec/far_mar/product_spec.rb b/spec/far_mar/product_spec.rb index 39921f54..77f20c70 100644 --- a/spec/far_mar/product_spec.rb +++ b/spec/far_mar/product_spec.rb @@ -62,7 +62,7 @@ describe "#vendor" do it "returns the Vendor associated with this Product" do sample_vendor = sample_product.vendor - expect(sample_vendor).to be_an_instance_of(Vendor) + expect(sample_vendor).to be_an_instance_of(FarMar::Vendor) expect(sample_vendor.id).to eq(sample_product.vendor_id) expect(sample_vendor.name).to eq("Bechtelar Inc") end From 4adfe0ccfaeafe5021003f7a368065f069a8b1d6 Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Wed, 21 Oct 2015 15:54:32 -0700 Subject: [PATCH 051/107] create test for Vendor.revenue --- spec/far_mar/vendor_spec.rb | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/spec/far_mar/vendor_spec.rb b/spec/far_mar/vendor_spec.rb index 439ea59f..74844b43 100644 --- a/spec/far_mar/vendor_spec.rb +++ b/spec/far_mar/vendor_spec.rb @@ -77,5 +77,11 @@ expect(sample_market.name).to eq("Riverside Farmers' Market") end end + + describe "#revenue" do + it "returns the sum of all the vendor's sales" do + expect(sample_vendor.revenue).to eq(17211) + end + end end end From fb459c72bad6631c548002b85232b036b10b1bce Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Wed, 21 Oct 2015 16:00:39 -0700 Subject: [PATCH 052/107] create Vendor.sales method --- lib/far_mar/vendor.rb | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/lib/far_mar/vendor.rb b/lib/far_mar/vendor.rb index c12d3670..2eb3cc4f 100644 --- a/lib/far_mar/vendor.rb +++ b/lib/far_mar/vendor.rb @@ -42,5 +42,12 @@ def sales def market return FarMar::Market.find(@market_id) end + + # returns the sum of all of the vendor's sales (in cents) + def revenue + sales_array = sales + sales_array.inject(0) { |sum, sale_obj| sum + sale_obj.amount } + end + end end From e8c52801f82d6438c5f87d94cab4a0fb201b5b98 Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Wed, 21 Oct 2015 16:04:16 -0700 Subject: [PATCH 053/107] create Product.number_of_sales test --- lib/far_mar/product.rb | 5 +++++ spec/far_mar/product_spec.rb | 6 ++++++ 2 files changed, 11 insertions(+) diff --git a/lib/far_mar/product.rb b/lib/far_mar/product.rb index 76165a2e..4f3d65f9 100644 --- a/lib/far_mar/product.rb +++ b/lib/far_mar/product.rb @@ -36,5 +36,10 @@ def vendor def sales return FarMar::Sale.by_product(@id) end + + # returns the number of times the product has been sold + def number_of_sales + + end end end diff --git a/spec/far_mar/product_spec.rb b/spec/far_mar/product_spec.rb index 77f20c70..8736c234 100644 --- a/spec/far_mar/product_spec.rb +++ b/spec/far_mar/product_spec.rb @@ -67,5 +67,11 @@ expect(sample_vendor.name).to eq("Bechtelar Inc") end end + + describe "#number_of_sales" do + it "returns the number of times the product has been sold" do + expect(sample_product.number_of_sales).to eq(7) + end + end end end From 8c197480544cf641a21da0019e11cfcc99abfbf9 Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Wed, 21 Oct 2015 16:06:27 -0700 Subject: [PATCH 054/107] create Product.number_of_sales method --- lib/far_mar/product.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/far_mar/product.rb b/lib/far_mar/product.rb index 4f3d65f9..2fc32b4c 100644 --- a/lib/far_mar/product.rb +++ b/lib/far_mar/product.rb @@ -39,7 +39,7 @@ def sales # returns the number of times the product has been sold def number_of_sales - + return sales.length end end end From 86b0f8ba99e35511640a8bb9d9066d2a2032b1a8 Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Wed, 21 Oct 2015 16:17:12 -0700 Subject: [PATCH 055/107] create test for Sale.vendor --- spec/far_mar/sale_spec.rb | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/spec/far_mar/sale_spec.rb b/spec/far_mar/sale_spec.rb index 437db99b..9c434fa2 100644 --- a/spec/far_mar/sale_spec.rb +++ b/spec/far_mar/sale_spec.rb @@ -2,17 +2,16 @@ describe FarMar do describe FarMar::Sale do + sale_hash = FarMar::Sale.convert_to_hash(["1", "9290", "2013-11-12 04:36:56 -0800", "5", "12"]) + let(:sample_sale) { FarMar::Sale.new(sale_hash) } describe "#initialize" do it "creates a new Sale instance" do - fake_array = ["1", "9290", "2013-11-12 04:36:56 -0800", "5", "12"] - fake_hash = FarMar::Sale.convert_to_hash(fake_array) - sale = FarMar::Sale.new(fake_hash) - expect(sale).to be_an_instance_of(FarMar::Sale) - expect(sale.id).to eq(1) - expect(sale.amount).to eq(9290) - expect(sale.purchase_time.class).to be(DateTime) - expect(sale.vendor_id).to eq(5) - expect(sale.product_id).to eq(12) + expect(sample_sale).to be_an_instance_of(FarMar::Sale) + expect(sample_sale.id).to eq(1) + expect(sample_sale.amount).to eq(9290) + expect(sample_sale.purchase_time.class).to be(DateTime) + expect(sample_sale.vendor_id).to eq(5) + expect(sample_sale.product_id).to eq(12) end end describe ".all" do @@ -53,5 +52,13 @@ expect(products[-1].id).to eq(38) end end + + describe "#vendor" do + it "returns vendor associated with the sale" do + sample_vendor = sample_sale.vendor + expect(sample_vendor.id).to eq(sample_sale.vendor_id) + expect(sample_vendor.name).to eq("Reynolds, Schmitt and Klocko") + end + end end end From 1d1bd1fbf51caa3058503d5427d468733345a01a Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Wed, 21 Oct 2015 16:18:59 -0700 Subject: [PATCH 056/107] create test for Sale.product --- spec/far_mar/sale_spec.rb | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/spec/far_mar/sale_spec.rb b/spec/far_mar/sale_spec.rb index 9c434fa2..dff6aacf 100644 --- a/spec/far_mar/sale_spec.rb +++ b/spec/far_mar/sale_spec.rb @@ -60,5 +60,13 @@ expect(sample_vendor.name).to eq("Reynolds, Schmitt and Klocko") end end + + describe "#product" do + it "returns the Product associate with the sale" do + sample_product = sample_sale.product + expect(sample_product.id).to eq(sample_sale.product_id) + expect(sample_product.name).to eq("Gorgeous Fish") + end + end end end From e56c702c630ef3763df88358b2075263d524e9c3 Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Wed, 21 Oct 2015 16:22:11 -0700 Subject: [PATCH 057/107] create Sale.vendor method --- lib/far_mar/sale.rb | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/far_mar/sale.rb b/lib/far_mar/sale.rb index e0f05801..5e41ed10 100644 --- a/lib/far_mar/sale.rb +++ b/lib/far_mar/sale.rb @@ -23,11 +23,19 @@ def self.convert_to_hash(sale_array) # returns all of the sales with the given product id def self.by_product(product_id) - FarMar::Sale.all_objects.find_all { |sale| sale.product_id == product_id } + return FarMar::Sale.all_objects.find_all { |sale| sale.product_id == product_id } end + # returns all of the vendors with the given vendor id def self.by_vendor(vendor_id) - FarMar::Sale.all_objects.find_all { |sale| sale.vendor_id == vendor_id } + return FarMar::Sale.all_objects.find_all { |sale| sale.vendor_id == vendor_id } end + + # returns the Vendor that is associated with the sale + def vendor + return FarMar::Vendor.find(@vendor_id) + end + + # returns the Product that is associated with the sale end end From 0af63d6377c50fd47ef4fbc5a4c1325403499ec4 Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Wed, 21 Oct 2015 16:22:56 -0700 Subject: [PATCH 058/107] create Sale.product method --- lib/far_mar/sale.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/far_mar/sale.rb b/lib/far_mar/sale.rb index 5e41ed10..e785a579 100644 --- a/lib/far_mar/sale.rb +++ b/lib/far_mar/sale.rb @@ -37,5 +37,8 @@ def vendor end # returns the Product that is associated with the sale + def product + return FarMar::Product.find(@product_id) + end end end From 630e3e0e060ca9d3647e15df2d6ceca4f6389d2e Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Wed, 21 Oct 2015 16:39:31 -0700 Subject: [PATCH 059/107] create test for Sale.between method --- spec/far_mar/sale_spec.rb | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/spec/far_mar/sale_spec.rb b/spec/far_mar/sale_spec.rb index dff6aacf..50d3585f 100644 --- a/spec/far_mar/sale_spec.rb +++ b/spec/far_mar/sale_spec.rb @@ -68,5 +68,13 @@ expect(sample_product.name).to eq("Gorgeous Fish") end end + + describe "#between(beginning_time, end_time)" do + it "returns all sales where the purchase time is between two times given" do + beginning = FarMar::Sale.find(1990).purchase_time + ending = FarMar::Sale.find(3602).purchase_time + expect(FarMar::Sale.between(beginning, ending)).to eq(24) + end + end end end From 8291147f5e687945d7cab4b99dccf617969234ed Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Wed, 21 Oct 2015 16:44:58 -0700 Subject: [PATCH 060/107] create Sale.between method --- lib/far_mar/sale.rb | 6 ++++++ spec/far_mar/sale_spec.rb | 4 +++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/far_mar/sale.rb b/lib/far_mar/sale.rb index e785a579..082e49f7 100644 --- a/lib/far_mar/sale.rb +++ b/lib/far_mar/sale.rb @@ -31,6 +31,12 @@ def self.by_vendor(vendor_id) return FarMar::Sale.all_objects.find_all { |sale| sale.vendor_id == vendor_id } end + # returns a collection of FarMar::Sale objects where the purchase time + # is between the two times given as arguments + def self.between(beginning_time, end_time) + return FarMar::Sale.all_objects.find_all { |sale_obj| sale_obj.purchase_time > beginning_time && sale_obj.purchase_time < end_time } + end + # returns the Vendor that is associated with the sale def vendor return FarMar::Vendor.find(@vendor_id) diff --git a/spec/far_mar/sale_spec.rb b/spec/far_mar/sale_spec.rb index 50d3585f..9ed354eb 100644 --- a/spec/far_mar/sale_spec.rb +++ b/spec/far_mar/sale_spec.rb @@ -73,7 +73,9 @@ it "returns all sales where the purchase time is between two times given" do beginning = FarMar::Sale.find(1990).purchase_time ending = FarMar::Sale.find(3602).purchase_time - expect(FarMar::Sale.between(beginning, ending)).to eq(24) + between_array = FarMar::Sale.between(beginning, ending) + expect(between_array).to be_an_instance_of(Array) + expect(between_array.length).to eq(24) end end end From bea2ce42bad690a1763621f70a3889ffb1e04b51 Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Wed, 21 Oct 2015 16:51:06 -0700 Subject: [PATCH 061/107] add error checking for sale.between --- lib/far_mar/sale.rb | 4 ++++ spec/far_mar/sale_spec.rb | 12 ++++++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/lib/far_mar/sale.rb b/lib/far_mar/sale.rb index 082e49f7..c879782e 100644 --- a/lib/far_mar/sale.rb +++ b/lib/far_mar/sale.rb @@ -34,6 +34,10 @@ def self.by_vendor(vendor_id) # returns a collection of FarMar::Sale objects where the purchase time # is between the two times given as arguments def self.between(beginning_time, end_time) + return [] if + beginning_time.class != DateTime || + end_time.class != DateTime || + end_time <= beginning_time return FarMar::Sale.all_objects.find_all { |sale_obj| sale_obj.purchase_time > beginning_time && sale_obj.purchase_time < end_time } end diff --git a/spec/far_mar/sale_spec.rb b/spec/far_mar/sale_spec.rb index 9ed354eb..92313bc8 100644 --- a/spec/far_mar/sale_spec.rb +++ b/spec/far_mar/sale_spec.rb @@ -70,9 +70,17 @@ end describe "#between(beginning_time, end_time)" do + beginning = FarMar::Sale.find(1990).purchase_time + ending = FarMar::Sale.find(3602).purchase_time + + it "returns empty array if parameters not DateTime" do + expect(FarMar::Sale.between('a', 'b')).to eq([]) + end + it "returns empty array if end time is before or equal to beginning" do + expect(FarMar::Sale.between(ending, beginning)).to eq([]) + end it "returns all sales where the purchase time is between two times given" do - beginning = FarMar::Sale.find(1990).purchase_time - ending = FarMar::Sale.find(3602).purchase_time + between_array = FarMar::Sale.between(beginning, ending) expect(between_array).to be_an_instance_of(Array) expect(between_array.length).to eq(24) From 6258ab9fc86ba73052083c5783d2c07e2d6a15f7 Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Wed, 21 Oct 2015 21:13:37 -0700 Subject: [PATCH 062/107] bugfix missing zip parameter in market class --- lib/far_mar/market.rb | 1 + spec/far_mar/market_spec.rb | 20 ++++++-------------- 2 files changed, 7 insertions(+), 14 deletions(-) diff --git a/lib/far_mar/market.rb b/lib/far_mar/market.rb index 08790550..12d137c7 100644 --- a/lib/far_mar/market.rb +++ b/lib/far_mar/market.rb @@ -20,6 +20,7 @@ def self.convert_to_hash(market_array) market_hash[:city] = market_array[3] market_hash[:county] = market_array[4] market_hash[:state] = market_array[5] + market_hash[:zip] = market_array[6] return market_hash end diff --git a/spec/far_mar/market_spec.rb b/spec/far_mar/market_spec.rb index e870513e..cf107e57 100644 --- a/spec/far_mar/market_spec.rb +++ b/spec/far_mar/market_spec.rb @@ -2,22 +2,14 @@ describe FarMar do describe FarMar::Market do + market_hash = FarMar::Market.convert_to_hash(["64","Oakmont Farmers Market","1 W Eagle Road","Havertown","Delaware","Pennsylvania","19083"]) + let(:sample_market) { FarMar::Market.new(market_hash)} describe "#initialize" do it "creates a new Market instance" do - fake_hash = { - :id => 1, - :name => "Holiday", - :address => "118 N 180th St", - :city => "Shoreline", - :county => "King County", - :state => "WA", - :zip => "98133" - } - new_market = FarMar::Market.new(fake_hash) - expect(new_market).to be_an_instance_of(FarMar::Market) - expect(new_market.id).to eq(1) - expect(new_market.name).to eq("Holiday") - expect(new_market.zip).to eq("98133") + expect(sample_market).to be_an_instance_of(FarMar::Market) + expect(sample_market.id).to eq(64) + expect(sample_market.name).to eq("Oakmont Farmers Market") + expect(sample_market.zip).to eq("19083") # I tried to iterate over the original hash but that was a fail. # fake_hash.each do |k, v| From d501b903f77e61f5939fd00fbde365dd150cae6c Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Wed, 21 Oct 2015 21:21:18 -0700 Subject: [PATCH 063/107] refactor market_spec init test to iterate over hash --- spec/far_mar/market_spec.rb | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/spec/far_mar/market_spec.rb b/spec/far_mar/market_spec.rb index cf107e57..838659c1 100644 --- a/spec/far_mar/market_spec.rb +++ b/spec/far_mar/market_spec.rb @@ -7,14 +7,11 @@ describe "#initialize" do it "creates a new Market instance" do expect(sample_market).to be_an_instance_of(FarMar::Market) - expect(sample_market.id).to eq(64) - expect(sample_market.name).to eq("Oakmont Farmers Market") - expect(sample_market.zip).to eq("19083") - - # I tried to iterate over the original hash but that was a fail. - # fake_hash.each do |k, v| - # expect(new_market.k.to_s).to eq(v) - # end + end + market_hash.each do |attr, val| + it "assigns the #{attr} attribute correctly" do + expect(sample_market.instance_variable_get("@#{attr}")).to eq(val) + end end end From 41dc121b407d9473f7e34db5b33732fa370bc093 Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Wed, 21 Oct 2015 21:23:05 -0700 Subject: [PATCH 064/107] refactor product spec to check each attribute --- spec/far_mar/product_spec.rb | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/spec/far_mar/product_spec.rb b/spec/far_mar/product_spec.rb index 8736c234..1d9f58eb 100644 --- a/spec/far_mar/product_spec.rb +++ b/spec/far_mar/product_spec.rb @@ -7,9 +7,11 @@ describe "#initialize" do it "creates a new Product instance" do expect(sample_product).to be_an_instance_of(FarMar::Product) - expect(sample_product.id).to eq(14) - expect(sample_product.name).to eq("Stupendous Carrots") - expect(sample_product.vendor_id).to eq(7) + end + product_hash.each do |attr, val| + it "assigns the #{attr} attribute correctly" do + expect(sample_product.instance_variable_get("@#{attr}")).to eq(val) + end end end From 791415764f936248d2783e801ecb4255729fb95e Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Wed, 21 Oct 2015 21:29:44 -0700 Subject: [PATCH 065/107] refactor sale_spec init tests --- spec/far_mar/sale_spec.rb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/spec/far_mar/sale_spec.rb b/spec/far_mar/sale_spec.rb index 92313bc8..76c074e3 100644 --- a/spec/far_mar/sale_spec.rb +++ b/spec/far_mar/sale_spec.rb @@ -7,11 +7,11 @@ describe "#initialize" do it "creates a new Sale instance" do expect(sample_sale).to be_an_instance_of(FarMar::Sale) - expect(sample_sale.id).to eq(1) - expect(sample_sale.amount).to eq(9290) - expect(sample_sale.purchase_time.class).to be(DateTime) - expect(sample_sale.vendor_id).to eq(5) - expect(sample_sale.product_id).to eq(12) + end + sale_hash.each do |attr, val| + it "assigns the #{attr} attribute correctly" do + expect(sample_sale.instance_variable_get("@#{attr}")).to eq(val) + end end end describe ".all" do From 9ae9e65cecbaf9279ddfbc1b7bbeaa519668987f Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Wed, 21 Oct 2015 21:30:50 -0700 Subject: [PATCH 066/107] refactor vendor_spec init tests --- spec/far_mar/vendor_spec.rb | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/spec/far_mar/vendor_spec.rb b/spec/far_mar/vendor_spec.rb index 74844b43..3a37a897 100644 --- a/spec/far_mar/vendor_spec.rb +++ b/spec/far_mar/vendor_spec.rb @@ -7,10 +7,11 @@ describe "#initialize" do it "creates a new Vendor instance" do expect(sample_vendor).to be_an_instance_of(FarMar::Vendor) - expect(sample_vendor.id).to eq(70) - expect(sample_vendor.name).to eq("Eichmann Group") - expect(sample_vendor.emp_num).to eq(8) - expect(sample_vendor.market_id).to eq(16) + end + vendor_hash.each do |attr, val| + it "assigns the #{attr} attribute correctly" do + expect(sample_vendor.instance_variable_get("@#{attr}")).to eq(val) + end end end describe "self.all" do From 04a852d28273a28cb2cf86c47e50b9958292c412 Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Wed, 21 Oct 2015 21:44:04 -0700 Subject: [PATCH 067/107] create test for Market.products --- spec/far_mar/market_spec.rb | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/spec/far_mar/market_spec.rb b/spec/far_mar/market_spec.rb index 838659c1..c3e12471 100644 --- a/spec/far_mar/market_spec.rb +++ b/spec/far_mar/market_spec.rb @@ -63,5 +63,16 @@ expect(vendors[0].name).to eq("Davis Group") end end + + describe "#products" do + it "returns all Products sold in the market" do + products = sample_market.products + expect(products).to be_an_instance_of(Array) + expect(products.length).to eq(4) + expect(products[0]).to be_an_instance_of(FarMar::Product) + expect(products[0].id).to eq(1073) + expect(products[-1].id).to eq(1076) + end + end end end From cbf491ff26206b8cca88149b4fd7f9626d664b6a Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Wed, 21 Oct 2015 21:53:34 -0700 Subject: [PATCH 068/107] complete market.products method --- lib/far_mar/market.rb | 9 +++++++++ spec/far_mar/market_spec.rb | 15 ++++----------- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/lib/far_mar/market.rb b/lib/far_mar/market.rb index 12d137c7..88457a8d 100644 --- a/lib/far_mar/market.rb +++ b/lib/far_mar/market.rb @@ -28,5 +28,14 @@ def vendors return FarMar::Vendor.by_market(@id) end + def products + products = [] + v = vendors + v.each do |vendor| + products += vendor.products + end + return products + end + end end diff --git a/spec/far_mar/market_spec.rb b/spec/far_mar/market_spec.rb index c3e12471..140558da 100644 --- a/spec/far_mar/market_spec.rb +++ b/spec/far_mar/market_spec.rb @@ -4,6 +4,7 @@ describe FarMar::Market do market_hash = FarMar::Market.convert_to_hash(["64","Oakmont Farmers Market","1 W Eagle Road","Havertown","Delaware","Pennsylvania","19083"]) let(:sample_market) { FarMar::Market.new(market_hash)} + let(:sample_market2) { FarMar::Market.new({ :id => 20, :name => "Scottdale Farmers Market", :address => "1 Centennial Way", :city => "Scottdale", :county => "Westmoreland", :state => "Pennsylvania", :zip => "15683" })} describe "#initialize" do it "creates a new Market instance" do expect(sample_market).to be_an_instance_of(FarMar::Market) @@ -45,18 +46,8 @@ end describe "#vendors" do - let(:sample_market) { - FarMar::Market.new({ - :id => 20, - :name => "Scottdale Farmers Market", - :address => "1 Centennial Way", - :city => "Scottdale", - :county => "Westmoreland", - :state => "Pennsylvania", - :zip => "15683" }) - } it "returns array of vendors for the market" do - vendors = sample_market.vendors + vendors = sample_market2.vendors expect(vendors).to be_an(Array) expect(vendors.length).to eq(7) expect(vendors[-1]).to be_an_instance_of(FarMar::Vendor) @@ -72,6 +63,8 @@ expect(products[0]).to be_an_instance_of(FarMar::Product) expect(products[0].id).to eq(1073) expect(products[-1].id).to eq(1076) + products2 = sample_market2.products + expect(products2.length).to eq(22) end end end From 24315f5c25d03f6845875aa572d80b2b22af702c Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Thu, 22 Oct 2015 10:36:55 -0700 Subject: [PATCH 069/107] create test for preferred vendor --- spec/far_mar/market_spec.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/spec/far_mar/market_spec.rb b/spec/far_mar/market_spec.rb index 140558da..095e8e87 100644 --- a/spec/far_mar/market_spec.rb +++ b/spec/far_mar/market_spec.rb @@ -67,5 +67,17 @@ expect(products2.length).to eq(22) end end + + describe "#preferred_vendor" do + it "returns the market's vendor with the highest revenue" do + max_vendor1 = sample_market.preferred_vendor + expect(max_vendor1).to be_an_instance_of(FarMar::Vendor) + expect(max_vendor1.id).to eq(64) + max_vendor2 = sample_market2.preferred_vendor + expect(max_vendor2).to be_an_instance_of(FarMar::Vendor) + expect(max_vendor2.id).to eq(102) + expect(max_vendor2.revenue).to eq(44789) + end + end end end From d31d8fbdab25f8af7dd1bf00c3d17e0048b7a868 Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Thu, 22 Oct 2015 10:46:24 -0700 Subject: [PATCH 070/107] create market.preferred_vendor method --- lib/far_mar/market.rb | 18 ++++++++++++++++++ spec/far_mar/market_spec.rb | 2 +- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/lib/far_mar/market.rb b/lib/far_mar/market.rb index 88457a8d..c7a0f033 100644 --- a/lib/far_mar/market.rb +++ b/lib/far_mar/market.rb @@ -24,10 +24,14 @@ def self.convert_to_hash(market_array) return market_hash end + # Returns a collection of FarMar::Vendor instances that are associated + # with the market by the market_id field def vendors return FarMar::Vendor.by_market(@id) end + # returns a collection of FarMar::Product instances that are associated + # to the market through the FarMar::Vendor class def products products = [] v = vendors @@ -37,5 +41,19 @@ def products return products end + # returns the vendor with the highest revenue from a market + def preferred_vendor + matches = vendors + if matches.length == 0 + return nil + elsif matches.length == 1 + return matches[0] + else + revenues = matches.map { |v| v.revenue } + max_index = revenues.each_with_index.max[1] + return matches[max_index] + end + end + end end diff --git a/spec/far_mar/market_spec.rb b/spec/far_mar/market_spec.rb index 095e8e87..f8fdc8a3 100644 --- a/spec/far_mar/market_spec.rb +++ b/spec/far_mar/market_spec.rb @@ -72,7 +72,7 @@ it "returns the market's vendor with the highest revenue" do max_vendor1 = sample_market.preferred_vendor expect(max_vendor1).to be_an_instance_of(FarMar::Vendor) - expect(max_vendor1.id).to eq(64) + expect(max_vendor1.id).to eq(350) max_vendor2 = sample_market2.preferred_vendor expect(max_vendor2).to be_an_instance_of(FarMar::Vendor) expect(max_vendor2.id).to eq(102) From 1ea8a809e3a50a17e091b3ab2ee6dfab332c2efe Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Thu, 22 Oct 2015 10:48:36 -0700 Subject: [PATCH 071/107] create test for market.worst_vendor --- spec/far_mar/market_spec.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/spec/far_mar/market_spec.rb b/spec/far_mar/market_spec.rb index f8fdc8a3..345e98c9 100644 --- a/spec/far_mar/market_spec.rb +++ b/spec/far_mar/market_spec.rb @@ -79,5 +79,17 @@ expect(max_vendor2.revenue).to eq(44789) end end + + describe "#worst_vendor" do + it "returns the market's vendor with the lowest revenue" do + min_vendor1 = sample_market.worst_vendor + expect(min_vendor1).to be_an_instance_of(FarMar::Vendor) + expect(min_vendor1.id).to eq(350) + min_vendor2 = sample_market2.worst_vendor + expect(min_vendor2).to be_an_instance_of(FarMar::Vendor) + expect(min_vendor2.id).to eq(105) + expect(min_vendor2.revenue).to eq(1810) + end + end end end From 531b4e2c5033a47d263c7d76aa10f9ed24622b3a Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Thu, 22 Oct 2015 10:50:53 -0700 Subject: [PATCH 072/107] create market.worst_vendor method --- lib/far_mar/market.rb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/lib/far_mar/market.rb b/lib/far_mar/market.rb index c7a0f033..f72f9ad0 100644 --- a/lib/far_mar/market.rb +++ b/lib/far_mar/market.rb @@ -55,5 +55,19 @@ def preferred_vendor end end + # returns the vendor with the lowest revenue from a market + def worst_vendor + matches = vendors + if matches.length == 0 + return nil + elsif matches.length == 1 + return matches[0] + else + revenues = matches.map { |v| v.revenue } + min_index = revenues.each_with_index.min[1] + return matches[min_index] + end + end + end end From f81d6c3e5e690ceefc41522c12bbdb51a6ed481a Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Thu, 22 Oct 2015 11:11:32 -0700 Subject: [PATCH 073/107] create revenue instance variable in vendor class --- lib/far_mar/vendor.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/far_mar/vendor.rb b/lib/far_mar/vendor.rb index 2eb3cc4f..b769bad7 100644 --- a/lib/far_mar/vendor.rb +++ b/lib/far_mar/vendor.rb @@ -1,10 +1,11 @@ module FarMar class Vendor < FarMar_Base - attr_reader :id, :name, :emp_num, :market_id + attr_reader :id, :name, :emp_num, :market_id, :revenue FILENAME = './support/vendors.csv' def initialize(vendor_hash) super(vendor_hash) + @revenue ||= revenue end def self.all_objects From 43df42b9df1d6db1de4578d8e73895a70b57a684 Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Thu, 22 Oct 2015 11:12:03 -0700 Subject: [PATCH 074/107] refactor preferred and worst vendor methods --- lib/far_mar/market.rb | 22 ++-------------------- 1 file changed, 2 insertions(+), 20 deletions(-) diff --git a/lib/far_mar/market.rb b/lib/far_mar/market.rb index f72f9ad0..4ef18dc1 100644 --- a/lib/far_mar/market.rb +++ b/lib/far_mar/market.rb @@ -43,30 +43,12 @@ def products # returns the vendor with the highest revenue from a market def preferred_vendor - matches = vendors - if matches.length == 0 - return nil - elsif matches.length == 1 - return matches[0] - else - revenues = matches.map { |v| v.revenue } - max_index = revenues.each_with_index.max[1] - return matches[max_index] - end + return vendors.max_by { |vend| vend.revenue } end # returns the vendor with the lowest revenue from a market def worst_vendor - matches = vendors - if matches.length == 0 - return nil - elsif matches.length == 1 - return matches[0] - else - revenues = matches.map { |v| v.revenue } - min_index = revenues.each_with_index.min[1] - return matches[min_index] - end + return vendors.min_by { |vend| vend.revenue } end end From b75b4d52916873dfc8069e2a641436c8cae07dc6 Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Thu, 22 Oct 2015 11:40:00 -0700 Subject: [PATCH 075/107] create test for Vendor.most_revenue --- lib/far_mar/far_mar_base.rb | 4 ++-- lib/far_mar/sale.rb | 1 + spec/far_mar/vendor_spec.rb | 18 ++++++++++++++++++ 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/lib/far_mar/far_mar_base.rb b/lib/far_mar/far_mar_base.rb index dea6034f..469701c0 100644 --- a/lib/far_mar/far_mar_base.rb +++ b/lib/far_mar/far_mar_base.rb @@ -15,9 +15,9 @@ def self.all csv_file.each do |row| # Convert the array to a hash csv_hash = self.convert_to_hash(row) - # Create a product object from each row-hash in the csv file + # Create an object from each row-hash in the csv file temp = self.new(csv_hash) - # Push product object to array of products + # Push object to array of products objects.push(temp) end self.class_variable_set(:@@all_objects, objects) diff --git a/lib/far_mar/sale.rb b/lib/far_mar/sale.rb index c879782e..69d3dd88 100644 --- a/lib/far_mar/sale.rb +++ b/lib/far_mar/sale.rb @@ -5,6 +5,7 @@ class Sale < FarMar_Base def initialize(sale_hash) super(sale_hash) + # FarMar::Vendor.all_objects[@vendor_id].revenue += @amount end def self.all_objects diff --git a/spec/far_mar/vendor_spec.rb b/spec/far_mar/vendor_spec.rb index 3a37a897..9cc1f27a 100644 --- a/spec/far_mar/vendor_spec.rb +++ b/spec/far_mar/vendor_spec.rb @@ -50,6 +50,24 @@ end end + describe ".most_revenue(n)" do + it "returns an empty array when n is invalid" do + expect(FarMar::Vendor.most_revenue(-1)).to eq([]) + expect(FarMar::Vendor.most_revenue('a')).to eq([]) + expect(FarMar::Vendor.most_revenue(9999999999999999)).to eq([]) + end + it "returns the top n vendor instances ranked by total revenue" do + top_revenue = FarMar::Vendor.most_revenue(1) + top_five = FarMar::Vendor.most_revenue(5) + expect(top_revenue).to be_an_instance_of(Array) + expect(top_revenue[0].revenue).to eq(123474) + expect(top_five).to be_an_instance_of(Array) + expect(top_five.length).to eq(5) + expect(top_five[0].revenue).to eq(123474) + expect(top_five[-1].revenue).to eq(107516) + end + end + describe "#products" do it "returns a collection of Products with the vendor's ID" do products = sample_vendor.products From da37dd8bc59a34d3ff5e58f571546b2639914409 Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Thu, 22 Oct 2015 11:55:31 -0700 Subject: [PATCH 076/107] create Vendor.most_revenue method --- lib/far_mar/vendor.rb | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/far_mar/vendor.rb b/lib/far_mar/vendor.rb index b769bad7..4d3aea7d 100644 --- a/lib/far_mar/vendor.rb +++ b/lib/far_mar/vendor.rb @@ -26,8 +26,18 @@ def self.by_market(market_id) FarMar::Vendor.all_objects.find_all { |vendor| vendor.market_id == market_id } end - # returns a collection of FarMar::Product instances that are - # associated by the FarMar::Product vendor_id field + # returns the top n vendor instances ranked by total number of items sold + def self.most_revenue(n) + return [] if + n.class != Fixnum || + n < 0 || + n > FarMar::Vendor.all_objects.length + sorted = FarMar::Vendor.all_objects.sort_by { |v| v.revenue } + return sorted[-n, n].reverse + end + + # returns a collection of FarMar::Product instances that are + # associated by the FarMar::Product vendor_id field def products return FarMar::Product.by_vendor(@id) end From d6ae240d07b1aede14c399ea5ec3e267dcf3c8ae Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Thu, 22 Oct 2015 13:27:29 -0700 Subject: [PATCH 077/107] create self.all for sale class to populate @@vendor_stats --- lib/far_mar/sale.rb | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/lib/far_mar/sale.rb b/lib/far_mar/sale.rb index 69d3dd88..dfc75d7d 100644 --- a/lib/far_mar/sale.rb +++ b/lib/far_mar/sale.rb @@ -9,7 +9,40 @@ def initialize(sale_hash) end def self.all_objects - @@all_objects ||= self.all + @@all_objects ||= self.all[0] + end + + def self.vendor_stats + @@vendor_stats ||= self.all[1] + end + + def self.all + if !self.class_variable_defined?(:@@all_objects) ||!self.class_variable_defined?(:@@vendor_stats) + csv_file = CSV.read(FILENAME) + # Create empty array which will hold all the objects + objects = [] + stats = {} + csv_file.each do |row| + # Convert the array to a hash + sale_hash = convert_to_hash(row) + # Create an object from each row-hash in the csv file + temp = self.new(sale_hash) + # Push object to array of products + objects.push(temp) + # Populate the stats array + if stats[sale_hash[:vendor_id]].nil? # if there is no revenue yet + stats[sale_hash[:vendor_id]] = {} + stats[sale_hash[:vendor_id]][:revenue] = sale_hash[:amount] # store the first revenue + stats[sale_hash[:vendor_id]][:num_sales] = 1 # and tally 1 for number of sales + else + stats[sale_hash[:vendor_id]][:revenue] += sale_hash[:amount] # add the amount to the revenue + stats[sale_hash[:vendor_id]][:num_sales] += 1 # and add 1 to sale tally + end + end + return self.class_variable_set(:@@all_objects, objects), self.class_variable_set(:@@vendor_stats, stats) + + end + self.all_objects end def self.convert_to_hash(sale_array) From 617e2269a84ea48c016ae77adbb0c0d30d6dc238 Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Thu, 22 Oct 2015 14:02:25 -0700 Subject: [PATCH 078/107] refactor Vendor.revenue method --- lib/far_mar/vendor.rb | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/far_mar/vendor.rb b/lib/far_mar/vendor.rb index 4d3aea7d..30fdc21e 100644 --- a/lib/far_mar/vendor.rb +++ b/lib/far_mar/vendor.rb @@ -1,11 +1,12 @@ module FarMar class Vendor < FarMar_Base - attr_reader :id, :name, :emp_num, :market_id, :revenue + attr_reader :id, :name, :emp_num, :market_id, :num_sales FILENAME = './support/vendors.csv' def initialize(vendor_hash) + # rev = FarMar::Sale.vendor_stats[@id][:revenue] + # num = FarMar::Sale.vendor_stats[@id][:num_sales] super(vendor_hash) - @revenue ||= revenue end def self.all_objects @@ -56,8 +57,8 @@ def market # returns the sum of all of the vendor's sales (in cents) def revenue - sales_array = sales - sales_array.inject(0) { |sum, sale_obj| sum + sale_obj.amount } + rev = FarMar::Sale.vendor_stats[@id][:revenue] + rev.nil? ? (return @revenue = 0) : (return @revenue = rev) end end From 89c524f0e8e16e781116546fc7098cc912eaf764 Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Thu, 22 Oct 2015 15:08:42 -0700 Subject: [PATCH 079/107] refactor vendor.most_revenue --- lib/far_mar/vendor.rb | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/lib/far_mar/vendor.rb b/lib/far_mar/vendor.rb index 30fdc21e..676ce39b 100644 --- a/lib/far_mar/vendor.rb +++ b/lib/far_mar/vendor.rb @@ -7,6 +7,7 @@ def initialize(vendor_hash) # rev = FarMar::Sale.vendor_stats[@id][:revenue] # num = FarMar::Sale.vendor_stats[@id][:num_sales] super(vendor_hash) + @revenue = nil end def self.all_objects @@ -33,8 +34,18 @@ def self.most_revenue(n) n.class != Fixnum || n < 0 || n > FarMar::Vendor.all_objects.length - sorted = FarMar::Vendor.all_objects.sort_by { |v| v.revenue } - return sorted[-n, n].reverse + id_rev = {} + FarMar::Vendor.all_objects.each do |vendor_obj| + id_rev[vendor_obj.id] = vendor_obj.revenue + end + backwards_revs = id_rev.sort_by {|id, revenue| revenue } + sorted_revs = backwards_revs.reverse + sorted_rev_results = sorted_revs[0, n] + results = [] + sorted_rev_results.each do |item| + results.push(FarMar::Vendor.find(item[0])) + end + return results end # returns a collection of FarMar::Product instances that are @@ -57,8 +68,13 @@ def market # returns the sum of all of the vendor's sales (in cents) def revenue - rev = FarMar::Sale.vendor_stats[@id][:revenue] - rev.nil? ? (return @revenue = 0) : (return @revenue = rev) + return @revenue if !@revenue.nil? + results = FarMar::Sale.vendor_stats + if results[@id].nil? + @revenue = 0 + else + @revenue = results[@id][:revenue] + end end end From f4bc0cc51151971f4a1b86e6c1f619e89dd586f8 Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Thu, 22 Oct 2015 15:24:41 -0700 Subject: [PATCH 080/107] refactor vendor.most_revenue method --- lib/far_mar/vendor.rb | 19 ++++--------------- 1 file changed, 4 insertions(+), 15 deletions(-) diff --git a/lib/far_mar/vendor.rb b/lib/far_mar/vendor.rb index 676ce39b..83f74eab 100644 --- a/lib/far_mar/vendor.rb +++ b/lib/far_mar/vendor.rb @@ -4,10 +4,8 @@ class Vendor < FarMar_Base FILENAME = './support/vendors.csv' def initialize(vendor_hash) - # rev = FarMar::Sale.vendor_stats[@id][:revenue] - # num = FarMar::Sale.vendor_stats[@id][:num_sales] super(vendor_hash) - @revenue = nil + @revenue = revenue end def self.all_objects @@ -34,18 +32,9 @@ def self.most_revenue(n) n.class != Fixnum || n < 0 || n > FarMar::Vendor.all_objects.length - id_rev = {} - FarMar::Vendor.all_objects.each do |vendor_obj| - id_rev[vendor_obj.id] = vendor_obj.revenue - end - backwards_revs = id_rev.sort_by {|id, revenue| revenue } - sorted_revs = backwards_revs.reverse - sorted_rev_results = sorted_revs[0, n] - results = [] - sorted_rev_results.each do |item| - results.push(FarMar::Vendor.find(item[0])) - end - return results + backwards = FarMar::Vendor.all_objects.sort_by{ |vendor_obj| vendor_obj.revenue } + sorted = backwards.reverse + return sorted[0, n] end # returns a collection of FarMar::Product instances that are From c31914f365993e67ac6c07a56d8fa3163cfc459d Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Thu, 22 Oct 2015 16:21:42 -0700 Subject: [PATCH 081/107] create test for most_items method --- lib/far_mar/vendor.rb | 13 ++++++++++++- spec/far_mar/vendor_spec.rb | 18 ++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/lib/far_mar/vendor.rb b/lib/far_mar/vendor.rb index 83f74eab..926204f2 100644 --- a/lib/far_mar/vendor.rb +++ b/lib/far_mar/vendor.rb @@ -1,11 +1,12 @@ module FarMar class Vendor < FarMar_Base - attr_reader :id, :name, :emp_num, :market_id, :num_sales + attr_reader :id, :name, :emp_num, :market_id FILENAME = './support/vendors.csv' def initialize(vendor_hash) super(vendor_hash) @revenue = revenue + @num_sales = num_sales end def self.all_objects @@ -66,5 +67,15 @@ def revenue end end + def num_sales + return @num_sales if !@num_sales.nil? + results = FarMar::Sale.vendor_stats + if results[@id].nil? + @num_sales = 0 + else + @num_sales = results[@id][:num_sales] + end + end + end end diff --git a/spec/far_mar/vendor_spec.rb b/spec/far_mar/vendor_spec.rb index 9cc1f27a..3a74aeab 100644 --- a/spec/far_mar/vendor_spec.rb +++ b/spec/far_mar/vendor_spec.rb @@ -68,6 +68,24 @@ end end + describe ".most_items(n)" do + it "returns an empty array when n is invalid" do + expect(FarMar::Vendor.most_items(-1)).to eq([]) + expect(FarMar::Vendor.most_items('a')).to eq([]) + expect(FarMar::Vendor.most_items(9999999999999999)).to eq([]) + end + it "returns the top n vendor instances ranked by number of items sold" do + top_item = FarMar::Vendor.most_items(1) + top_thirty = FarMar::Vendor.most_items(30) + expect(top_item).to be_an_instance_of(Array) + expect(top_item.length).to eq(1) + expect(top_item.num_sales).to eq(18) + expect(top_thirty).to be_an_instance_of(Array) + expect(top_thirty.length).to eq(30) + expect(top_thirty[-1].num_sales).to eq(17) + end + end + describe "#products" do it "returns a collection of Products with the vendor's ID" do products = sample_vendor.products From b6b94332d516686dca7ad232455730ab0e4f48f1 Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Thu, 22 Oct 2015 16:42:13 -0700 Subject: [PATCH 082/107] create vendor.most_items method --- lib/far_mar/vendor.rb | 13 ++++++++++++- spec/far_mar/vendor_spec.rb | 2 +- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/far_mar/vendor.rb b/lib/far_mar/vendor.rb index 926204f2..219bd419 100644 --- a/lib/far_mar/vendor.rb +++ b/lib/far_mar/vendor.rb @@ -33,7 +33,18 @@ def self.most_revenue(n) n.class != Fixnum || n < 0 || n > FarMar::Vendor.all_objects.length - backwards = FarMar::Vendor.all_objects.sort_by{ |vendor_obj| vendor_obj.revenue } + backwards = FarMar::Vendor.all_objects.sort_by { |vendor_obj| vendor_obj.revenue } + sorted = backwards.reverse + return sorted[0, n] + end + + # returns the top n vendor instances ranked by total number of items sold + def self.most_items(n) + return [] if + n.class != Fixnum || + n < 0 || + n > FarMar::Vendor.all_objects.length + backwards = FarMar::Vendor.all_objects.sort_by { |vendor_obj| vendor_obj.num_sales } sorted = backwards.reverse return sorted[0, n] end diff --git a/spec/far_mar/vendor_spec.rb b/spec/far_mar/vendor_spec.rb index 3a74aeab..3ebc0d37 100644 --- a/spec/far_mar/vendor_spec.rb +++ b/spec/far_mar/vendor_spec.rb @@ -79,7 +79,7 @@ top_thirty = FarMar::Vendor.most_items(30) expect(top_item).to be_an_instance_of(Array) expect(top_item.length).to eq(1) - expect(top_item.num_sales).to eq(18) + expect(top_item[0].num_sales).to eq(18) expect(top_thirty).to be_an_instance_of(Array) expect(top_thirty.length).to eq(30) expect(top_thirty[-1].num_sales).to eq(17) From 09d17cc38b1597e2226ef88f596a8b83f9401d3f Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Thu, 22 Oct 2015 16:48:39 -0700 Subject: [PATCH 083/107] refactor vendor most methods to use helper method --- lib/far_mar/vendor.rb | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/lib/far_mar/vendor.rb b/lib/far_mar/vendor.rb index 219bd419..822cf310 100644 --- a/lib/far_mar/vendor.rb +++ b/lib/far_mar/vendor.rb @@ -27,26 +27,25 @@ def self.by_market(market_id) FarMar::Vendor.all_objects.find_all { |vendor| vendor.market_id == market_id } end - # returns the top n vendor instances ranked by total number of items sold - def self.most_revenue(n) + # helper method for the most methods + def self.most_x(n, attribute) return [] if n.class != Fixnum || n < 0 || n > FarMar::Vendor.all_objects.length - backwards = FarMar::Vendor.all_objects.sort_by { |vendor_obj| vendor_obj.revenue } + backwards = FarMar::Vendor.all_objects.sort_by { |vendor_obj| vendor_obj.instance_variable_get(attribute) } sorted = backwards.reverse return sorted[0, n] end + # returns the top n vendor instances ranked by total number of items sold + def self.most_revenue(n) + FarMar::Vendor.most_x(n, "@revenue") + end + # returns the top n vendor instances ranked by total number of items sold def self.most_items(n) - return [] if - n.class != Fixnum || - n < 0 || - n > FarMar::Vendor.all_objects.length - backwards = FarMar::Vendor.all_objects.sort_by { |vendor_obj| vendor_obj.num_sales } - sorted = backwards.reverse - return sorted[0, n] + FarMar::Vendor.most_x(n, "@num_sales") end # returns a collection of FarMar::Product instances that are From 56da2b21a01a8f93d7023dbc00c3bde420239b12 Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Thu, 22 Oct 2015 21:40:15 -0700 Subject: [PATCH 084/107] create tests for vendor.day_revenue --- lib/far_mar/vendor.rb | 9 +++++++++ spec/far_mar/vendor_spec.rb | 17 +++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/lib/far_mar/vendor.rb b/lib/far_mar/vendor.rb index 822cf310..1ddde746 100644 --- a/lib/far_mar/vendor.rb +++ b/lib/far_mar/vendor.rb @@ -87,5 +87,14 @@ def num_sales end end + # returns the vendor's revenue for a given date + def self.revenue(date) + end + + # returns the total revenue for the specific date and vendor instance + def day_revenue(date) + + end + end end diff --git a/spec/far_mar/vendor_spec.rb b/spec/far_mar/vendor_spec.rb index 3ebc0d37..7fc01cac 100644 --- a/spec/far_mar/vendor_spec.rb +++ b/spec/far_mar/vendor_spec.rb @@ -120,5 +120,22 @@ expect(sample_vendor.revenue).to eq(17211) end end + + describe "#day_revenue(date)" do + it "returns 0 for invalid date" do + expect(sample_vendor.day_revenue(123)).to eq(0) + expect(sample_vendor.day_revenue('abc')).to eq(0) + end + it "returns the total revenue for the vendor on the given date" do + date1 = DateTime.parse("2013-11-12T13:07:19-08:00") + date2 = DateTime.parse("2013-11-10T15:40:57-08:00") + date3 = DateTime.parse("2013-11-08T07:41:09-08:00") + date4 = DateTime.parse("2013-11-09T07:41:09-08:00") + expect(sample_vendor.day_revenue(date1)).to eq(1135) + expect(sample_vendor.day_revenue(date2)).to eq(7225) + expect(sample_vendor.day_revenue(date3)).to eq(4733) + expect(sample_vendor.day_revenue(date4)).to eq(0) + end + end end end From 8786e429e089ba8339ad3b1b30545a80a18dea23 Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Thu, 22 Oct 2015 21:47:18 -0700 Subject: [PATCH 085/107] create vendor.day_revenue method --- lib/far_mar/vendor.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/far_mar/vendor.rb b/lib/far_mar/vendor.rb index 1ddde746..0b2d79fb 100644 --- a/lib/far_mar/vendor.rb +++ b/lib/far_mar/vendor.rb @@ -93,7 +93,10 @@ def self.revenue(date) # returns the total revenue for the specific date and vendor instance def day_revenue(date) - + return 0 if date.class != DateTime + all_sales = FarMar::Sale.by_vendor(@id) + day_sales = all_sales.select { |sale| sale.purchase_time.to_date == date.to_date } + return day_sales.inject(0) { |sum, sale_obj| sum + sale_obj.amount } end end From dc56cf200a730e87653b9a65aac769efde7e5779 Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Thu, 22 Oct 2015 22:02:30 -0700 Subject: [PATCH 086/107] create test for product.revenue --- lib/far_mar/sale.rb | 32 ++++++++++++++++++++++---------- spec/far_mar/product_spec.rb | 6 ++++++ 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/lib/far_mar/sale.rb b/lib/far_mar/sale.rb index dfc75d7d..0edea328 100644 --- a/lib/far_mar/sale.rb +++ b/lib/far_mar/sale.rb @@ -16,12 +16,17 @@ def self.vendor_stats @@vendor_stats ||= self.all[1] end + def self.product_stats + @@product_stats ||= self.all[2] + end + def self.all if !self.class_variable_defined?(:@@all_objects) ||!self.class_variable_defined?(:@@vendor_stats) csv_file = CSV.read(FILENAME) # Create empty array which will hold all the objects objects = [] - stats = {} + vend_stats = {} + prod_stats = {} csv_file.each do |row| # Convert the array to a hash sale_hash = convert_to_hash(row) @@ -29,17 +34,24 @@ def self.all temp = self.new(sale_hash) # Push object to array of products objects.push(temp) - # Populate the stats array - if stats[sale_hash[:vendor_id]].nil? # if there is no revenue yet - stats[sale_hash[:vendor_id]] = {} - stats[sale_hash[:vendor_id]][:revenue] = sale_hash[:amount] # store the first revenue - stats[sale_hash[:vendor_id]][:num_sales] = 1 # and tally 1 for number of sales + # Populate the vend_stats array + if vend_stats[sale_hash[:vendor_id]].nil? # if there is no revenue yet + vend_stats[sale_hash[:vendor_id]] = {} + vend_stats[sale_hash[:vendor_id]][:revenue] = sale_hash[:amount] # store the first revenue + vend_stats[sale_hash[:vendor_id]][:num_sales] = 1 # and tally 1 for number of sales + else + vend_stats[sale_hash[:vendor_id]][:revenue] += sale_hash[:amount] # add the amount to the revenue + vend_stats[sale_hash[:vendor_id]][:num_sales] += 1 # and add 1 to sale tally + end + # Populate product_stats array + if prod_stats[sale_hash[:product_id]].nil? # if there is no product revenue yet + prod_stats[sale_hash[:product_id]] = {} + prod_stats[sale_hash[:product_id]][:revenue] = sale_hash[:amount] # stores the first revenue else - stats[sale_hash[:vendor_id]][:revenue] += sale_hash[:amount] # add the amount to the revenue - stats[sale_hash[:vendor_id]][:num_sales] += 1 # and add 1 to sale tally + prod_stats[sale_hash[:product_id]][:revenue] += sale_hash[:amount] # adds the amount to the revenue end end - return self.class_variable_set(:@@all_objects, objects), self.class_variable_set(:@@vendor_stats, stats) + return self.class_variable_set(:@@all_objects, objects), self.class_variable_set(:@@vendor_stats, vend_stats), self.class_variable_set(:@@product_stats, prod_stats) end self.all_objects @@ -60,7 +72,7 @@ def self.by_product(product_id) return FarMar::Sale.all_objects.find_all { |sale| sale.product_id == product_id } end - # returns all of the vendors with the given vendor id + # returns all of the sales with the given vendor id def self.by_vendor(vendor_id) return FarMar::Sale.all_objects.find_all { |sale| sale.vendor_id == vendor_id } end diff --git a/spec/far_mar/product_spec.rb b/spec/far_mar/product_spec.rb index 1d9f58eb..a1f8c75b 100644 --- a/spec/far_mar/product_spec.rb +++ b/spec/far_mar/product_spec.rb @@ -51,6 +51,12 @@ end end + describe ".revenue" do + it "returns the revenue for a given product" do + expect(sample_product.revenue).to eq(39719) + end + end + describe "#sales" do it "returns array of Sales that match the product id" do sales = sample_product.sales From 1a3034101e2c9eb6ca3b0dd5515f75e423221fc5 Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Thu, 22 Oct 2015 22:05:29 -0700 Subject: [PATCH 087/107] create revenue method for product class --- lib/far_mar/product.rb | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/lib/far_mar/product.rb b/lib/far_mar/product.rb index 2fc32b4c..894d3492 100644 --- a/lib/far_mar/product.rb +++ b/lib/far_mar/product.rb @@ -5,6 +5,7 @@ class Product < FarMar_Base def initialize(product_hash) super(product_hash) + @revenue = revenue end def self.all_objects @@ -41,5 +42,17 @@ def sales def number_of_sales return sales.length end + + # returns the total revenue for a given product + def revenue + return @revenue if !@revenue.nil? + results = FarMar::Sale.product_stats + if results[@id].nil? + @revenue = 0 + else + @revenue = results[@id][:revenue] + end + end + end end From 7eba91827bfa51ebde55c8106b6771660d20c81b Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Thu, 22 Oct 2015 22:17:31 -0700 Subject: [PATCH 088/107] refactor revenue method into vendor product base class --- lib/far_mar.rb | 1 + lib/far_mar/product.rb | 10 ++-------- lib/far_mar/vend_prod_base.rb | 13 +++++++++++++ lib/far_mar/vendor.rb | 10 ++-------- 4 files changed, 18 insertions(+), 16 deletions(-) create mode 100644 lib/far_mar/vend_prod_base.rb diff --git a/lib/far_mar.rb b/lib/far_mar.rb index 587f4257..7b177d7d 100644 --- a/lib/far_mar.rb +++ b/lib/far_mar.rb @@ -4,6 +4,7 @@ # Order matters here so I might have to change it require './lib/far_mar/far_mar_base' +require './lib/far_mar/vend_prod_base' require './lib/far_mar/market' require './lib/far_mar/vendor' require './lib/far_mar/product' diff --git a/lib/far_mar/product.rb b/lib/far_mar/product.rb index 894d3492..8f46b597 100644 --- a/lib/far_mar/product.rb +++ b/lib/far_mar/product.rb @@ -1,5 +1,5 @@ module FarMar - class Product < FarMar_Base + class Product < VendorProductBase attr_reader :id, :name, :vendor_id FILENAME = './support/products.csv' @@ -45,13 +45,7 @@ def number_of_sales # returns the total revenue for a given product def revenue - return @revenue if !@revenue.nil? - results = FarMar::Sale.product_stats - if results[@id].nil? - @revenue = 0 - else - @revenue = results[@id][:revenue] - end + super(:@@product_stats) end end diff --git a/lib/far_mar/vend_prod_base.rb b/lib/far_mar/vend_prod_base.rb new file mode 100644 index 00000000..1b265b6d --- /dev/null +++ b/lib/far_mar/vend_prod_base.rb @@ -0,0 +1,13 @@ +module FarMar + class VendorProductBase < FarMar_Base + def revenue(attribute) + return @revenue if !@revenue.nil? + results = FarMar::Sale.class_variable_get(attribute) + if results[@id].nil? + @revenue = 0 + else + @revenue = results[@id][:revenue] + end + end + end +end diff --git a/lib/far_mar/vendor.rb b/lib/far_mar/vendor.rb index 0b2d79fb..6a4b8b96 100644 --- a/lib/far_mar/vendor.rb +++ b/lib/far_mar/vendor.rb @@ -1,5 +1,5 @@ module FarMar - class Vendor < FarMar_Base + class Vendor < VendorProductBase attr_reader :id, :name, :emp_num, :market_id FILENAME = './support/vendors.csv' @@ -68,13 +68,7 @@ def market # returns the sum of all of the vendor's sales (in cents) def revenue - return @revenue if !@revenue.nil? - results = FarMar::Sale.vendor_stats - if results[@id].nil? - @revenue = 0 - else - @revenue = results[@id][:revenue] - end + super(:@@vendor_stats) end def num_sales From 05a6bdf4ac43a603e44b4c2aedc627891fa1a988 Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Thu, 22 Oct 2015 22:21:14 -0700 Subject: [PATCH 089/107] refactor most_x method to be in vend_prod_base class --- lib/far_mar/vend_prod_base.rb | 12 ++++++++++++ lib/far_mar/vendor.rb | 15 ++------------- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/lib/far_mar/vend_prod_base.rb b/lib/far_mar/vend_prod_base.rb index 1b265b6d..0207e93a 100644 --- a/lib/far_mar/vend_prod_base.rb +++ b/lib/far_mar/vend_prod_base.rb @@ -1,5 +1,17 @@ module FarMar class VendorProductBase < FarMar_Base + + # helper method for the most methods in product and vendor classes + def self.most_x(n, attribute) + return [] if + n.class != Fixnum || + n < 0 || + n > FarMar::Vendor.all_objects.length + backwards = FarMar::Vendor.all_objects.sort_by { |vendor_obj| vendor_obj.instance_variable_get(attribute) } + sorted = backwards.reverse + return sorted[0, n] + end + def revenue(attribute) return @revenue if !@revenue.nil? results = FarMar::Sale.class_variable_get(attribute) diff --git a/lib/far_mar/vendor.rb b/lib/far_mar/vendor.rb index 6a4b8b96..9092e800 100644 --- a/lib/far_mar/vendor.rb +++ b/lib/far_mar/vendor.rb @@ -27,25 +27,14 @@ def self.by_market(market_id) FarMar::Vendor.all_objects.find_all { |vendor| vendor.market_id == market_id } end - # helper method for the most methods - def self.most_x(n, attribute) - return [] if - n.class != Fixnum || - n < 0 || - n > FarMar::Vendor.all_objects.length - backwards = FarMar::Vendor.all_objects.sort_by { |vendor_obj| vendor_obj.instance_variable_get(attribute) } - sorted = backwards.reverse - return sorted[0, n] - end - # returns the top n vendor instances ranked by total number of items sold def self.most_revenue(n) - FarMar::Vendor.most_x(n, "@revenue") + FarMar::VendorProductBase.most_x(n, "@revenue") end # returns the top n vendor instances ranked by total number of items sold def self.most_items(n) - FarMar::Vendor.most_x(n, "@num_sales") + FarMar::VendorProductBase.most_x(n, "@num_sales") end # returns a collection of FarMar::Product instances that are From ad3532658af7712fa243d90150fc8315561dcb73 Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Fri, 23 Oct 2015 08:50:21 -0700 Subject: [PATCH 090/107] create tests for vendor.revenue(date) --- spec/far_mar/vendor_spec.rb | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/spec/far_mar/vendor_spec.rb b/spec/far_mar/vendor_spec.rb index 7fc01cac..2d85019e 100644 --- a/spec/far_mar/vendor_spec.rb +++ b/spec/far_mar/vendor_spec.rb @@ -137,5 +137,24 @@ expect(sample_vendor.day_revenue(date4)).to eq(0) end end + + describe ".revenue(date)" do + it "returns 0 for invalid dates" do + expect(FarMar::Vendor.revenue(123)).to eq(0) + expect(FarMar::Vendor.revenue('abc')).to eq(0) + end + it "returns the total revenue for the date across all vendors" do + date1 = DateTime.parse("2013-11-12T13:07:19-08:00") + date2 = DateTime.parse("2013-11-10T15:40:57-08:00") + match1 = FarMar::Vendor.revenue(date1) + match2 = FarMar::Vendor.revenue(date2) + # date3 = DateTime.parse("2013-11-08T07:41:09-08:00") + # date4 = DateTime.parse("2013-11-09T07:41:09-08:00") + expect(match1).to be_an_instance_of(Array) + expect(match1).to eq(8871052) + expect(match2).to be_an_instance_of(Array) + expect(match2).to eq(9119618) + end + end end end From d17b2b19904ce6991bb284b82a9120221424ac72 Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Fri, 23 Oct 2015 08:57:11 -0700 Subject: [PATCH 091/107] create self.revenue_on(date) in vendor class --- lib/far_mar/vendor.rb | 8 ++++++-- spec/far_mar/vendor_spec.rb | 14 +++++++------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/lib/far_mar/vendor.rb b/lib/far_mar/vendor.rb index 9092e800..dbb96149 100644 --- a/lib/far_mar/vendor.rb +++ b/lib/far_mar/vendor.rb @@ -70,8 +70,12 @@ def num_sales end end - # returns the vendor's revenue for a given date - def self.revenue(date) + # returns the total revenue across all vendors for a given date + def self.revenue_on(date) + return 0 if date.class != DateTime + match = FarMar::Sale.all_objects.select { |sale| sale.purchase_time.to_date == date.to_date } + rev = match.inject(0) { |sum, sale| sum + sale.amount } + return rev end # returns the total revenue for the specific date and vendor instance diff --git a/spec/far_mar/vendor_spec.rb b/spec/far_mar/vendor_spec.rb index 2d85019e..dd075010 100644 --- a/spec/far_mar/vendor_spec.rb +++ b/spec/far_mar/vendor_spec.rb @@ -138,21 +138,21 @@ end end - describe ".revenue(date)" do + describe ".revenue_on(date)" do it "returns 0 for invalid dates" do - expect(FarMar::Vendor.revenue(123)).to eq(0) - expect(FarMar::Vendor.revenue('abc')).to eq(0) + expect(FarMar::Vendor.revenue_on(123)).to eq(0) + expect(FarMar::Vendor.revenue_on('abc')).to eq(0) end it "returns the total revenue for the date across all vendors" do date1 = DateTime.parse("2013-11-12T13:07:19-08:00") date2 = DateTime.parse("2013-11-10T15:40:57-08:00") - match1 = FarMar::Vendor.revenue(date1) - match2 = FarMar::Vendor.revenue(date2) + match1 = FarMar::Vendor.revenue_on(date1) + match2 = FarMar::Vendor.revenue_on(date2) # date3 = DateTime.parse("2013-11-08T07:41:09-08:00") # date4 = DateTime.parse("2013-11-09T07:41:09-08:00") - expect(match1).to be_an_instance_of(Array) + expect(match1).to be_an_instance_of(Fixnum) expect(match1).to eq(8871052) - expect(match2).to be_an_instance_of(Array) + expect(match2).to be_an_instance_of(Fixnum) expect(match2).to eq(9119618) end end From dc0b03d2271954bb80adfc14a8d55d603956b844 Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Fri, 23 Oct 2015 09:07:40 -0700 Subject: [PATCH 092/107] create test for most_revenue(n) method --- spec/far_mar/product_spec.rb | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/spec/far_mar/product_spec.rb b/spec/far_mar/product_spec.rb index a1f8c75b..7306afea 100644 --- a/spec/far_mar/product_spec.rb +++ b/spec/far_mar/product_spec.rb @@ -57,6 +57,24 @@ end end + describe ".most_revenue(n)" do + it "returns an empty array when n is invalid" do + expect(FarMar::Product.most_revenue(-3)).to eq([]) + expect(FarMar::Product.most_revenue('abc')).to eq([]) + expect(FarMar::Product.most_revenue(9999999999999)).to eq([]) + end + it "returns the top n products ranked by total revenue" do + top_revenue = FarMar::Product.most_revenue(1) + top_five = FarMar::Product.most_revenue(5) + expect(top_revenue).to be_an_instance_of(Array) + expect(top_revenue.length).to eq(1) + expect(top_revenue[0].revenue).to eq(118516) + expect(top_five).to be_an_instance_of(Array) + expect(top_five.length).to eq(5) + expect(top_revenue[-1].revenue).to eq(99098) + end + end + describe "#sales" do it "returns array of Sales that match the product id" do sales = sample_product.sales From 177afd69562e5d5ed14e8e004a3e28f31e34b7ce Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Fri, 23 Oct 2015 09:44:36 -0700 Subject: [PATCH 093/107] refactor VendorProductBase to be a module instead of class --- lib/far_mar/vend_prod_base.rb | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/lib/far_mar/vend_prod_base.rb b/lib/far_mar/vend_prod_base.rb index 0207e93a..5a6b9657 100644 --- a/lib/far_mar/vend_prod_base.rb +++ b/lib/far_mar/vend_prod_base.rb @@ -1,20 +1,26 @@ module FarMar - class VendorProductBase < FarMar_Base + module VendorProductBase + + def self.included(base) + base.extend(ClassMethods) + end # helper method for the most methods in product and vendor classes - def self.most_x(n, attribute) - return [] if - n.class != Fixnum || - n < 0 || - n > FarMar::Vendor.all_objects.length - backwards = FarMar::Vendor.all_objects.sort_by { |vendor_obj| vendor_obj.instance_variable_get(attribute) } - sorted = backwards.reverse - return sorted[0, n] + module ClassMethods + def most_x(n, attribute) + return [] if + n.class != Fixnum || + n < 0 || + n > self.all_objects.length + backwards = self.all_objects.sort_by { |obj| obj.instance_variable_get(attribute) } + sorted = backwards.reverse + return sorted[0, n] + end end - def revenue(attribute) + def revenue return @revenue if !@revenue.nil? - results = FarMar::Sale.class_variable_get(attribute) + results = FarMar::Sale.class_variable_get(self.class::REV_ATTR) if results[@id].nil? @revenue = 0 else From 65eee101e3822e4bec448c495f8ec2d3575626dc Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Fri, 23 Oct 2015 09:45:11 -0700 Subject: [PATCH 094/107] bugfix in most_revenue test --- spec/far_mar/product_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/far_mar/product_spec.rb b/spec/far_mar/product_spec.rb index 7306afea..e5c5b19c 100644 --- a/spec/far_mar/product_spec.rb +++ b/spec/far_mar/product_spec.rb @@ -71,7 +71,7 @@ expect(top_revenue[0].revenue).to eq(118516) expect(top_five).to be_an_instance_of(Array) expect(top_five.length).to eq(5) - expect(top_revenue[-1].revenue).to eq(99098) + expect(top_five[-1].revenue).to eq(99098) end end From 49825b1aae83902fcb3cb20c1c3f255c25e86339 Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Fri, 23 Oct 2015 09:46:33 -0700 Subject: [PATCH 095/107] refactor to use methods from VendorProductBase module --- lib/far_mar/product.rb | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/lib/far_mar/product.rb b/lib/far_mar/product.rb index 8f46b597..771234c1 100644 --- a/lib/far_mar/product.rb +++ b/lib/far_mar/product.rb @@ -1,7 +1,10 @@ module FarMar - class Product < VendorProductBase + class Product < FarMar_Base attr_reader :id, :name, :vendor_id FILENAME = './support/products.csv' + REV_ATTR = :@@product_stats + + include VendorProductBase def initialize(product_hash) super(product_hash) @@ -26,6 +29,12 @@ def self.by_vendor(vendor_id) FarMar::Product.all_objects.find_all { |product| product.vendor_id == vendor_id } end + # returns the top n products instances ranked by total revenue + def self.most_revenue(n) + most_x(n, "@revenue") + # FarMar::VendorProductBase.class_method(:most_x).bind(self).call(n, "@revenue") + end + # returns the FarMar::Vendor instance that is associated with this # vendor using the vendor_id field def vendor @@ -43,10 +52,10 @@ def number_of_sales return sales.length end - # returns the total revenue for a given product - def revenue - super(:@@product_stats) - end + # # returns the total revenue for a given product + # def revenue + # super(:@@product_stats) + # end end end From 207079caf6b310b9a75bb8dc305f850f2c13cef7 Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Fri, 23 Oct 2015 09:46:54 -0700 Subject: [PATCH 096/107] refactor vendor class to use methods from VendorProductBase module --- lib/far_mar/vendor.rb | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/lib/far_mar/vendor.rb b/lib/far_mar/vendor.rb index dbb96149..7643c66d 100644 --- a/lib/far_mar/vendor.rb +++ b/lib/far_mar/vendor.rb @@ -1,7 +1,10 @@ module FarMar - class Vendor < VendorProductBase + class Vendor < FarMar_Base attr_reader :id, :name, :emp_num, :market_id FILENAME = './support/vendors.csv' + REV_ATTR = :@@vendor_stats + + include VendorProductBase def initialize(vendor_hash) super(vendor_hash) @@ -29,12 +32,12 @@ def self.by_market(market_id) # returns the top n vendor instances ranked by total number of items sold def self.most_revenue(n) - FarMar::VendorProductBase.most_x(n, "@revenue") + most_x(n, "@revenue") end # returns the top n vendor instances ranked by total number of items sold def self.most_items(n) - FarMar::VendorProductBase.most_x(n, "@num_sales") + most_x(n, "@num_sales") end # returns a collection of FarMar::Product instances that are @@ -55,10 +58,10 @@ def market return FarMar::Market.find(@market_id) end - # returns the sum of all of the vendor's sales (in cents) - def revenue - super(:@@vendor_stats) - end + # # returns the sum of all of the vendor's sales (in cents) + # def revenue + # super(:@@vendor_stats) + # end def num_sales return @num_sales if !@num_sales.nil? From 6e68f8d9ae231848751492319284b7fabf52f05a Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Fri, 23 Oct 2015 09:58:15 -0700 Subject: [PATCH 097/107] add comments to better understand all the refactoring --- lib/far_mar/product.rb | 10 +++------- lib/far_mar/vend_prod_base.rb | 7 +++++-- lib/far_mar/vendor.rb | 9 +++------ 3 files changed, 11 insertions(+), 15 deletions(-) diff --git a/lib/far_mar/product.rb b/lib/far_mar/product.rb index 771234c1..c95edec7 100644 --- a/lib/far_mar/product.rb +++ b/lib/far_mar/product.rb @@ -2,8 +2,10 @@ module FarMar class Product < FarMar_Base attr_reader :id, :name, :vendor_id FILENAME = './support/products.csv' - REV_ATTR = :@@product_stats + REV_ATTR = :@@product_stats # the attribute accessed by the revenue method + # include adds methods to the instance of a class + # in this case, revenue is an instance method that is included include VendorProductBase def initialize(product_hash) @@ -51,11 +53,5 @@ def sales def number_of_sales return sales.length end - - # # returns the total revenue for a given product - # def revenue - # super(:@@product_stats) - # end - end end diff --git a/lib/far_mar/vend_prod_base.rb b/lib/far_mar/vend_prod_base.rb index 5a6b9657..a02e8660 100644 --- a/lib/far_mar/vend_prod_base.rb +++ b/lib/far_mar/vend_prod_base.rb @@ -1,12 +1,15 @@ module FarMar module VendorProductBase - + # info from http://www.railstips.org/blog/archives/2009/05/15/include-vs-extend-in-ruby/ def self.included(base) + # extend is for adding class methods base.extend(ClassMethods) end - # helper method for the most methods in product and vendor classes module ClassMethods + # these are class methods for the VendorProductBase module + # written this way so that "self" refers to the class that is calling it + # self is either FarMar::Vendor or FarMar::Product def most_x(n, attribute) return [] if n.class != Fixnum || diff --git a/lib/far_mar/vendor.rb b/lib/far_mar/vendor.rb index 7643c66d..4a138101 100644 --- a/lib/far_mar/vendor.rb +++ b/lib/far_mar/vendor.rb @@ -2,8 +2,10 @@ module FarMar class Vendor < FarMar_Base attr_reader :id, :name, :emp_num, :market_id FILENAME = './support/vendors.csv' - REV_ATTR = :@@vendor_stats + REV_ATTR = :@@vendor_stats # the attribute accessed by the revenue method + # include adds methods to the instance of a class + # in this case, revenue is an instance method that is included include VendorProductBase def initialize(vendor_hash) @@ -58,11 +60,6 @@ def market return FarMar::Market.find(@market_id) end - # # returns the sum of all of the vendor's sales (in cents) - # def revenue - # super(:@@vendor_stats) - # end - def num_sales return @num_sales if !@num_sales.nil? results = FarMar::Sale.vendor_stats From 79ad1aad93b3881ca22a5b6b02accd3717829e5c Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Fri, 23 Oct 2015 10:14:05 -0700 Subject: [PATCH 098/107] randomize running of rspec tests --- spec/spec_helper.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index f7dc6dfb..3ac4c70e 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,4 +1,8 @@ require 'simplecov' SimpleCov.start +RSpec.configure do |config| + config.order = 'random' +end + require './lib/far_mar' From a8762fe1bb6a6208a19f345bbde58f98cf2f466a Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Fri, 23 Oct 2015 10:28:05 -0700 Subject: [PATCH 099/107] create beginnings of test for preferred and worst vendor --- spec/far_mar/market_spec.rb | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/spec/far_mar/market_spec.rb b/spec/far_mar/market_spec.rb index 345e98c9..7c3971c6 100644 --- a/spec/far_mar/market_spec.rb +++ b/spec/far_mar/market_spec.rb @@ -68,6 +68,7 @@ end end + # Returns the vendor with the highest revenue describe "#preferred_vendor" do it "returns the market's vendor with the highest revenue" do max_vendor1 = sample_market.preferred_vendor @@ -80,6 +81,7 @@ end end + # Returns the vendor with the lowest revenue describe "#worst_vendor" do it "returns the market's vendor with the lowest revenue" do min_vendor1 = sample_market.worst_vendor @@ -91,5 +93,37 @@ expect(min_vendor2.revenue).to eq(1810) end end + + # Returns the vendor with the highest revenue for the given date + describe "#preferred_vendor(date)" do + it "returns nil for an invalid date" do + expect(sample_market.preferred_vendor(123)).to be_nil + expect(sample_market.preferred_vendor('abc')).to be_nil + end + it "returns the vendor with the highest revenue for the given date" do + date = DateTime.parse("2013-11-12T13:07:19-08:00") + preferred = sample_market.preferred_vendor(date) + expect(preferred).to be_an_instance_of(FarMar::Vendor) + # TODO : Figure out expected + expect(preferred.revenue).to eq() + end + end + + # Returns the vendor with the lowest revenue for the given date + describe "#worst_vendor(date)" do + it "returns nil for an invalid date" do + expect(sample_market.worst_vendor(123)).to be_nil + expect(sample_market.worst_vendor('abc')).to be_nil + end + it "returns the vendor with the highest revenue for the given date" do + date = DateTime.parse("2013-11-12T13:07:19-08:00") + worst = sample_market.worst_vendor(date) + expect(worst).to be_an_instance_of(FarMar::Vendor) + # TODO : Figure out expected + expect(worst.revenue).to eq() + + end + end + end end From 39f5d0a4a259b754201a52fa60de85207a355242 Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Fri, 23 Oct 2015 14:06:57 -0700 Subject: [PATCH 100/107] refactor market.prefered_vendor to have nil default date --- lib/far_mar/market.rb | 26 +++++++++++++++++++++++--- spec/far_mar/market_spec.rb | 18 ++++++++---------- 2 files changed, 31 insertions(+), 13 deletions(-) diff --git a/lib/far_mar/market.rb b/lib/far_mar/market.rb index 4ef18dc1..a8f83cc8 100644 --- a/lib/far_mar/market.rb +++ b/lib/far_mar/market.rb @@ -40,10 +40,20 @@ def products end return products end + # + # # returns the vendor with the highest revenue from a market + # def preferred_vendor + # + # end - # returns the vendor with the highest revenue from a market - def preferred_vendor - return vendors.max_by { |vend| vend.revenue } + # returns the vendor with the highest revenue for the given date + def preferred_vendor(date = nil) + return vendors.max_by { |vend| vend.revenue } if date.nil? + return nil if date.class != DateTime + m_vendors = vendors + rev = m_vendors.map { |vend| vend.day_revenue(date) } + max_index = rev.each_with_index.max[1] + return m_vendors[max_index] end # returns the vendor with the lowest revenue from a market @@ -51,5 +61,15 @@ def worst_vendor return vendors.min_by { |vend| vend.revenue } end + #returns the vendor with the lowest revenue on the given date + def worst_vendor_on(date) + return nil if date.class != DateTime + return nil if date.class != DateTime + m_vendors = vendors + rev = m_vendors.map { |vend| vend.day_revenue(date) } + min_index = rev.each_with_index.min[1] + return m_vendors[min_index] + end + end end diff --git a/spec/far_mar/market_spec.rb b/spec/far_mar/market_spec.rb index 7c3971c6..ecff05ec 100644 --- a/spec/far_mar/market_spec.rb +++ b/spec/far_mar/market_spec.rb @@ -102,26 +102,24 @@ end it "returns the vendor with the highest revenue for the given date" do date = DateTime.parse("2013-11-12T13:07:19-08:00") - preferred = sample_market.preferred_vendor(date) + preferred = sample_market2.preferred_vendor(date) expect(preferred).to be_an_instance_of(FarMar::Vendor) - # TODO : Figure out expected - expect(preferred.revenue).to eq() + expect(preferred.id).to eq(104) end end # Returns the vendor with the lowest revenue for the given date - describe "#worst_vendor(date)" do + describe "#worst_vendor_on(date)" do it "returns nil for an invalid date" do - expect(sample_market.worst_vendor(123)).to be_nil - expect(sample_market.worst_vendor('abc')).to be_nil + expect(sample_market.worst_vendor_on(123)).to be_nil + expect(sample_market.worst_vendor_on('abc')).to be_nil end it "returns the vendor with the highest revenue for the given date" do date = DateTime.parse("2013-11-12T13:07:19-08:00") - worst = sample_market.worst_vendor(date) + worst = sample_market2.worst_vendor_on(date) expect(worst).to be_an_instance_of(FarMar::Vendor) - # TODO : Figure out expected - expect(worst.revenue).to eq() - + # NOTE: many different possible with 0: 102, 105, 107 + expect(worst.id).to eq(102) end end From 4355c9b16fa3c228fd4046c27a59d01d96465a18 Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Fri, 23 Oct 2015 14:10:44 -0700 Subject: [PATCH 101/107] refactor worst_vendor method to have date as nil default --- lib/far_mar/market.rb | 18 +++++------------- spec/far_mar/market_spec.rb | 10 +++++----- 2 files changed, 10 insertions(+), 18 deletions(-) diff --git a/lib/far_mar/market.rb b/lib/far_mar/market.rb index a8f83cc8..e0603abf 100644 --- a/lib/far_mar/market.rb +++ b/lib/far_mar/market.rb @@ -40,13 +40,9 @@ def products end return products end - # - # # returns the vendor with the highest revenue from a market - # def preferred_vendor - # - # end # returns the vendor with the highest revenue for the given date + # or returns the vendor with the highest revenue if no date is given def preferred_vendor(date = nil) return vendors.max_by { |vend| vend.revenue } if date.nil? return nil if date.class != DateTime @@ -56,14 +52,10 @@ def preferred_vendor(date = nil) return m_vendors[max_index] end - # returns the vendor with the lowest revenue from a market - def worst_vendor - return vendors.min_by { |vend| vend.revenue } - end - - #returns the vendor with the lowest revenue on the given date - def worst_vendor_on(date) - return nil if date.class != DateTime + # returns the vendor with the lowest revenue on the given date + # or returns the vendor with the lowest revenue if no date is given + def worst_vendor(date = nil) + return vendors.min_by { |vend| vend.revenue } if date.nil? return nil if date.class != DateTime m_vendors = vendors rev = m_vendors.map { |vend| vend.day_revenue(date) } diff --git a/spec/far_mar/market_spec.rb b/spec/far_mar/market_spec.rb index ecff05ec..e4a7f281 100644 --- a/spec/far_mar/market_spec.rb +++ b/spec/far_mar/market_spec.rb @@ -109,14 +109,14 @@ end # Returns the vendor with the lowest revenue for the given date - describe "#worst_vendor_on(date)" do + describe "#worst_vendor(date)" do it "returns nil for an invalid date" do - expect(sample_market.worst_vendor_on(123)).to be_nil - expect(sample_market.worst_vendor_on('abc')).to be_nil + expect(sample_market.worst_vendor(123)).to be_nil + expect(sample_market.worst_vendor('abc')).to be_nil end - it "returns the vendor with the highest revenue for the given date" do + it "returns the vendor with the lowest revenue for the given date" do date = DateTime.parse("2013-11-12T13:07:19-08:00") - worst = sample_market2.worst_vendor_on(date) + worst = sample_market2.worst_vendor(date) expect(worst).to be_an_instance_of(FarMar::Vendor) # NOTE: many different possible with 0: 102, 105, 107 expect(worst.id).to eq(102) From d697e4a7de3dd018e644e67c852e2c4720902e4a Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Fri, 23 Oct 2015 14:35:49 -0700 Subject: [PATCH 102/107] create test for market.search method --- lib/far_mar/far_mar_base.rb | 4 ++++ lib/far_mar/market.rb | 6 ++++++ spec/far_mar/market_spec.rb | 18 ++++++++++++++++++ 3 files changed, 28 insertions(+) diff --git a/lib/far_mar/far_mar_base.rb b/lib/far_mar/far_mar_base.rb index 469701c0..1acf2324 100644 --- a/lib/far_mar/far_mar_base.rb +++ b/lib/far_mar/far_mar_base.rb @@ -29,5 +29,9 @@ def self.find(id) self.all_objects.find { |obj| obj.id == id } end + def self.find_by_x(match) + + end + end end diff --git a/lib/far_mar/market.rb b/lib/far_mar/market.rb index e0603abf..e5c47824 100644 --- a/lib/far_mar/market.rb +++ b/lib/far_mar/market.rb @@ -24,6 +24,12 @@ def self.convert_to_hash(market_array) return market_hash end + # Returns a collection of FarMar::Market instances where the market name + # or vendor name contain the search_term + def self.search(search_term) + + end + # Returns a collection of FarMar::Vendor instances that are associated # with the market by the market_id field def vendors diff --git a/spec/far_mar/market_spec.rb b/spec/far_mar/market_spec.rb index e4a7f281..012e15cf 100644 --- a/spec/far_mar/market_spec.rb +++ b/spec/far_mar/market_spec.rb @@ -45,6 +45,24 @@ end end + describe ".search(search_term)" do + let(:cor_search) { FarMar::Market.search("cor") } + it "returns an empty array when search term is not a string" do + expect(FarMar::Market.search(123)).to eq([]) + expect(FarMar::Market.search([1, 2, 3])).to eq([]) + end + it "returns a collection of market instances" do + expect(cor_search).to be_an_instance_of(Array) + cor_search.each do |item| + expect(item).to be_an_instance_of(FarMar::Market) + end + end + it "markets have search term in market or vendor name" do + expect(cor_search.length).to eq(37) + end + + end + describe "#vendors" do it "returns array of vendors for the market" do vendors = sample_market2.vendors From 9de52ad569a148343d643316fa9da36d19922c28 Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Fri, 23 Oct 2015 14:47:22 -0700 Subject: [PATCH 103/107] create FarMar::Market.search method --- lib/far_mar/market.rb | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/lib/far_mar/market.rb b/lib/far_mar/market.rb index e5c47824..84706b3b 100644 --- a/lib/far_mar/market.rb +++ b/lib/far_mar/market.rb @@ -27,7 +27,27 @@ def self.convert_to_hash(market_array) # Returns a collection of FarMar::Market instances where the market name # or vendor name contain the search_term def self.search(search_term) - + return [] if search_term.class != String + search_term.downcase! + # find all markets that include the search term in the market name + market_match = FarMar::Market.all_objects.select do |market| + market_name = market.name.downcase + market_name.include?(search_term) + end + # find all markets that have the search term in the vendor name + vendor_match = FarMar::Vendor.all_objects.select do |vendor| + vendor_name = vendor.name.downcase + vendor_name.include?(search_term) + end + # This iterates through the vendors that include the search term + vendor_match.each do |vendor| + # and finds their associated market based on the market_id + market = FarMar::Market.find(vendor.market_id) + # then checks to see if the market is already in the market_match array + # and if not, it pushes it into the array + market_match.push(market) if !market_match.include?(market) + end + return market_match end # Returns a collection of FarMar::Vendor instances that are associated From 1d9f047144c08cd9889f032844eb8e4ed010c485 Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Fri, 23 Oct 2015 14:56:34 -0700 Subject: [PATCH 104/107] create vendor.find_by_name(match) tests --- spec/far_mar/vendor_spec.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/spec/far_mar/vendor_spec.rb b/spec/far_mar/vendor_spec.rb index dd075010..29c45d73 100644 --- a/spec/far_mar/vendor_spec.rb +++ b/spec/far_mar/vendor_spec.rb @@ -86,6 +86,18 @@ end end + describe ".find_by_name(match)" do + it "returns nil for no match" do + expect(FarMar::Vendor.find_by_name(3)).to be_nil + expect(FarMar::Vendor.find_by_name('xxxxxxxxxx')).to be_nil + expect(FarMar::Vendor.find_by_name([])).to be_nil + end + it "returns a single instance for matching the attribute" do + expect(FarMar::Vendor.find_by_name('Von-Hamill').id).to be(18) + expect(FarMar::Vendor.find_by_name('von-hamill').id).to be(18) + end + end + describe "#products" do it "returns a collection of Products with the vendor's ID" do products = sample_vendor.products From c234fdca030d49271aeceb4c40aac8d5dfe67a8c Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Fri, 23 Oct 2015 15:03:39 -0700 Subject: [PATCH 105/107] create vendor find_by_name --- lib/far_mar/vendor.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/lib/far_mar/vendor.rb b/lib/far_mar/vendor.rb index 4a138101..e249c7e0 100644 --- a/lib/far_mar/vendor.rb +++ b/lib/far_mar/vendor.rb @@ -42,6 +42,16 @@ def self.most_items(n) most_x(n, "@num_sales") end + # returns a single instance whose attribute matches the parameter + def self.find_by_name(match) + return nil if match.class != String + result = FarMar::Vendor.all_objects.find do |vendor| + test_string = vendor.name.downcase + test_string.include?(match.downcase) + end + return result + end + # returns a collection of FarMar::Product instances that are # associated by the FarMar::Product vendor_id field def products From 5aa03f676e20d7834cc234ab6564d9d5bbd68991 Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Fri, 23 Oct 2015 15:05:38 -0700 Subject: [PATCH 106/107] refactor find_by_name to be in far_mar_base file --- lib/far_mar/far_mar_base.rb | 10 ++++++++-- lib/far_mar/vendor.rb | 10 +--------- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/lib/far_mar/far_mar_base.rb b/lib/far_mar/far_mar_base.rb index 1acf2324..a504eabc 100644 --- a/lib/far_mar/far_mar_base.rb +++ b/lib/far_mar/far_mar_base.rb @@ -29,8 +29,14 @@ def self.find(id) self.all_objects.find { |obj| obj.id == id } end - def self.find_by_x(match) - + # returns a single instance whose attribute matches the parameter + def self.find_by_name(match) + return nil if match.class != String + result = FarMar::Vendor.all_objects.find do |vendor| + test_string = vendor.name.downcase + test_string.include?(match.downcase) + end + return result end end diff --git a/lib/far_mar/vendor.rb b/lib/far_mar/vendor.rb index e249c7e0..3b3bb141 100644 --- a/lib/far_mar/vendor.rb +++ b/lib/far_mar/vendor.rb @@ -42,15 +42,7 @@ def self.most_items(n) most_x(n, "@num_sales") end - # returns a single instance whose attribute matches the parameter - def self.find_by_name(match) - return nil if match.class != String - result = FarMar::Vendor.all_objects.find do |vendor| - test_string = vendor.name.downcase - test_string.include?(match.downcase) - end - return result - end + # returns a collection of FarMar::Product instances that are # associated by the FarMar::Product vendor_id field From c02741f5fa54885f78bda425aec217af4b4bac62 Mon Sep 17 00:00:00 2001 From: Jennie Buechner Date: Fri, 23 Oct 2015 15:38:51 -0700 Subject: [PATCH 107/107] minor edits of comments and code --- lib/far_mar/sale.rb | 1 - lib/far_mar/vendor.rb | 2 -- 2 files changed, 3 deletions(-) diff --git a/lib/far_mar/sale.rb b/lib/far_mar/sale.rb index 0edea328..a3625851 100644 --- a/lib/far_mar/sale.rb +++ b/lib/far_mar/sale.rb @@ -5,7 +5,6 @@ class Sale < FarMar_Base def initialize(sale_hash) super(sale_hash) - # FarMar::Vendor.all_objects[@vendor_id].revenue += @amount end def self.all_objects diff --git a/lib/far_mar/vendor.rb b/lib/far_mar/vendor.rb index 3b3bb141..4a138101 100644 --- a/lib/far_mar/vendor.rb +++ b/lib/far_mar/vendor.rb @@ -42,8 +42,6 @@ def self.most_items(n) most_x(n, "@num_sales") end - - # returns a collection of FarMar::Product instances that are # associated by the FarMar::Product vendor_id field def products