From 440b5c88f86717c7aab9e72ed0067eec58e203bf Mon Sep 17 00:00:00 2001 From: Karin Kubischta Date: Tue, 6 Sep 2016 10:56:11 -0700 Subject: [PATCH 01/45] setting up spec and class files --- Rakefile | 11 +++++++++++ far_mar.rb | 11 +++++++++++ lib/farmar_market.rb | 0 lib/farmar_product.rb | 0 lib/farmar_sale.rb | 0 lib/farmar_vendor.rb | 0 specs/market_spec.rb | 0 specs/product_spec.rb | 0 specs/sale_spec.rb | 0 specs/spec_helper.rb | 12 ++++++++++++ specs/vendor_spec.rb | 0 11 files changed, 34 insertions(+) create mode 100644 Rakefile create mode 100644 far_mar.rb create mode 100644 lib/farmar_market.rb create mode 100644 lib/farmar_product.rb create mode 100644 lib/farmar_sale.rb create mode 100644 lib/farmar_vendor.rb create mode 100644 specs/market_spec.rb create mode 100644 specs/product_spec.rb create mode 100644 specs/sale_spec.rb create mode 100644 specs/spec_helper.rb create mode 100644 specs/vendor_spec.rb diff --git a/Rakefile b/Rakefile new file mode 100644 index 00000000..7a6bcf2f --- /dev/null +++ b/Rakefile @@ -0,0 +1,11 @@ +# I have also just copied the contents of the Rakefile that was included in the in-class TDD demo. +# To run the rake tests, just be inside the FarMar directory in your terminal and type 'rake'. + +require 'rake/testtask' + +Rake::TestTask.new do |t| + t.test_files = FileList['specs/*_spec.rb'] + #this is the set of files that will be tested. + end + +task default: :test diff --git a/far_mar.rb b/far_mar.rb new file mode 100644 index 00000000..1b2623b3 --- /dev/null +++ b/far_mar.rb @@ -0,0 +1,11 @@ +# gems your project needs +require 'csv' + +# our namespace module +module FarMar; end + +# all of our data classes that live in the module +require 'lib/farmar_market' +require 'lib/farmar_product' +require 'lib/farmar_sale' +require 'lib/farmar_vendor' diff --git a/lib/farmar_market.rb b/lib/farmar_market.rb new file mode 100644 index 00000000..e69de29b diff --git a/lib/farmar_product.rb b/lib/farmar_product.rb new file mode 100644 index 00000000..e69de29b diff --git a/lib/farmar_sale.rb b/lib/farmar_sale.rb new file mode 100644 index 00000000..e69de29b diff --git a/lib/farmar_vendor.rb b/lib/farmar_vendor.rb new file mode 100644 index 00000000..e69de29b diff --git a/specs/market_spec.rb b/specs/market_spec.rb new file mode 100644 index 00000000..e69de29b diff --git a/specs/product_spec.rb b/specs/product_spec.rb new file mode 100644 index 00000000..e69de29b diff --git a/specs/sale_spec.rb b/specs/sale_spec.rb new file mode 100644 index 00000000..e69de29b diff --git a/specs/spec_helper.rb b/specs/spec_helper.rb new file mode 100644 index 00000000..f0185f8c --- /dev/null +++ b/specs/spec_helper.rb @@ -0,0 +1,12 @@ +# This is from the simplecov documentation +require 'simplecov' +SimpleCov.start + +# I've just copied the spec_helper.rb file from the in-class TDD demo. +require 'minitest' +require 'minitest/spec' +require "minitest/autorun" +require "minitest/reporters" +require 'minitest/pride' + +Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new diff --git a/specs/vendor_spec.rb b/specs/vendor_spec.rb new file mode 100644 index 00000000..e69de29b From e9595870921b3a0e70c6dfd801d620dc967aaef7 Mon Sep 17 00:00:00 2001 From: Karin Kubischta Date: Tue, 6 Sep 2016 10:59:56 -0700 Subject: [PATCH 02/45] adding gitignore file --- .gitignore | 1 + 1 file changed, 1 insertion(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..404abb22 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +coverage/ From 48305a108b3dde3092f11088c482c8f4d7ea7c91 Mon Sep 17 00:00:00 2001 From: Karin Kubischta Date: Tue, 6 Sep 2016 11:21:17 -0700 Subject: [PATCH 03/45] added one spec test for market, it passes --- lib/farmar_market.rb | 29 +++++++++++++++++++++++++++++ specs/market_spec.rb | 13 +++++++++++++ 2 files changed, 42 insertions(+) diff --git a/lib/farmar_market.rb b/lib/farmar_market.rb index e69de29b..90b938ba 100644 --- a/lib/farmar_market.rb +++ b/lib/farmar_market.rb @@ -0,0 +1,29 @@ +require 'csv' + +module FarMar + class Market + def initialize (id, name, address, city, county, state, zip) + # ID - (Fixnum) a unique identifier for that market + # Name - (String) the name of the market (not guaranteed unique) + # Address - (String) street address of the market + # City - (String) city in which the market is located + # County - (String) county in which the market is located + # State - (String) state in which the market is located + # Zip - (String) zipcode in which the market is located) + end + end +end + +# This is what I wrote for the accounts self.all to read in the csv file. +# def self.all +# accounts = {} +# +# CSV.read("support/accounts.csv").each do |line| +# account_id = line[0].to_i +# balance = line[1].to_i +# open_date = line[2] +# accounts[account_id] = self.new(account_id, open_date, balance) +# end +# +# return accounts +# end diff --git a/specs/market_spec.rb b/specs/market_spec.rb index e69de29b..e249705e 100644 --- a/specs/market_spec.rb +++ b/specs/market_spec.rb @@ -0,0 +1,13 @@ +require_relative 'spec_helper' #get all the stuff we need for testing. +require_relative '../lib/farmar_market' #this is the class we're testing here. + +module FarMar + describe Market do + describe "#initialize" do + it "should make a new instance of market" do + #this just tests the basic "I can make a new instance of this class." + Market.new(1, "name", "address", "city", "county", "ST", 98017).must_be_instance_of(Market) + end + end + end +end From 8c94bd16031ac5b9fc67d2b8fd95289bf7a6e929 Mon Sep 17 00:00:00 2001 From: Karin Kubischta Date: Tue, 6 Sep 2016 11:27:09 -0700 Subject: [PATCH 04/45] updated spec and class for product --- lib/farmar_product.rb | 8 ++++++++ specs/product_spec.rb | 12 ++++++++++++ 2 files changed, 20 insertions(+) diff --git a/lib/farmar_product.rb b/lib/farmar_product.rb index e69de29b..db9eb550 100644 --- a/lib/farmar_product.rb +++ b/lib/farmar_product.rb @@ -0,0 +1,8 @@ +require 'csv' + +module FarMar + class Product + + + end +end diff --git a/specs/product_spec.rb b/specs/product_spec.rb index e69de29b..8cda9354 100644 --- a/specs/product_spec.rb +++ b/specs/product_spec.rb @@ -0,0 +1,12 @@ +require_relative 'spec_helper' #get all the stuff we need for testing. +require_relative '../lib/farmar_product' #this is the class we're testing here. + +module FarMar + describe Product do + describe "#initialize" do + it "should make a new instance of product" do + #write test to check that a new product instance was created. + end + end + end +end From dccd6be79071699f29d95e25513292272799d441 Mon Sep 17 00:00:00 2001 From: Karin Kubischta Date: Tue, 6 Sep 2016 11:40:09 -0700 Subject: [PATCH 05/45] updated sale_spec and sale class --- lib/farmar_sale.rb | 5 +++++ specs/sale_spec.rb | 12 ++++++++++++ 2 files changed, 17 insertions(+) diff --git a/lib/farmar_sale.rb b/lib/farmar_sale.rb index e69de29b..d68f9ded 100644 --- a/lib/farmar_sale.rb +++ b/lib/farmar_sale.rb @@ -0,0 +1,5 @@ +module FarMar + class Sale + + end +end diff --git a/specs/sale_spec.rb b/specs/sale_spec.rb index e69de29b..a6b00f62 100644 --- a/specs/sale_spec.rb +++ b/specs/sale_spec.rb @@ -0,0 +1,12 @@ +require_relative '../lib/farmar_sale' +require_relative 'spec_helper' + +module FarMar + describe Sale do + describe "#initialize" do + it "should make a new instance of sale" do + #write something here to test new instance of sale + end + end + end +end From 60bda626942e745a5ccf7ed3e538eb828de58eb2 Mon Sep 17 00:00:00 2001 From: Karin Kubischta Date: Tue, 6 Sep 2016 11:43:51 -0700 Subject: [PATCH 06/45] updated vendor spec/class --- lib/farmar_sale.rb | 2 ++ lib/farmar_vendor.rb | 7 +++++++ specs/vendor_spec.rb | 12 ++++++++++++ 3 files changed, 21 insertions(+) diff --git a/lib/farmar_sale.rb b/lib/farmar_sale.rb index d68f9ded..8ee98c4c 100644 --- a/lib/farmar_sale.rb +++ b/lib/farmar_sale.rb @@ -1,3 +1,5 @@ +require 'csv' + module FarMar class Sale diff --git a/lib/farmar_vendor.rb b/lib/farmar_vendor.rb index e69de29b..362ead07 100644 --- a/lib/farmar_vendor.rb +++ b/lib/farmar_vendor.rb @@ -0,0 +1,7 @@ +require 'csv' + +module FarMar + class Vendor + + end +end diff --git a/specs/vendor_spec.rb b/specs/vendor_spec.rb index e69de29b..c0f77db8 100644 --- a/specs/vendor_spec.rb +++ b/specs/vendor_spec.rb @@ -0,0 +1,12 @@ +require_relative '../lib/farmar_vendor' +require_relative 'spec_helper' + +module FarMar + describe Vendor do + describe "#initialize" do + it "should make a new instance of vendor" do + #write something here to test new instance of vendor + end + end + end +end From 9e4fbb26dd2364b8cd4279ab37c43c955de66cd9 Mon Sep 17 00:00:00 2001 From: Karin Kubischta Date: Tue, 6 Sep 2016 12:05:29 -0700 Subject: [PATCH 07/45] updated specs with initialize tests, pass --- lib/farmar_market.rb | 3 +++ lib/farmar_product.rb | 10 ++++++++-- lib/farmar_sale.rb | 9 ++++++++- lib/farmar_vendor.rb | 10 +++++++++- specs/market_spec.rb | 2 ++ specs/product_spec.rb | 8 +++++++- specs/sale_spec.rb | 6 ++++++ specs/vendor_spec.rb | 7 +++++++ 8 files changed, 50 insertions(+), 5 deletions(-) diff --git a/lib/farmar_market.rb b/lib/farmar_market.rb index 90b938ba..e0ab049a 100644 --- a/lib/farmar_market.rb +++ b/lib/farmar_market.rb @@ -14,6 +14,9 @@ def initialize (id, name, address, city, county, state, zip) end end +# self.all: returns a collection of instances, representing all of the objects described in the CSV +# self.find(id): returns an instance of the object where the value of the id field in the CSV matches the passed parameter. + # This is what I wrote for the accounts self.all to read in the csv file. # def self.all # accounts = {} diff --git a/lib/farmar_product.rb b/lib/farmar_product.rb index db9eb550..fffc7c8d 100644 --- a/lib/farmar_product.rb +++ b/lib/farmar_product.rb @@ -2,7 +2,13 @@ module FarMar class Product - - + def initialize(product_id, product_name, vendor_id) + # ID - (Fixnum) uniquely identifies the product + # Name - (String) the name of the product (not guaranteed unique) + # Vendor_id - (Fixnum) a reference to which vendor sells this product + end end end + +# self.all: returns a collection of instances, representing all of the objects described in the CSV +# self.find(id): returns an instance of the object where the value of the id field in the CSV matches the passed parameter. diff --git a/lib/farmar_sale.rb b/lib/farmar_sale.rb index 8ee98c4c..61ef1e2f 100644 --- a/lib/farmar_sale.rb +++ b/lib/farmar_sale.rb @@ -2,6 +2,13 @@ module FarMar class Sale - + def initialize(product_id, product_name, vendor_id) + # ID - (Fixnum) uniquely identifies the product + # Name - (String) the name of the product (not guaranteed unique) + # Vendor_id - (Fixnum) a reference to which vendor sells this product + end end end + +# self.all: returns a collection of instances, representing all of the objects described in the CSV +# self.find(id): returns an instance of the object where the value of the id field in the CSV matches the passed parameter. diff --git a/lib/farmar_vendor.rb b/lib/farmar_vendor.rb index 362ead07..fea3d097 100644 --- a/lib/farmar_vendor.rb +++ b/lib/farmar_vendor.rb @@ -2,6 +2,14 @@ module FarMar class Vendor - + def initialize(id, name, num_employees, market_id) + # ID - (Fixnum) uniquely identifies the vendor + # Name - (String) the name of the vendor (not guaranteed unique) + # No. of Employees - (Fixnum) How many employees the vendor has at the market + # Market_id - (Fixnum) a reference to which market the vendor attends + end end end + +# self.all: returns a collection of instances, representing all of the objects described in the CSV +# self.find(id): returns an instance of the object where the value of the id field in the CSV matches the passed parameter. diff --git a/specs/market_spec.rb b/specs/market_spec.rb index e249705e..d8c4dc93 100644 --- a/specs/market_spec.rb +++ b/specs/market_spec.rb @@ -9,5 +9,7 @@ module FarMar Market.new(1, "name", "address", "city", "county", "ST", 98017).must_be_instance_of(Market) end end + # self.all: returns a collection of instances, representing all of the objects described in the CSV + # self.find(id): returns an instance of the object where the value of the id field in the CSV matches the passed parameter. end end diff --git a/specs/product_spec.rb b/specs/product_spec.rb index 8cda9354..0dc851de 100644 --- a/specs/product_spec.rb +++ b/specs/product_spec.rb @@ -5,8 +5,14 @@ module FarMar describe Product do describe "#initialize" do it "should make a new instance of product" do - #write test to check that a new product instance was created. + #write test to check that a new product instance was created. + Product.new(1, "product", 123).must_be_instance_of(Product) + # ID - (Fixnum) uniquely identifies the product + # Name - (String) the name of the product (not guaranteed unique) + # Vendor_id - (Fixnum) a reference to which vendor sells this product end end + # self.all: returns a collection of instances, representing all of the objects described in the CSV + # self.find(id): returns an instance of the object where the value of the id field in the CSV matches the passed parameter. end end diff --git a/specs/sale_spec.rb b/specs/sale_spec.rb index a6b00f62..49c9323e 100644 --- a/specs/sale_spec.rb +++ b/specs/sale_spec.rb @@ -6,7 +6,13 @@ module FarMar describe "#initialize" do it "should make a new instance of sale" do #write something here to test new instance of sale + Sale.new(1, "product name", 1).must_be_instance_of(Sale) + # ID - (Fixnum) uniquely identifies the product (product id?) + # Name - (String) the name of the product (not guaranteed unique) + # Vendor_id - (Fixnum) a reference to which vendor sells this product end end + # self.all: returns a collection of instances, representing all of the objects described in the CSV + # self.find(id): returns an instance of the object where the value of the id field in the CSV matches the passed parameter. end end diff --git a/specs/vendor_spec.rb b/specs/vendor_spec.rb index c0f77db8..de7e6978 100644 --- a/specs/vendor_spec.rb +++ b/specs/vendor_spec.rb @@ -6,7 +6,14 @@ module FarMar describe "#initialize" do it "should make a new instance of vendor" do #write something here to test new instance of vendor + Vendor.new(1, "vendor name", 1, 1).must_be_instance_of(Vendor) + # ID - (Fixnum) uniquely identifies the vendor + # Name - (String) the name of the vendor (not guaranteed unique) + # No. of Employees - (Fixnum) How many employees the vendor has at the market + # Market_id - (Fixnum) a reference to which market the vendor attends end end + # self.all: returns a collection of instances, representing all of the objects described in the CSV + # self.find(id): returns an instance of the object where the value of the id field in the CSV matches the passed parameter. end end From 750fc95113d24f5c00c76b4697c52a6bae1fd512 Mon Sep 17 00:00:00 2001 From: Karin Kubischta Date: Tue, 6 Sep 2016 12:17:10 -0700 Subject: [PATCH 08/45] got explanation for what far_mar is doing --- far_mar.rb | 8 ++++---- specs/market_spec.rb | 1 - specs/product_spec.rb | 1 - specs/sale_spec.rb | 1 - specs/spec_helper.rb | 4 ++++ specs/vendor_spec.rb | 1 - 6 files changed, 8 insertions(+), 8 deletions(-) diff --git a/far_mar.rb b/far_mar.rb index 1b2623b3..8d4d9af2 100644 --- a/far_mar.rb +++ b/far_mar.rb @@ -5,7 +5,7 @@ module FarMar; end # all of our data classes that live in the module -require 'lib/farmar_market' -require 'lib/farmar_product' -require 'lib/farmar_sale' -require 'lib/farmar_vendor' +require_relative './lib/farmar_market' +require_relative './lib/farmar_product' +require_relative './lib/farmar_sale' +require_relative './lib/farmar_vendor' diff --git a/specs/market_spec.rb b/specs/market_spec.rb index d8c4dc93..2ede661b 100644 --- a/specs/market_spec.rb +++ b/specs/market_spec.rb @@ -1,5 +1,4 @@ require_relative 'spec_helper' #get all the stuff we need for testing. -require_relative '../lib/farmar_market' #this is the class we're testing here. module FarMar describe Market do diff --git a/specs/product_spec.rb b/specs/product_spec.rb index 0dc851de..9c121c08 100644 --- a/specs/product_spec.rb +++ b/specs/product_spec.rb @@ -1,5 +1,4 @@ require_relative 'spec_helper' #get all the stuff we need for testing. -require_relative '../lib/farmar_product' #this is the class we're testing here. module FarMar describe Product do diff --git a/specs/sale_spec.rb b/specs/sale_spec.rb index 49c9323e..7d6cd858 100644 --- a/specs/sale_spec.rb +++ b/specs/sale_spec.rb @@ -1,4 +1,3 @@ -require_relative '../lib/farmar_sale' require_relative 'spec_helper' module FarMar diff --git a/specs/spec_helper.rb b/specs/spec_helper.rb index f0185f8c..c5e9f160 100644 --- a/specs/spec_helper.rb +++ b/specs/spec_helper.rb @@ -9,4 +9,8 @@ require "minitest/reporters" require 'minitest/pride' +# The far_mar file will reference all the farmer's market class files. + +require_relative '../far_mar' + Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new diff --git a/specs/vendor_spec.rb b/specs/vendor_spec.rb index de7e6978..c7ea0d9c 100644 --- a/specs/vendor_spec.rb +++ b/specs/vendor_spec.rb @@ -1,4 +1,3 @@ -require_relative '../lib/farmar_vendor' require_relative 'spec_helper' module FarMar From 52b64fffbb1c9ff4ba9ce217644e2deff114122a Mon Sep 17 00:00:00 2001 From: Karin Kubischta Date: Tue, 6 Sep 2016 12:22:07 -0700 Subject: [PATCH 09/45] fixed initialize for sale --- lib/farmar_sale.rb | 10 ++++++---- specs/sale_spec.rb | 10 ++++++---- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/lib/farmar_sale.rb b/lib/farmar_sale.rb index 61ef1e2f..a237a44b 100644 --- a/lib/farmar_sale.rb +++ b/lib/farmar_sale.rb @@ -2,10 +2,12 @@ module FarMar class Sale - def initialize(product_id, product_name, vendor_id) - # ID - (Fixnum) uniquely identifies the product - # Name - (String) the name of the product (not guaranteed unique) - # Vendor_id - (Fixnum) a reference to which vendor sells this product + def initialize(sale_id, amount, purchase_time, vendor_id, product_id) + # ID - (Fixnum) uniquely identifies the sale + # Amount - (Fixnum) the amount of the transaction, in cents (i.e., 150 would be $1.50) + # Purchase_time - (Datetime) when the sale was completed + # Vendor_id - (Fixnum) a reference to which vendor completed the sale + # Product_id - (Fixnum) a reference to which product was sold end end end diff --git a/specs/sale_spec.rb b/specs/sale_spec.rb index 7d6cd858..1f7fd3e9 100644 --- a/specs/sale_spec.rb +++ b/specs/sale_spec.rb @@ -5,10 +5,12 @@ module FarMar describe "#initialize" do it "should make a new instance of sale" do #write something here to test new instance of sale - Sale.new(1, "product name", 1).must_be_instance_of(Sale) - # ID - (Fixnum) uniquely identifies the product (product id?) - # Name - (String) the name of the product (not guaranteed unique) - # Vendor_id - (Fixnum) a reference to which vendor sells this product + Sale.new(1, 100, DateTime.new(2104), 1, 1).must_be_instance_of(Sale) + # ID - (Fixnum) uniquely identifies the sale + # Amount - (Fixnum) the amount of the transaction, in cents (i.e., 150 would be $1.50) + # Purchase_time - (Datetime) when the sale was completed + # Vendor_id - (Fixnum) a reference to which vendor completed the sale + # Product_id - (Fixnum) a reference to which product was sold end end # self.all: returns a collection of instances, representing all of the objects described in the CSV From 2f806ab4cbb72fc1e58764573c9bf8b0901d42c0 Mon Sep 17 00:00:00 2001 From: Karin Kubischta Date: Tue, 6 Sep 2016 12:32:05 -0700 Subject: [PATCH 10/45] added additional initializtion tests --- lib/farmar_market.rb | 4 ++++ specs/market_spec.rb | 17 +++++++++++++++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/lib/farmar_market.rb b/lib/farmar_market.rb index e0ab049a..ae7b67d5 100644 --- a/lib/farmar_market.rb +++ b/lib/farmar_market.rb @@ -2,10 +2,14 @@ module FarMar class Market + attr_reader :id, :name, :address def initialize (id, name, address, city, county, state, zip) # ID - (Fixnum) a unique identifier for that market + @id = id # Name - (String) the name of the market (not guaranteed unique) + @name = name # Address - (String) street address of the market + @address = address # City - (String) city in which the market is located # County - (String) county in which the market is located # State - (String) state in which the market is located diff --git a/specs/market_spec.rb b/specs/market_spec.rb index 2ede661b..ad416ded 100644 --- a/specs/market_spec.rb +++ b/specs/market_spec.rb @@ -2,13 +2,26 @@ module FarMar describe Market do + let(:market_test) {Market.new(1, "name", "address", "city", "county", "ST", 98017)} describe "#initialize" do it "should make a new instance of market" do #this just tests the basic "I can make a new instance of this class." - Market.new(1, "name", "address", "city", "county", "ST", 98017).must_be_instance_of(Market) + market_test.must_be_instance_of(Market) end + it "should respond to .id" do + market_test.id.must_be_instance_of(Fixnum) + end + it "should respond to .name" do + market_test.name.must_be_instance_of(String) + end + it "should respond to .address" do + market_test.address.must_be_instance_of(String) + end + end + describe "#self.all" do + # self.all: returns a collection of instances, representing all of the objects described in the CSV + end - # self.all: returns a collection of instances, representing all of the objects described in the CSV # self.find(id): returns an instance of the object where the value of the id field in the CSV matches the passed parameter. end end From 5847eb768704837338f4b3fb0d1fe60ec40ab618 Mon Sep 17 00:00:00 2001 From: Karin Kubischta Date: Tue, 6 Sep 2016 14:10:38 -0700 Subject: [PATCH 11/45] added specs for self.all and self.find(id), they pass --- lib/farmar_market.rb | 27 +++++++++++++++++++++++++-- specs/market_spec.rb | 27 +++++++++++++++++++++++---- 2 files changed, 48 insertions(+), 6 deletions(-) diff --git a/lib/farmar_market.rb b/lib/farmar_market.rb index ae7b67d5..4e646dff 100644 --- a/lib/farmar_market.rb +++ b/lib/farmar_market.rb @@ -15,11 +15,34 @@ def initialize (id, name, address, city, county, state, zip) # State - (String) state in which the market is located # Zip - (String) zipcode in which the market is located) end + + def self.all + # self.all: returns a collection of instances, representing all of the objects described in the CSV + markets = {} + + CSV.read("support/markets.csv").each do |line| + # Is there a way to do this with an enumerable instead of an each? + id = line[0].to_i + name = line[1] + address = line[2] + city = line[3] + county = line[4] + state = line[5] + zip = line[6].to_i + markets[id] = self.new(id, name, address, city, county, state, zip) + end + + return markets + end + + def self.find(id) + # self.find(id): returns an instance of the object where the value of the id field in the CSV matches the passed parameter. + all_markets = self.all + return all_markets[id] + end end end -# self.all: returns a collection of instances, representing all of the objects described in the CSV -# self.find(id): returns an instance of the object where the value of the id field in the CSV matches the passed parameter. # This is what I wrote for the accounts self.all to read in the csv file. # def self.all diff --git a/specs/market_spec.rb b/specs/market_spec.rb index ad416ded..58f54890 100644 --- a/specs/market_spec.rb +++ b/specs/market_spec.rb @@ -5,13 +5,15 @@ module FarMar let(:market_test) {Market.new(1, "name", "address", "city", "county", "ST", 98017)} describe "#initialize" do it "should make a new instance of market" do - #this just tests the basic "I can make a new instance of this class." + # this just tests the basic "I can make a new instance of this class." market_test.must_be_instance_of(Market) end - it "should respond to .id" do + + # I will need to get some information back from each instance of market - namely the name and the ID, check that a newly instantiated market will have these things. + it "should respond to .id with a Fixnum" do market_test.id.must_be_instance_of(Fixnum) end - it "should respond to .name" do + it "should respond to .name with a String" do market_test.name.must_be_instance_of(String) end it "should respond to .address" do @@ -19,9 +21,26 @@ module FarMar end end describe "#self.all" do + it "should return a collection of the markets included in the csv" do # self.all: returns a collection of instances, representing all of the objects described in the CSV + # I think this will be a hash - key of market_id, and value of market_name, etc. + Market.all.must_be_instance_of(Hash) + end + it "should contain a specific market in the csv" do + # Check that the Hash contains a key of 2 + Market.all.keys.must_include(2) + end + end + describe "#self.find(id)" do + # self.find(id): returns an instance of the object where the value of the id field in the CSV matches the passed parameter. + it "should return an instance of Market when given an id" do + Market.find(2).must_be_instance_of(Market) + end + + it "should return the correct instance of Market when given an id" do + Market.find(12).name.must_equal("Coxsackie Farmers' Market") + end end - # self.find(id): returns an instance of the object where the value of the id field in the CSV matches the passed parameter. end end From d10d807693a24d5d84979658980e7afc8c7d403f Mon Sep 17 00:00:00 2001 From: Karin Kubischta Date: Tue, 6 Sep 2016 14:28:52 -0700 Subject: [PATCH 12/45] specs added for vendor .all method, pass --- lib/farmar_market.rb | 19 ++++++++++++------- lib/farmar_vendor.rb | 22 +++++++++++++++++++++- specs/vendor_spec.rb | 30 +++++++++++++++++++++++++++--- 3 files changed, 60 insertions(+), 11 deletions(-) diff --git a/lib/farmar_market.rb b/lib/farmar_market.rb index 4e646dff..6d59ba83 100644 --- a/lib/farmar_market.rb +++ b/lib/farmar_market.rb @@ -22,13 +22,18 @@ def self.all CSV.read("support/markets.csv").each do |line| # Is there a way to do this with an enumerable instead of an each? - id = line[0].to_i - name = line[1] - address = line[2] - city = line[3] - county = line[4] - state = line[5] - zip = line[6].to_i + # id = line[0].to_i + # name = line[1] + # address = line[2] + # city = line[3] + # county = line[4] + # state = line[5] + # zip = line[6].to_i + + id, name, address, city, county, state, zip = line # parallel assignment! + id = id.to_i # need id to be a fixnum + zip = zip.to_i # want zip to be a fixnum also (I think) + markets[id] = self.new(id, name, address, city, county, state, zip) end diff --git a/lib/farmar_vendor.rb b/lib/farmar_vendor.rb index fea3d097..38af8f1e 100644 --- a/lib/farmar_vendor.rb +++ b/lib/farmar_vendor.rb @@ -2,12 +2,32 @@ module FarMar class Vendor + attr_reader :id, :name, :market_id, :num_employees def initialize(id, name, num_employees, market_id) # ID - (Fixnum) uniquely identifies the vendor + @id = id # Name - (String) the name of the vendor (not guaranteed unique) + @name = name # No. of Employees - (Fixnum) How many employees the vendor has at the market + @num_employees = num_employees # Market_id - (Fixnum) a reference to which market the vendor attends - end + @market_id = market_id + end + + def self.all + vendors = {} + + CSV.read("support/vendors.csv").each do |line| + id, name, num_employees, market_id = line # parallel assignment! + id = id.to_i # need id to be a fixnum + num_employees = num_employees.to_i # want num_employees to be a fixnum also + market_id = market_id.to_i #want market_id to be a fixnum + + vendors[id] = self.new(id, name, num_employees, market_id) + end + + return vendors + end end end diff --git a/specs/vendor_spec.rb b/specs/vendor_spec.rb index c7ea0d9c..878c031a 100644 --- a/specs/vendor_spec.rb +++ b/specs/vendor_spec.rb @@ -2,17 +2,41 @@ module FarMar describe Vendor do + let(:ven) {Vendor.new(1, "vendor name", 1, 1)} describe "#initialize" do it "should make a new instance of vendor" do #write something here to test new instance of vendor - Vendor.new(1, "vendor name", 1, 1).must_be_instance_of(Vendor) + ven.must_be_instance_of(Vendor) # ID - (Fixnum) uniquely identifies the vendor # Name - (String) the name of the vendor (not guaranteed unique) # No. of Employees - (Fixnum) How many employees the vendor has at the market # Market_id - (Fixnum) a reference to which market the vendor attends end + it "should respond to .name with a string" do + ven.name.must_be_instance_of(String) + end + + it "should respond to .name with the correct name" do + ven.name.must_equal("vendor name") + end + end + describe "#self.all" do + # self.all: returns a collection of instances, representing all of the objects described in the CSV + it "should return a collection of Vendor objects" do + # I'm going to use the id as the key, rest of info as value. + Vendor.all.must_be_instance_of(Hash) + end + it "should contain a specific Vendor object, given the key" do + Vendor.all.must_include(2) end - # self.all: returns a collection of instances, representing all of the objects described in the CSV - # self.find(id): returns an instance of the object where the value of the id field in the CSV matches the passed parameter. + describe "self.find(id)" do + it "should return the correct name given the id" do + # self.find(id): returns an instance of the object where the value of the id field in the CSV matches the passed parameter. + skip + # 74,Haag-Padberg,3,17 + end + end + end + end end From ef2fbf0c385980058e22510f43f76772f46f73fc Mon Sep 17 00:00:00 2001 From: Karin Kubischta Date: Tue, 6 Sep 2016 14:52:03 -0700 Subject: [PATCH 13/45] added specs for vendor .find(id), pass --- lib/farmar_vendor.rb | 9 ++++++--- specs/product_spec.rb | 3 ++- specs/vendor_spec.rb | 8 ++++++-- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/lib/farmar_vendor.rb b/lib/farmar_vendor.rb index 38af8f1e..02627aef 100644 --- a/lib/farmar_vendor.rb +++ b/lib/farmar_vendor.rb @@ -15,6 +15,7 @@ def initialize(id, name, num_employees, market_id) end def self.all + # self.all: returns a collection of instances, representing all of the objects described in the CSV vendors = {} CSV.read("support/vendors.csv").each do |line| @@ -28,8 +29,10 @@ def self.all return vendors end + def self.find(id) + # self.find(id): returns an instance of the object where the value of the id field in the CSV matches the passed parameter. + all_vendors = self.all + all_vendors[id] + end end end - -# self.all: returns a collection of instances, representing all of the objects described in the CSV -# self.find(id): returns an instance of the object where the value of the id field in the CSV matches the passed parameter. diff --git a/specs/product_spec.rb b/specs/product_spec.rb index 9c121c08..9328f049 100644 --- a/specs/product_spec.rb +++ b/specs/product_spec.rb @@ -2,10 +2,11 @@ module FarMar describe Product do + let(:pro_deal) {Product.new(1, "product", 123)} describe "#initialize" do it "should make a new instance of product" do #write test to check that a new product instance was created. - Product.new(1, "product", 123).must_be_instance_of(Product) + pro_deal.must_be_instance_of(Product) # ID - (Fixnum) uniquely identifies the product # Name - (String) the name of the product (not guaranteed unique) # Vendor_id - (Fixnum) a reference to which vendor sells this product diff --git a/specs/vendor_spec.rb b/specs/vendor_spec.rb index 878c031a..89d0b9d5 100644 --- a/specs/vendor_spec.rb +++ b/specs/vendor_spec.rb @@ -30,9 +30,13 @@ module FarMar Vendor.all.must_include(2) end describe "self.find(id)" do + # self.find(id): returns an instance of the object where the value of the id field in the CSV matches the passed parameter. + it "should return a vendor, given an id" do + Vendor.find(2).must_be_instance_of(Vendor) + end + it "should return the correct name given the id" do - # self.find(id): returns an instance of the object where the value of the id field in the CSV matches the passed parameter. - skip + Vendor.find(74).name.must_equal("Haag-Padberg") # 74,Haag-Padberg,3,17 end end From a9fbb0079260f061171042f337e9e66e0c29add5 Mon Sep 17 00:00:00 2001 From: Karin Kubischta Date: Tue, 6 Sep 2016 16:15:11 -0700 Subject: [PATCH 14/45] product .self and .find methods written/tested --- lib/farmar_product.rb | 28 +++++++++++++++++++++++++--- specs/product_spec.rb | 27 +++++++++++++++++++++++++-- 2 files changed, 50 insertions(+), 5 deletions(-) diff --git a/lib/farmar_product.rb b/lib/farmar_product.rb index fffc7c8d..be7e2d97 100644 --- a/lib/farmar_product.rb +++ b/lib/farmar_product.rb @@ -2,13 +2,35 @@ module FarMar class Product + attr_reader :product_id, :product_name, :vendor_id def initialize(product_id, product_name, vendor_id) # ID - (Fixnum) uniquely identifies the product + @product_id = product_id # Name - (String) the name of the product (not guaranteed unique) + @product_name = product_name # Vendor_id - (Fixnum) a reference to which vendor sells this product + @vendor_id = vendor_id + end + + def self.all + # self.all: returns a collection of instances, representing all of the objects described in the CSV + products = {} + + CSV.read("support/products.csv").each do |line| + product_id, product_name, vendor_id = line # parallel assignment! + product_id = product_id.to_i # need product_id to be a fixnum + vendor_id = vendor_id.to_i #want vendor_id to be a fixnum + + products[product_id] = self.new(product_id, product_name, vendor_id) + end + + return products + end + + def self.find(id) + # self.find(id): returns an instance of the object where the value of the id field in the CSV matches the passed parameter. + specific_product = Product.all + return specific_product[id] end end end - -# self.all: returns a collection of instances, representing all of the objects described in the CSV -# self.find(id): returns an instance of the object where the value of the id field in the CSV matches the passed parameter. diff --git a/specs/product_spec.rb b/specs/product_spec.rb index 9328f049..43b4d9c4 100644 --- a/specs/product_spec.rb +++ b/specs/product_spec.rb @@ -11,8 +11,31 @@ module FarMar # Name - (String) the name of the product (not guaranteed unique) # Vendor_id - (Fixnum) a reference to which vendor sells this product end + it "should respond to .product_name" do + pro_deal.product_name.must_equal("product") + end + end + describe "self.all" do + # self.all: returns a collection of instances, representing all of the objects described in the CSV + it "should return a collection of products" do + #return a collection of products + Product.all.must_be_instance_of(Hash) + end + + it "should be a hash of Products" do + Product.all[12].must_be_instance_of(Product) + end + end + describe "self.find(id)" do + # self.find(id): returns an instance of the object where the value of the id field in the CSV matches the passed parameter. + it "should return a Product object when given an id" do + Product.find(12).must_be_instance_of(Product) + end + + it "should return the correct Product object when given an id" do + Product.find(15).product_name.must_equal("Comfortable Pretzel") + # 15,Comfortable Pretzel,8 + end end - # self.all: returns a collection of instances, representing all of the objects described in the CSV - # self.find(id): returns an instance of the object where the value of the id field in the CSV matches the passed parameter. end end From 42754dcd94fd7952cfe9e7e0ded5e1e1f02c2cb0 Mon Sep 17 00:00:00 2001 From: Karin Kubischta Date: Tue, 6 Sep 2016 17:06:04 -0700 Subject: [PATCH 15/45] added self.all spec, it's currently broken --- lib/farmar_sale.rb | 27 ++++++++++++++++++++++++++- specs/sale_spec.rb | 20 ++++++++++++++++++-- 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/lib/farmar_sale.rb b/lib/farmar_sale.rb index a237a44b..c66b8142 100644 --- a/lib/farmar_sale.rb +++ b/lib/farmar_sale.rb @@ -1,16 +1,41 @@ require 'csv' +require 'date' module FarMar class Sale + attr_reader :sale_id, :amount, :vendor_id, :product_id, :purchase_time def initialize(sale_id, amount, purchase_time, vendor_id, product_id) + # sale_id, amount, purchase_time, vendor_id, product_id) # ID - (Fixnum) uniquely identifies the sale + @sale_id = sale_id # Amount - (Fixnum) the amount of the transaction, in cents (i.e., 150 would be $1.50) + @amount = amount # Purchase_time - (Datetime) when the sale was completed + @purchase_time = DateTime.new(purchase_time) #BUG: purchase_time in csv is the string representation of a datetime, not the comma delimited input that Datetime is expecting. https://ruby-doc.org/stdlib-2.3.1/libdoc/date/rdoc/DateTime.html#method-c-parse # Vendor_id - (Fixnum) a reference to which vendor completed the sale + @vendor_id = vendor_id # Product_id - (Fixnum) a reference to which product was sold + @product_id = product_id + end + + def self.all + # self.all: returns a collection of instances, representing all of the objects described in the CSV + sales = {} + + CSV.read("support/sales.csv").each do |line| + sale_id, amount, purchase_time, vendor_id, product_id = line # parallel assignment! + sale_id = sale_id.to_i # need sale_id to be a fixnum + amount = amount.to_i # want amount to be a fixnum also + purchase_time = DateTime.new(purchase_time) #BUG: the purchase_times are strings, that are the DateTime format, but not the format for making a new DateTime. This is currently breaking the .all test in sale_spec. + vendor_id = vendor_id # want vendor_id to be a fixnum + product_id = product_id # want product_id also to be a fixnum + + sales[sale_id] = self.new(sale_id, amount, purchase_time, vendor_id, product_id) + end + + return sales end end end -# self.all: returns a collection of instances, representing all of the objects described in the CSV # self.find(id): returns an instance of the object where the value of the id field in the CSV matches the passed parameter. diff --git a/specs/sale_spec.rb b/specs/sale_spec.rb index 1f7fd3e9..198285f1 100644 --- a/specs/sale_spec.rb +++ b/specs/sale_spec.rb @@ -2,18 +2,34 @@ module FarMar describe Sale do + let(:buy_a_thing) {Sale.new(1,100,2016, 1, 1)} describe "#initialize" do it "should make a new instance of sale" do #write something here to test new instance of sale - Sale.new(1, 100, DateTime.new(2104), 1, 1).must_be_instance_of(Sale) + buy_a_thing.must_be_instance_of(Sale) # ID - (Fixnum) uniquely identifies the sale # Amount - (Fixnum) the amount of the transaction, in cents (i.e., 150 would be $1.50) # Purchase_time - (Datetime) when the sale was completed # Vendor_id - (Fixnum) a reference to which vendor completed the sale # Product_id - (Fixnum) a reference to which product was sold end + + it "should have a sale amount" do + buy_a_thing.must_respond_to :amount + end + + it "should have a purchase_time" do + buy_a_thing.purchase_time.must_be_instance_of(DateTime) + end + end + + describe "self.all" do + # self.all: returns a collection of instances, representing all of the objects described in the CSV + it "should return a collection of Sale objects" do + # I'm going to start with hash with sale ID as the key. I think I'm going to need to get back out the vendor and product associated with it, but this seems good for now. + Sale.all.must_be_instance_of(Hash) + end end - # self.all: returns a collection of instances, representing all of the objects described in the CSV # self.find(id): returns an instance of the object where the value of the id field in the CSV matches the passed parameter. end end From 8db814033588653d795b60f1d727638039ca4437 Mon Sep 17 00:00:00 2001 From: Karin Kubischta Date: Wed, 7 Sep 2016 14:14:02 -0700 Subject: [PATCH 16/45] fixed DateTime conversion, tests pass --- lib/farmar_sale.rb | 3 +-- specs/sale_spec.rb | 3 ++- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/farmar_sale.rb b/lib/farmar_sale.rb index c66b8142..41fdf5fc 100644 --- a/lib/farmar_sale.rb +++ b/lib/farmar_sale.rb @@ -11,7 +11,7 @@ def initialize(sale_id, amount, purchase_time, vendor_id, product_id) # Amount - (Fixnum) the amount of the transaction, in cents (i.e., 150 would be $1.50) @amount = amount # Purchase_time - (Datetime) when the sale was completed - @purchase_time = DateTime.new(purchase_time) #BUG: purchase_time in csv is the string representation of a datetime, not the comma delimited input that Datetime is expecting. https://ruby-doc.org/stdlib-2.3.1/libdoc/date/rdoc/DateTime.html#method-c-parse + @purchase_time = DateTime.parse(purchase_time) # purchase_time in csv is the string representation of a datetime, not the comma delimited input that Datetime is expecting. https://ruby-doc.org/stdlib-2.3.1/libdoc/date/rdoc/DateTime.html#method-c-parse # Vendor_id - (Fixnum) a reference to which vendor completed the sale @vendor_id = vendor_id # Product_id - (Fixnum) a reference to which product was sold @@ -26,7 +26,6 @@ def self.all sale_id, amount, purchase_time, vendor_id, product_id = line # parallel assignment! sale_id = sale_id.to_i # need sale_id to be a fixnum amount = amount.to_i # want amount to be a fixnum also - purchase_time = DateTime.new(purchase_time) #BUG: the purchase_times are strings, that are the DateTime format, but not the format for making a new DateTime. This is currently breaking the .all test in sale_spec. vendor_id = vendor_id # want vendor_id to be a fixnum product_id = product_id # want product_id also to be a fixnum diff --git a/specs/sale_spec.rb b/specs/sale_spec.rb index 198285f1..e3c166dd 100644 --- a/specs/sale_spec.rb +++ b/specs/sale_spec.rb @@ -2,7 +2,7 @@ module FarMar describe Sale do - let(:buy_a_thing) {Sale.new(1,100,2016, 1, 1)} + let(:buy_a_thing) {Sale.new(1,100,"2013-11-07", 1, 1)} describe "#initialize" do it "should make a new instance of sale" do #write something here to test new instance of sale @@ -29,6 +29,7 @@ module FarMar # I'm going to start with hash with sale ID as the key. I think I'm going to need to get back out the vendor and product associated with it, but this seems good for now. Sale.all.must_be_instance_of(Hash) end + end # self.find(id): returns an instance of the object where the value of the id field in the CSV matches the passed parameter. end From f26f5e4c1a6327a1c623db480577f6c8db64b5da Mon Sep 17 00:00:00 2001 From: Karin Kubischta Date: Wed, 7 Sep 2016 16:03:14 -0700 Subject: [PATCH 17/45] Wrote #vendors method for Market --- lib/farmar_market.rb | 7 +++++++ specs/market_spec.rb | 16 ++++++++++++++++ specs/sale_spec.rb | 3 +-- 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/lib/farmar_market.rb b/lib/farmar_market.rb index 6d59ba83..4a5c8012 100644 --- a/lib/farmar_market.rb +++ b/lib/farmar_market.rb @@ -45,6 +45,13 @@ def self.find(id) all_markets = self.all return all_markets[id] end + + def vendors + Vendor.all.values.group_by { |vendor| vendor.market_id }[@id] + # Call Vendor.all (returns a hash of vendors) + # The Vaules of the Vendor.all hash is an array of Vendors, so want to group those on their market_id. + # Then, want only the vendors that correspond to the market instance's id. + end end end diff --git a/specs/market_spec.rb b/specs/market_spec.rb index 58f54890..43b83d4b 100644 --- a/specs/market_spec.rb +++ b/specs/market_spec.rb @@ -42,5 +42,21 @@ module FarMar Market.find(12).name.must_equal("Coxsackie Farmers' Market") end end + describe "#vendors" do + let(:coop_market) {Market.new(1,"People's Co-op Farmers Market","30th and Burnside","Portland","Multnomah","Oregon",97202)} + it "should return an array" do + coop_market.vendors.must_be_instance_of(Array) + # I'm going to make it an array of Vendors. + end + it "should return an array of Vendor objects" do + # Check that the first item is a vendor. + coop_market.vendors[0].must_be_instance_of(Vendor) + end + it "the vendor returned, should have a market_id that matches the market" do + # Checking that the objects returned have the same as the market instance I'm working with. + # This will check using the market method in Vendor. + coop_market.vendors[0].market.must_equal(coop_market) + end + end end end diff --git a/specs/sale_spec.rb b/specs/sale_spec.rb index e3c166dd..16d3226c 100644 --- a/specs/sale_spec.rb +++ b/specs/sale_spec.rb @@ -5,7 +5,6 @@ module FarMar let(:buy_a_thing) {Sale.new(1,100,"2013-11-07", 1, 1)} describe "#initialize" do it "should make a new instance of sale" do - #write something here to test new instance of sale buy_a_thing.must_be_instance_of(Sale) # ID - (Fixnum) uniquely identifies the sale # Amount - (Fixnum) the amount of the transaction, in cents (i.e., 150 would be $1.50) @@ -29,7 +28,7 @@ module FarMar # I'm going to start with hash with sale ID as the key. I think I'm going to need to get back out the vendor and product associated with it, but this seems good for now. Sale.all.must_be_instance_of(Hash) end - + end # self.find(id): returns an instance of the object where the value of the id field in the CSV matches the passed parameter. end From c98ccea072c09e4b8e31b06a77df70ab731c0894 Mon Sep 17 00:00:00 2001 From: Karin Kubischta Date: Wed, 7 Sep 2016 16:19:41 -0700 Subject: [PATCH 18/45] added #market method to vendor, tests pass --- lib/farmar_vendor.rb | 6 +++++- specs/market_spec.rb | 2 +- specs/vendor_spec.rb | 10 ++++++++++ 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/lib/farmar_vendor.rb b/lib/farmar_vendor.rb index 02627aef..60c8cda3 100644 --- a/lib/farmar_vendor.rb +++ b/lib/farmar_vendor.rb @@ -32,7 +32,11 @@ def self.all def self.find(id) # self.find(id): returns an instance of the object where the value of the id field in the CSV matches the passed parameter. all_vendors = self.all - all_vendors[id] + all_vendors[id] + end + def market + # Return the market object that corresponds to the instance's market_id. + Market.find(@market_id) end end end diff --git a/specs/market_spec.rb b/specs/market_spec.rb index 43b83d4b..c89058b4 100644 --- a/specs/market_spec.rb +++ b/specs/market_spec.rb @@ -55,7 +55,7 @@ module FarMar it "the vendor returned, should have a market_id that matches the market" do # Checking that the objects returned have the same as the market instance I'm working with. # This will check using the market method in Vendor. - coop_market.vendors[0].market.must_equal(coop_market) + coop_market.vendors[0].market_id.must_be_same_as(coop_market.id) end end end diff --git a/specs/vendor_spec.rb b/specs/vendor_spec.rb index 89d0b9d5..066850a4 100644 --- a/specs/vendor_spec.rb +++ b/specs/vendor_spec.rb @@ -40,6 +40,16 @@ module FarMar # 74,Haag-Padberg,3,17 end end + describe "#market" do + let(:feil_f) {Vendor.new(1,"Feil-Farrell",8,1)} + it "should return the market object that is associated with the market_id" do + #this seems like it'll use Market.find ? + feil_f.market.name.must_equal("People's Co-op Farmers Market") + end + it "should return a market object" do + feil_f.market.must_be_instance_of(Market) + end + end end end From a265355b6f90cd0b4cbf7e5a39627dfb7b13b69b Mon Sep 17 00:00:00 2001 From: Karin Kubischta Date: Wed, 7 Sep 2016 16:52:19 -0700 Subject: [PATCH 19/45] Moved the Vendor.by_market code out of Market's #vendors method --- lib/farmar_market.rb | 3 ++- lib/farmar_vendor.rb | 9 +++++++++ specs/market_spec.rb | 3 ++- specs/vendor_spec.rb | 12 ++++++++++++ 4 files changed, 25 insertions(+), 2 deletions(-) diff --git a/lib/farmar_market.rb b/lib/farmar_market.rb index 4a5c8012..5258db2e 100644 --- a/lib/farmar_market.rb +++ b/lib/farmar_market.rb @@ -47,10 +47,11 @@ def self.find(id) end def vendors - Vendor.all.values.group_by { |vendor| vendor.market_id }[@id] + Vendor.by_market(@id) # Call Vendor.all (returns a hash of vendors) # The Vaules of the Vendor.all hash is an array of Vendors, so want to group those on their market_id. # Then, want only the vendors that correspond to the market instance's id. + # TODO: This can be updated when I write the Vendor.by_market(market_id): returns all of the vendors with the given market_id - and I don't have to do Vendor.all. The code currently here will move to the Vendor class, and this method will call that method. Maybe I'll do that now. end end end diff --git a/lib/farmar_vendor.rb b/lib/farmar_vendor.rb index 60c8cda3..15114cb2 100644 --- a/lib/farmar_vendor.rb +++ b/lib/farmar_vendor.rb @@ -29,14 +29,23 @@ def self.all return vendors end + def self.find(id) # self.find(id): returns an instance of the object where the value of the id field in the CSV matches the passed parameter. all_vendors = self.all all_vendors[id] end + def market + #market: returns the FarMar::Market instance that is associated with this vendor using the FarMar::Vendor market_id field # Return the market object that corresponds to the instance's market_id. Market.find(@market_id) end + + def self.by_market(market_id) + # self.by_market(market_id): returns all of the vendors with the given market_id + Vendor.all.values.group_by { |vendor| vendor.market_id }[market_id] + #I don't like this -- I have an instance variable @market_id for a vendor instance, and the argument market_id that is getting passed in. + end end end diff --git a/specs/market_spec.rb b/specs/market_spec.rb index c89058b4..3e28b468 100644 --- a/specs/market_spec.rb +++ b/specs/market_spec.rb @@ -53,7 +53,8 @@ module FarMar coop_market.vendors[0].must_be_instance_of(Vendor) end it "the vendor returned, should have a market_id that matches the market" do - # Checking that the objects returned have the same as the market instance I'm working with. + # Checking that the objects returned have the same id as the market instance I'm working with. + # NOTE: It's not ACTUALLY the same object (same data, different object id), since I have created coop_market here. I wonder if I should check for the same object. I'm not sure how. # This will check using the market method in Vendor. coop_market.vendors[0].market_id.must_be_same_as(coop_market.id) end diff --git a/specs/vendor_spec.rb b/specs/vendor_spec.rb index 066850a4..6f22197b 100644 --- a/specs/vendor_spec.rb +++ b/specs/vendor_spec.rb @@ -41,6 +41,7 @@ module FarMar end end describe "#market" do + #market: returns the FarMar::Market instance that is associated with this vendor using the FarMar::Vendor market_id field let(:feil_f) {Vendor.new(1,"Feil-Farrell",8,1)} it "should return the market object that is associated with the market_id" do #this seems like it'll use Market.find ? @@ -50,6 +51,17 @@ module FarMar feil_f.market.must_be_instance_of(Market) end end + + describe "self.by_market" do + # self.by_market(market_id): returns all of the vendors with the given market_id + it "should return an array" do + Vendor.by_market(1).must_be_instance_of(Array) + end + + it "should be an array of Vendors" do + Vendor.by_market(1)[0].must_be_instance_of(Vendor) + end + end end end From 2e96f348624e651e853f14399265760f5b1b1b4b Mon Sep 17 00:00:00 2001 From: Karin Kubischta Date: Wed, 7 Sep 2016 17:01:41 -0700 Subject: [PATCH 20/45] added comments for rest of vendor methods --- lib/farmar_vendor.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/farmar_vendor.rb b/lib/farmar_vendor.rb index 15114cb2..1e945db9 100644 --- a/lib/farmar_vendor.rb +++ b/lib/farmar_vendor.rb @@ -45,7 +45,11 @@ def market def self.by_market(market_id) # self.by_market(market_id): returns all of the vendors with the given market_id Vendor.all.values.group_by { |vendor| vendor.market_id }[market_id] - #I don't like this -- I have an instance variable @market_id for a vendor instance, and the argument market_id that is getting passed in. + #I don't like this -- I have an instance variable @market_id for a vendor instance, and the argument market_id that is getting passed in. end + + #products: returns a collection of FarMar::Product instances that are associated by the FarMar::Product vendor_id field. + #sales: returns a collection of FarMar::Sale instances that are associated by the vendor_id field. + #revenue: returns the the sum of all of the vendor's sales (in cents) end end From 2197d5a323c42c7ed7669a624c8af13c135ea249 Mon Sep 17 00:00:00 2001 From: Karin Kubischta Date: Thu, 8 Sep 2016 11:09:57 -0700 Subject: [PATCH 21/45] adding notes for additional methods --- lib/farmar_product.rb | 8 +++++++- lib/farmar_vendor.rb | 6 ++++-- specs/sale_spec.rb | 5 +++++ 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/lib/farmar_product.rb b/lib/farmar_product.rb index be7e2d97..3632717d 100644 --- a/lib/farmar_product.rb +++ b/lib/farmar_product.rb @@ -30,7 +30,13 @@ def self.all def self.find(id) # self.find(id): returns an instance of the object where the value of the id field in the CSV matches the passed parameter. specific_product = Product.all - return specific_product[id] + return specific_product[id] end end end + +#vendor: returns the FarMar::Vendor instance that is associated with this vendor using the FarMar::Product vendor_id field + +#sales: returns a collection of FarMar::Sale instances that are associated using the FarMar::Sale product_id field. +#number_of_sales: returns the number of times this product has been sold. +# self.by_vendor(vendor_id): returns all of the products with the given vendor_id (will call this in the Vendor #products method. ) diff --git a/lib/farmar_vendor.rb b/lib/farmar_vendor.rb index 1e945db9..47b5a6a3 100644 --- a/lib/farmar_vendor.rb +++ b/lib/farmar_vendor.rb @@ -45,10 +45,12 @@ def market def self.by_market(market_id) # self.by_market(market_id): returns all of the vendors with the given market_id Vendor.all.values.group_by { |vendor| vendor.market_id }[market_id] - #I don't like this -- I have an instance variable @market_id for a vendor instance, and the argument market_id that is getting passed in. + #I don't like that these things are called the same thing -- I have an instance variable @market_id for a vendor instance, and the argument market_id that is getting passed in. Seems to be working though. end - #products: returns a collection of FarMar::Product instances that are associated by the FarMar::Product vendor_id field. + #products: returns a collection of FarMar::Product instances that are associated by the FarMar::Product vendor_id field. Seems to be related to the Product.by_vendor(id), which returns all the products with a given vendor_id. + + #sales: returns a collection of FarMar::Sale instances that are associated by the vendor_id field. #revenue: returns the the sum of all of the vendor's sales (in cents) end diff --git a/specs/sale_spec.rb b/specs/sale_spec.rb index 16d3226c..f47375b9 100644 --- a/specs/sale_spec.rb +++ b/specs/sale_spec.rb @@ -31,5 +31,10 @@ module FarMar end # self.find(id): returns an instance of the object where the value of the id field in the CSV matches the passed parameter. + end end + +#vendor: returns the FarMar::Vendor instance that is associated with this sale using the FarMar::Sale vendor_id field +#product: returns the FarMar::Product instance that is associated with this sale using the FarMar::Sale product_id field +# self.between(beginning_time, end_time): returns a collection of FarMar::Sale objects where the purchase time is between the two times given as arguments From acdaacfd1da16f3a8c7c192a71c7710d8b2288fd Mon Sep 17 00:00:00 2001 From: Karin Kubischta Date: Thu, 8 Sep 2016 11:17:34 -0700 Subject: [PATCH 22/45] added find(id) method to sale, passes first test --- lib/farmar_sale.rb | 4 ++++ specs/sale_spec.rb | 20 ++++++++++++++++++-- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/lib/farmar_sale.rb b/lib/farmar_sale.rb index 41fdf5fc..266570f9 100644 --- a/lib/farmar_sale.rb +++ b/lib/farmar_sale.rb @@ -34,6 +34,10 @@ def self.all return sales end + def self.find(id) + #this is going to call self.all, and then find the one with key of id + all[id] + end end end diff --git a/specs/sale_spec.rb b/specs/sale_spec.rb index f47375b9..b8affd1e 100644 --- a/specs/sale_spec.rb +++ b/specs/sale_spec.rb @@ -30,8 +30,24 @@ module FarMar end end - # self.find(id): returns an instance of the object where the value of the id field in the CSV matches the passed parameter. - + + describe "self.find(id)" do + let(:sale_21) {Sale.find(21)} + # self.find(id): returns an instance of the object where the value of the id field in the CSV matches the passed parameter. + it "should return a sale, given an id" do + sale_21.must_be_instance_of(Sale) + end + + it "should return the correct vendor id given the sale id" do + skip + sale_21.vendor_id.must_equal(4) + # 21,8963,2013-11-10 12:26:30 -0800,4,7 + end + it "should return the correct product id, given the sale id" do + skip + sale_21.product_id.must_equal(7) + end + end end end From 43cc4dc126857f656a9b9f9dd74fd3b1910665ee Mon Sep 17 00:00:00 2001 From: Karin Kubischta Date: Thu, 8 Sep 2016 11:21:00 -0700 Subject: [PATCH 23/45] fixed other sale tests for find(id) --- lib/farmar_sale.rb | 4 ++-- specs/sale_spec.rb | 12 ++++++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/farmar_sale.rb b/lib/farmar_sale.rb index 266570f9..c1c603cd 100644 --- a/lib/farmar_sale.rb +++ b/lib/farmar_sale.rb @@ -26,8 +26,8 @@ def self.all sale_id, amount, purchase_time, vendor_id, product_id = line # parallel assignment! sale_id = sale_id.to_i # need sale_id to be a fixnum amount = amount.to_i # want amount to be a fixnum also - vendor_id = vendor_id # want vendor_id to be a fixnum - product_id = product_id # want product_id also to be a fixnum + vendor_id = vendor_id.to_i # want vendor_id to be a fixnum + product_id = product_id.to_i # want product_id also to be a fixnum sales[sale_id] = self.new(sale_id, amount, purchase_time, vendor_id, product_id) end diff --git a/specs/sale_spec.rb b/specs/sale_spec.rb index b8affd1e..977c7cf8 100644 --- a/specs/sale_spec.rb +++ b/specs/sale_spec.rb @@ -32,20 +32,20 @@ module FarMar end describe "self.find(id)" do - let(:sale_21) {Sale.find(21)} + before(:all) do + @sale_21 = Sale.find(21) + end # self.find(id): returns an instance of the object where the value of the id field in the CSV matches the passed parameter. it "should return a sale, given an id" do - sale_21.must_be_instance_of(Sale) + @sale_21.must_be_instance_of(Sale) end it "should return the correct vendor id given the sale id" do - skip - sale_21.vendor_id.must_equal(4) + @sale_21.vendor_id.must_equal(4) # 21,8963,2013-11-10 12:26:30 -0800,4,7 end it "should return the correct product id, given the sale id" do - skip - sale_21.product_id.must_equal(7) + @sale_21.product_id.must_equal(7) end end end From 28b19ef258f764716c3a66b0fa6d9c2c36f9eac8 Mon Sep 17 00:00:00 2001 From: Karin Kubischta Date: Thu, 8 Sep 2016 11:39:38 -0700 Subject: [PATCH 24/45] updated comments in sale class --- lib/farmar_product.rb | 6 +++++- lib/farmar_sale.rb | 3 +-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/farmar_product.rb b/lib/farmar_product.rb index 3632717d..94acb452 100644 --- a/lib/farmar_product.rb +++ b/lib/farmar_product.rb @@ -32,10 +32,14 @@ def self.find(id) specific_product = Product.all return specific_product[id] end + + def vendor + #vendor: returns the FarMar::Vendor instance that is associated with this vendor using the FarMar::Product vendor_id field - this will work LIKE the vendor.market method. + Vendor.find(@vendor_id) + end end end -#vendor: returns the FarMar::Vendor instance that is associated with this vendor using the FarMar::Product vendor_id field #sales: returns a collection of FarMar::Sale instances that are associated using the FarMar::Sale product_id field. #number_of_sales: returns the number of times this product has been sold. diff --git a/lib/farmar_sale.rb b/lib/farmar_sale.rb index c1c603cd..5d5ae65f 100644 --- a/lib/farmar_sale.rb +++ b/lib/farmar_sale.rb @@ -35,10 +35,9 @@ def self.all return sales end def self.find(id) + # self.find(id): returns an instance of the object where the value of the id field in the CSV matches the passed parameter. #this is going to call self.all, and then find the one with key of id all[id] end end end - -# self.find(id): returns an instance of the object where the value of the id field in the CSV matches the passed parameter. From 6be2376258d3788cdc581bea7322554e0b7c5ed6 Mon Sep 17 00:00:00 2001 From: Karin Kubischta Date: Thu, 8 Sep 2016 11:40:18 -0700 Subject: [PATCH 25/45] updated sale's vendor method test --- specs/product_spec.rb | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/specs/product_spec.rb b/specs/product_spec.rb index 43b4d9c4..cf7ddac2 100644 --- a/specs/product_spec.rb +++ b/specs/product_spec.rb @@ -37,5 +37,16 @@ module FarMar # 15,Comfortable Pretzel,8 end end + describe "#vendor" do + #vendor: returns the FarMar::Vendor instance that is associated with this product using the FarMar::Product vendor_id field - this will work LIKE the vendor.market method. + # 41,Thundering Carrots,15 + let(:carrot) {Product.new(41,"Thundering Carrots",15)} + it "should return the vendor that is associated with the vendor_id" do + carrot.vendor.id.must_equal(15) + end + it "should return a Vendor object" do + carrot.vendor.must_be_instance_of(Vendor) + end + end end end From 8f1b7677f7ad91a172220588c78c19adcc485754 Mon Sep 17 00:00:00 2001 From: Karin Kubischta Date: Thu, 8 Sep 2016 12:01:13 -0700 Subject: [PATCH 26/45] added Product.by_vendor(vendor_id) method, tests pass --- lib/farmar_product.rb | 7 ++++++- specs/product_spec.rb | 11 +++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/lib/farmar_product.rb b/lib/farmar_product.rb index 94acb452..26fe56b1 100644 --- a/lib/farmar_product.rb +++ b/lib/farmar_product.rb @@ -37,10 +37,15 @@ def vendor #vendor: returns the FarMar::Vendor instance that is associated with this vendor using the FarMar::Product vendor_id field - this will work LIKE the vendor.market method. Vendor.find(@vendor_id) end + + def self.by_vendor(vendor_id) + # self.by_vendor(vendor_id): returns all of the products with the given vendor_id (will call this in the Vendor #products method. ) + all.values.group_by { |product| product.vendor_id }[vendor_id] + #I don't like that these things are called the same thing -- I have an instance variable @vendor_id for a product instance, and the argument vendor_id that is getting passed in. Seems to be working though. + end end end #sales: returns a collection of FarMar::Sale instances that are associated using the FarMar::Sale product_id field. #number_of_sales: returns the number of times this product has been sold. -# self.by_vendor(vendor_id): returns all of the products with the given vendor_id (will call this in the Vendor #products method. ) diff --git a/specs/product_spec.rb b/specs/product_spec.rb index cf7ddac2..960d467b 100644 --- a/specs/product_spec.rb +++ b/specs/product_spec.rb @@ -48,5 +48,16 @@ module FarMar carrot.vendor.must_be_instance_of(Vendor) end end + describe "self.by_vendor(vendor_id)" do + # self.by_vendor(vendor_id): returns all of the products with the given vendor_id (will call this in the Vendor #products method. ) + # This is similar to self.by_market method in vendor. + it "should return an array" do + Product.by_vendor(1).must_be_instance_of(Array) + end + + it "should be an array of Products" do + Product.by_vendor(1)[0].must_be_instance_of(Product) + end + end end end From 411e53883431159d5f3110eb538849691151c325 Mon Sep 17 00:00:00 2001 From: Karin Kubischta Date: Thu, 8 Sep 2016 12:14:27 -0700 Subject: [PATCH 27/45] wrote tests for vendor's products method --- specs/vendor_spec.rb | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/specs/vendor_spec.rb b/specs/vendor_spec.rb index 6f22197b..1117447f 100644 --- a/specs/vendor_spec.rb +++ b/specs/vendor_spec.rb @@ -20,6 +20,7 @@ module FarMar ven.name.must_equal("vendor name") end end + describe "#self.all" do # self.all: returns a collection of instances, representing all of the objects described in the CSV it "should return a collection of Vendor objects" do @@ -28,9 +29,11 @@ module FarMar end it "should contain a specific Vendor object, given the key" do Vendor.all.must_include(2) + end end + describe "self.find(id)" do - # self.find(id): returns an instance of the object where the value of the id field in the CSV matches the passed parameter. + # self.find(id): returns an instance of the object where the value of the id field in the CSV matches the passed parameter. it "should return a vendor, given an id" do Vendor.find(2).must_be_instance_of(Vendor) end @@ -40,8 +43,9 @@ module FarMar # 74,Haag-Padberg,3,17 end end + describe "#market" do - #market: returns the FarMar::Market instance that is associated with this vendor using the FarMar::Vendor market_id field + #market: returns the FarMar::Market instance that is associated with this vendor using the FarMar::Vendor market_id field let(:feil_f) {Vendor.new(1,"Feil-Farrell",8,1)} it "should return the market object that is associated with the market_id" do #this seems like it'll use Market.find ? @@ -62,7 +66,27 @@ module FarMar Vendor.by_market(1)[0].must_be_instance_of(Vendor) end end - end + describe "#products" do + # Products: returns a collection of FarMar::Product instances that are associated by the FarMar::Product vendor_id field. + # This is going to call the Products.by_vendor(vendor_id) method! + let(:some_vendor) {Vendor.new(28,"Watsica and Sons",10,8)} + it "should return an array" do + some_vendor.products.must_be_instance_of(Array) + # I'm going to make it an array of Products. + end + it "should return an array of Product objects" do + # Check that the first item is a vendor. + skip + some_vendor.products[0].must_be_instance_of(Product) + end + it "the products returned should have a vendor_id that matches the vendor" do + # Checking that the objects returned have the same id as the vendor instance I'm working with. + # NOTE: It's not ACTUALLY the same object (same data, different object id), since I have created some_vendor here in the test. + # This will check using the Vendor method in Product. + skip + some_vendor.products[0].vendor_id.must_be_same_as(some_vendor.id) + end + end end end From 68116ba943fe0d6002042959e808dc2923471691 Mon Sep 17 00:00:00 2001 From: Karin Kubischta Date: Thu, 8 Sep 2016 12:19:00 -0700 Subject: [PATCH 28/45] added Vendor's product method, tests pass --- lib/farmar_market.rb | 1 - lib/farmar_vendor.rb | 6 ++++-- specs/vendor_spec.rb | 2 -- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/lib/farmar_market.rb b/lib/farmar_market.rb index 5258db2e..9eb2d1fb 100644 --- a/lib/farmar_market.rb +++ b/lib/farmar_market.rb @@ -51,7 +51,6 @@ def vendors # Call Vendor.all (returns a hash of vendors) # The Vaules of the Vendor.all hash is an array of Vendors, so want to group those on their market_id. # Then, want only the vendors that correspond to the market instance's id. - # TODO: This can be updated when I write the Vendor.by_market(market_id): returns all of the vendors with the given market_id - and I don't have to do Vendor.all. The code currently here will move to the Vendor class, and this method will call that method. Maybe I'll do that now. end end end diff --git a/lib/farmar_vendor.rb b/lib/farmar_vendor.rb index 47b5a6a3..06d23277 100644 --- a/lib/farmar_vendor.rb +++ b/lib/farmar_vendor.rb @@ -48,9 +48,11 @@ def self.by_market(market_id) #I don't like that these things are called the same thing -- I have an instance variable @market_id for a vendor instance, and the argument market_id that is getting passed in. Seems to be working though. end - #products: returns a collection of FarMar::Product instances that are associated by the FarMar::Product vendor_id field. Seems to be related to the Product.by_vendor(id), which returns all the products with a given vendor_id. + def products + #products: returns a collection of FarMar::Product instances that are associated by the FarMar::Product vendor_id field. Seems to be related to the Product.by_vendor(id), which returns all the products with a given vendor_id. + Product.by_vendor(@id) + end - #sales: returns a collection of FarMar::Sale instances that are associated by the vendor_id field. #revenue: returns the the sum of all of the vendor's sales (in cents) end diff --git a/specs/vendor_spec.rb b/specs/vendor_spec.rb index 1117447f..0be64571 100644 --- a/specs/vendor_spec.rb +++ b/specs/vendor_spec.rb @@ -77,14 +77,12 @@ module FarMar end it "should return an array of Product objects" do # Check that the first item is a vendor. - skip some_vendor.products[0].must_be_instance_of(Product) end it "the products returned should have a vendor_id that matches the vendor" do # Checking that the objects returned have the same id as the vendor instance I'm working with. # NOTE: It's not ACTUALLY the same object (same data, different object id), since I have created some_vendor here in the test. # This will check using the Vendor method in Product. - skip some_vendor.products[0].vendor_id.must_be_same_as(some_vendor.id) end end From efa5d9925d55f912f50453810c24b7314cc217ef Mon Sep 17 00:00:00 2001 From: Karin Kubischta Date: Thu, 8 Sep 2016 12:32:09 -0700 Subject: [PATCH 29/45] Added vendor method to Sale, tests pass --- lib/farmar_sale.rb | 7 +++++++ specs/sale_spec.rb | 14 +++++++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/lib/farmar_sale.rb b/lib/farmar_sale.rb index 5d5ae65f..9e5bc84d 100644 --- a/lib/farmar_sale.rb +++ b/lib/farmar_sale.rb @@ -34,10 +34,17 @@ def self.all return sales end + def self.find(id) # self.find(id): returns an instance of the object where the value of the id field in the CSV matches the passed parameter. #this is going to call self.all, and then find the one with key of id all[id] end + + def vendor + #vendor: returns the FarMar::Vendor instance that is associated with this sale using the FarMar::Sale vendor_id field + Vendor.find(@vendor_id) + end + end end diff --git a/specs/sale_spec.rb b/specs/sale_spec.rb index 977c7cf8..dfd8c049 100644 --- a/specs/sale_spec.rb +++ b/specs/sale_spec.rb @@ -48,9 +48,21 @@ module FarMar @sale_21.product_id.must_equal(7) end end + + describe "#vendor" do + let (:new_sale) {Sale.new(13,3450,"2013-11-12 12:00:35 -0800",3,4)} + #vendor: returns the FarMar::Vendor instance that is associated with this sale using the FarMar::Sale vendor_id field + it "should return a vendor instance" do + # will this use the Vendor.find(id) method? + new_sale.vendor.must_be_instance_of(Vendor) + end + it "should be the correct vendor" do + # Same problem as other tests - it's a different instance of the object - same values, different object ID. + new_sale.vendor.name.must_equal("Breitenberg Inc") + end + end end end -#vendor: returns the FarMar::Vendor instance that is associated with this sale using the FarMar::Sale vendor_id field #product: returns the FarMar::Product instance that is associated with this sale using the FarMar::Sale product_id field # self.between(beginning_time, end_time): returns a collection of FarMar::Sale objects where the purchase time is between the two times given as arguments From aeebee35fb4508d0cbf4e4d32e517ac18bd060bd Mon Sep 17 00:00:00 2001 From: Karin Kubischta Date: Thu, 8 Sep 2016 14:27:22 -0700 Subject: [PATCH 30/45] added product method to sale, tests pass --- lib/farmar_sale.rb | 6 +++++- specs/sale_spec.rb | 16 ++++++++++++++-- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/lib/farmar_sale.rb b/lib/farmar_sale.rb index 9e5bc84d..e1a59d46 100644 --- a/lib/farmar_sale.rb +++ b/lib/farmar_sale.rb @@ -1,5 +1,5 @@ require 'csv' -require 'date' +# require 'date' module FarMar class Sale @@ -46,5 +46,9 @@ def vendor Vendor.find(@vendor_id) end + def product + Product.find(@product_id) + end + end end diff --git a/specs/sale_spec.rb b/specs/sale_spec.rb index dfd8c049..b65abae1 100644 --- a/specs/sale_spec.rb +++ b/specs/sale_spec.rb @@ -3,6 +3,7 @@ module FarMar describe Sale do let(:buy_a_thing) {Sale.new(1,100,"2013-11-07", 1, 1)} + let(:new_sale) {Sale.new(13,3450,"2013-11-12 12:00:35 -0800",3,4)} describe "#initialize" do it "should make a new instance of sale" do buy_a_thing.must_be_instance_of(Sale) @@ -50,7 +51,6 @@ module FarMar end describe "#vendor" do - let (:new_sale) {Sale.new(13,3450,"2013-11-12 12:00:35 -0800",3,4)} #vendor: returns the FarMar::Vendor instance that is associated with this sale using the FarMar::Sale vendor_id field it "should return a vendor instance" do # will this use the Vendor.find(id) method? @@ -61,8 +61,20 @@ module FarMar new_sale.vendor.name.must_equal("Breitenberg Inc") end end + + describe "#product" do + #product: returns the FarMar::Product instance that is associated with this sale using the FarMar::Sale product_id field + it "should return a product instance" do + # use the Product.find(id) method? + new_sale.product.must_be_instance_of(Product) + end + it "should be the correct product" do + #check that the name or the product_id is correct. + new_sale.product.product_id.must_equal(4) + end + + end end end -#product: returns the FarMar::Product instance that is associated with this sale using the FarMar::Sale product_id field # self.between(beginning_time, end_time): returns a collection of FarMar::Sale objects where the purchase time is between the two times given as arguments From c2d2c626727d9cb04e34cf48caa4366bcada5abf Mon Sep 17 00:00:00 2001 From: Karin Kubischta Date: Thu, 8 Sep 2016 14:37:39 -0700 Subject: [PATCH 31/45] Added product sales method, tests pass --- lib/farmar_product.rb | 6 +++++- specs/product_spec.rb | 16 +++++++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/lib/farmar_product.rb b/lib/farmar_product.rb index 26fe56b1..32b943c0 100644 --- a/lib/farmar_product.rb +++ b/lib/farmar_product.rb @@ -43,9 +43,13 @@ def self.by_vendor(vendor_id) all.values.group_by { |product| product.vendor_id }[vendor_id] #I don't like that these things are called the same thing -- I have an instance variable @vendor_id for a product instance, and the argument vendor_id that is getting passed in. Seems to be working though. end + + def sales + #sales: returns a collection of FarMar::Sale instances that are associated using the FarMar::Sale product_id field. + Sale.all.values.group_by {|sale| sale.product_id}[@product_id] + end end end -#sales: returns a collection of FarMar::Sale instances that are associated using the FarMar::Sale product_id field. #number_of_sales: returns the number of times this product has been sold. diff --git a/specs/product_spec.rb b/specs/product_spec.rb index 960d467b..4f18aa62 100644 --- a/specs/product_spec.rb +++ b/specs/product_spec.rb @@ -3,6 +3,8 @@ module FarMar describe Product do let(:pro_deal) {Product.new(1, "product", 123)} + let(:carrot) {Product.new(41,"Thundering Carrots",15)} + describe "#initialize" do it "should make a new instance of product" do #write test to check that a new product instance was created. @@ -40,7 +42,6 @@ module FarMar describe "#vendor" do #vendor: returns the FarMar::Vendor instance that is associated with this product using the FarMar::Product vendor_id field - this will work LIKE the vendor.market method. # 41,Thundering Carrots,15 - let(:carrot) {Product.new(41,"Thundering Carrots",15)} it "should return the vendor that is associated with the vendor_id" do carrot.vendor.id.must_equal(15) end @@ -59,5 +60,18 @@ module FarMar Product.by_vendor(1)[0].must_be_instance_of(Product) end end + describe "#sales" do + #sales: returns a collection of FarMar::Sale instances that are associated using the FarMar::Sale product_id field. + it "should return an Array" do + # going to collect sales (using Sale.all), group by sale.product_id, grab only the array from this instance's product_id. + carrot.sales.must_be_instance_of(Array) + end + it "should return an array of Sales" do + carrot.sales[0].must_be_instance_of(Sale) + end + it "should return sales whose product_id is the same as this product_id" do + carrot.sales[0].product_id.must_equal(carrot.product_id) + end + end end end From 2c0b783db06a6ee61edf7a16550398676c2639bc Mon Sep 17 00:00:00 2001 From: Karin Kubischta Date: Thu, 8 Sep 2016 14:56:59 -0700 Subject: [PATCH 32/45] added tests for vendor sales method --- specs/vendor_spec.rb | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/specs/vendor_spec.rb b/specs/vendor_spec.rb index 0be64571..4f0c5912 100644 --- a/specs/vendor_spec.rb +++ b/specs/vendor_spec.rb @@ -3,6 +3,8 @@ module FarMar describe Vendor do let(:ven) {Vendor.new(1, "vendor name", 1, 1)} + let(:some_vendor) {Vendor.new(28,"Watsica and Sons",10,8)} + describe "#initialize" do it "should make a new instance of vendor" do #write something here to test new instance of vendor @@ -70,7 +72,6 @@ module FarMar describe "#products" do # Products: returns a collection of FarMar::Product instances that are associated by the FarMar::Product vendor_id field. # This is going to call the Products.by_vendor(vendor_id) method! - let(:some_vendor) {Vendor.new(28,"Watsica and Sons",10,8)} it "should return an array" do some_vendor.products.must_be_instance_of(Array) # I'm going to make it an array of Products. @@ -86,5 +87,28 @@ module FarMar some_vendor.products[0].vendor_id.must_be_same_as(some_vendor.id) end end + describe "#sales" do + #sales: returns a collection of FarMar::Sale instances that are associated by the vendor_id field. + # for this vendor object, I should get the list of products associated with it, then call product.sales for each of those products, which should return a collection of sales (probably as a hash, keyed off of product_id). + it "should return a Hash" do + some_vendor.sales.must_be_instance_of(Hash) + end + it "should return a Hash where the keys are Products" do + skip + some_vendor.sales.keys[0].must_be_instance_of(Product) + end + it "should return a Hash where the values are an array of Sales" do + skip + some_vendor.sales.values[0].must_be_instance_of(Array) + end + it "elements within the value Arrays should be Sale objects" do + skip + some_vendor.sales.values[0][0].must_be_instance_of(Sale) + end + it "should return Sales where the vendor_id is the same as it's vendor_id" do + skip + some_vendor.sales.values[0][0].vendor_id.must_equal(some_vendor.id) + end + end end end From 7bae167c667348e45be0360fdbda49b441a972fc Mon Sep 17 00:00:00 2001 From: Karin Kubischta Date: Thu, 8 Sep 2016 15:20:10 -0700 Subject: [PATCH 33/45] wrote vendor sales method, accounts for if there are no sales for a product --- lib/farmar_product.rb | 3 ++- lib/farmar_vendor.rb | 18 ++++++++++++++++-- specs/vendor_spec.rb | 5 +---- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/lib/farmar_product.rb b/lib/farmar_product.rb index 32b943c0..f7c62c02 100644 --- a/lib/farmar_product.rb +++ b/lib/farmar_product.rb @@ -45,8 +45,9 @@ def self.by_vendor(vendor_id) end def sales - #sales: returns a collection of FarMar::Sale instances that are associated using the FarMar::Sale product_id field. + #sales: returns an array of FarMar::Sale instances that are associated using the FarMar::Sale product_id field. Sale.all.values.group_by {|sale| sale.product_id}[@product_id] + #NOTE: Is it possible that a given product has NO sales? Seems like yes. end end end diff --git a/lib/farmar_vendor.rb b/lib/farmar_vendor.rb index 06d23277..8c30e16c 100644 --- a/lib/farmar_vendor.rb +++ b/lib/farmar_vendor.rb @@ -49,11 +49,25 @@ def self.by_market(market_id) end def products - #products: returns a collection of FarMar::Product instances that are associated by the FarMar::Product vendor_id field. Seems to be related to the Product.by_vendor(id), which returns all the products with a given vendor_id. + #products: returns an array of FarMar::Product instances that are associated by the FarMar::Product vendor_id field. Seems to be related to the Product.by_vendor(id), which returns all the products with a given vendor_id. Product.by_vendor(@id) end - #sales: returns a collection of FarMar::Sale instances that are associated by the vendor_id field. + def sales + #sales: returns a collection of FarMar::Sale instances that are associated by the vendor_id field. + # for this vendor object, I should get the list of products associated with it, then call product.sales for each of those products, which should return a collection of sales (probably as a hash, keyed off of product_id). + sales_hash = {} + + products.each do |product| #for each product do the following. + # it is possible for there to be no sales for a given product + unless product.sales == nil + sales_hash[product] = product.sales + end + end + + return sales_hash + end + #revenue: returns the the sum of all of the vendor's sales (in cents) end end diff --git a/specs/vendor_spec.rb b/specs/vendor_spec.rb index 4f0c5912..a8df3b3d 100644 --- a/specs/vendor_spec.rb +++ b/specs/vendor_spec.rb @@ -94,19 +94,16 @@ module FarMar some_vendor.sales.must_be_instance_of(Hash) end it "should return a Hash where the keys are Products" do - skip some_vendor.sales.keys[0].must_be_instance_of(Product) end it "should return a Hash where the values are an array of Sales" do - skip some_vendor.sales.values[0].must_be_instance_of(Array) end it "elements within the value Arrays should be Sale objects" do - skip + #it is possible I'll have an empty array if there are no sales for a given product. some_vendor.sales.values[0][0].must_be_instance_of(Sale) end it "should return Sales where the vendor_id is the same as it's vendor_id" do - skip some_vendor.sales.values[0][0].vendor_id.must_equal(some_vendor.id) end end From 88075962b0f053fc45383764f7e2a48a1a55df4a Mon Sep 17 00:00:00 2001 From: Karin Kubischta Date: Thu, 8 Sep 2016 15:26:13 -0700 Subject: [PATCH 34/45] added number_of_sales method, tests pass --- lib/farmar_product.rb | 5 +++++ specs/product_spec.rb | 7 +++++++ 2 files changed, 12 insertions(+) diff --git a/lib/farmar_product.rb b/lib/farmar_product.rb index f7c62c02..1c66c74b 100644 --- a/lib/farmar_product.rb +++ b/lib/farmar_product.rb @@ -49,6 +49,11 @@ def sales Sale.all.values.group_by {|sale| sale.product_id}[@product_id] #NOTE: Is it possible that a given product has NO sales? Seems like yes. end + + def number_of_sales + #number_of_sales: returns the number of times this product has been sold. + sales.length + end end end diff --git a/specs/product_spec.rb b/specs/product_spec.rb index 4f18aa62..78ad28c8 100644 --- a/specs/product_spec.rb +++ b/specs/product_spec.rb @@ -73,5 +73,12 @@ module FarMar carrot.sales[0].product_id.must_equal(carrot.product_id) end end + describe "#number_of_sales" do + # number_of_sales: returns the number of times this product has been sold. + # will call #sales to get the list of sales for the product, and should look at the length. + it "must return the number of sales for the product" do + carrot.number_of_sales.must_equal(5) #replace with the number. + end + end end end From addd1c9c792f9db04962c48c98e7b4c0062864a8 Mon Sep 17 00:00:00 2001 From: Karin Kubischta Date: Thu, 8 Sep 2016 16:30:28 -0700 Subject: [PATCH 35/45] added vendor revenue method, tests pass --- lib/farmar_vendor.rb | 20 +++++++++++++++++++- specs/vendor_spec.rb | 14 ++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/lib/farmar_vendor.rb b/lib/farmar_vendor.rb index 8c30e16c..c5803a5d 100644 --- a/lib/farmar_vendor.rb +++ b/lib/farmar_vendor.rb @@ -68,6 +68,24 @@ def sales return sales_hash end - #revenue: returns the the sum of all of the vendor's sales (in cents) + def revenue + #revenue: returns the the sum of all of the vendor's sales (in cents) + #probably uses the #sales method, grabs the amount associated with each sale, and then reduces (:+) them to a sum? + #sales returns a hash with key value pairs of product: [array of sales], so I'll want to iterate through each array of sales, grab the sale.amount, make a collection of those, and reduce them to the sum. Then for each product, I'll have a a total sale amount. (maybe use a map here), and I can then reduce again to get the total revenue. + revenue_by_product = sales.map do |k, v| + #v is an array of sales + #want to get an array of sale.amounts for it. + amounts = [] + v.each do |sale| + amounts << sale.amount + end + + # taking the array of sale.amounts, I'm going to reduce to get revenue for this product. The result should be an array of revenues per product. (number of amounts in this array should equal the same number of products for the vendor where there have been a sale.) + # at some point in the future, I might want to keep the product as the key, but for right now I don't think I care. + amounts.reduce(:+) + end + + revenue_by_product.reduce(:+) + end end end diff --git a/specs/vendor_spec.rb b/specs/vendor_spec.rb index a8df3b3d..3d819dee 100644 --- a/specs/vendor_spec.rb +++ b/specs/vendor_spec.rb @@ -107,5 +107,19 @@ module FarMar some_vendor.sales.values[0][0].vendor_id.must_equal(some_vendor.id) end end + + describe "#revenue" do + #revenue: returns the the sum of all of the vendor's sales (in cents) + #probably uses the #sales method, grabs the amount associated with each sale, and then reduces (:+) them to a sum? + #sales returns a hash with key value pairs of product: [array of sales], so I'll want to iterate through each array of sales, grab the sale.amount, make a collection of those, and reduce them to the sum. Then for each product, I'll have a a total sale amount. (maybe use a map here), and I can then reduce again to get the total revenue. + it "should return a Fixnum" do + some_vendor.revenue.must_be_instance_of(Fixnum) + end + + it "should return the total revenue from all the products for the vendor" do + #in cents + some_vendor.revenue.must_equal(4589) + end + end end end From 0f031b9afc3f23f52996626283f495155628c3ce Mon Sep 17 00:00:00 2001 From: Karin Kubischta Date: Thu, 8 Sep 2016 16:31:06 -0700 Subject: [PATCH 36/45] fixed some of the comments --- lib/farmar_sale.rb | 2 +- specs/vendor_spec.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/farmar_sale.rb b/lib/farmar_sale.rb index e1a59d46..f7edb797 100644 --- a/lib/farmar_sale.rb +++ b/lib/farmar_sale.rb @@ -47,8 +47,8 @@ def vendor end def product + #product: returns the FarMar::Product instance that is associated with this sale using the FarMar::Sale product_id field Product.find(@product_id) end - end end diff --git a/specs/vendor_spec.rb b/specs/vendor_spec.rb index 3d819dee..f097aefe 100644 --- a/specs/vendor_spec.rb +++ b/specs/vendor_spec.rb @@ -89,7 +89,7 @@ module FarMar end describe "#sales" do #sales: returns a collection of FarMar::Sale instances that are associated by the vendor_id field. - # for this vendor object, I should get the list of products associated with it, then call product.sales for each of those products, which should return a collection of sales (probably as a hash, keyed off of product_id). + # for this vendor object, I should get the list of products associated with it, then call product.sales for each of those products, which should return a collection of sales (as a hash, keyed off of product_id). it "should return a Hash" do some_vendor.sales.must_be_instance_of(Hash) end From 030c96ef87d03525a3615f96222674e9af50188c Mon Sep 17 00:00:00 2001 From: Karin Kubischta Date: Fri, 9 Sep 2016 10:41:17 -0700 Subject: [PATCH 37/45] notes about filter vs group_by --- lib/farmar_product.rb | 1 + lib/farmar_vendor.rb | 1 + 2 files changed, 2 insertions(+) diff --git a/lib/farmar_product.rb b/lib/farmar_product.rb index 1c66c74b..fc76223d 100644 --- a/lib/farmar_product.rb +++ b/lib/farmar_product.rb @@ -42,6 +42,7 @@ def self.by_vendor(vendor_id) # self.by_vendor(vendor_id): returns all of the products with the given vendor_id (will call this in the Vendor #products method. ) all.values.group_by { |product| product.vendor_id }[vendor_id] #I don't like that these things are called the same thing -- I have an instance variable @vendor_id for a product instance, and the argument vendor_id that is getting passed in. Seems to be working though. + #TODO: rather than using group_by and getting all the product groups by vendor_id, and then throwing away all the groups except the one I care about, research using "filter". end def sales diff --git a/lib/farmar_vendor.rb b/lib/farmar_vendor.rb index c5803a5d..65ce5f67 100644 --- a/lib/farmar_vendor.rb +++ b/lib/farmar_vendor.rb @@ -46,6 +46,7 @@ def self.by_market(market_id) # self.by_market(market_id): returns all of the vendors with the given market_id Vendor.all.values.group_by { |vendor| vendor.market_id }[market_id] #I don't like that these things are called the same thing -- I have an instance variable @market_id for a vendor instance, and the argument market_id that is getting passed in. Seems to be working though. + #TODO: rather than using group_by and getting all the vendor groups by market_id, and then throwing away all the groups except the one I care about, research using "filter". end def products From ca2484a6f7724facfd065b6ab37601ef295cd808 Mon Sep 17 00:00:00 2001 From: Karin Kubischta Date: Fri, 9 Sep 2016 11:00:50 -0700 Subject: [PATCH 38/45] added tests for Sale.between(b,e) method --- specs/sale_spec.rb | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/specs/sale_spec.rb b/specs/sale_spec.rb index b65abae1..7a5886ec 100644 --- a/specs/sale_spec.rb +++ b/specs/sale_spec.rb @@ -72,9 +72,32 @@ module FarMar #check that the name or the product_id is correct. new_sale.product.product_id.must_equal(4) end + end + + describe "self.between(begin,end)" do + let(:beginning_time) {DateTime.new()} + let(:end_time) {DateTime.new()} + let(:some_sales) {Sale.between(beginning_time, end_time)} + # self.between(beginning_time, end_time): returns a collection of FarMar::Sale objects where the purchase time is between the two times given as arguments + it "should return an array" do + #should return an array of sale objects + some_sales.must_be_instance_of(Array) + end + + it "should return an array of Sale objects" do + skip + some_sales[0].must_be_instance_of(Sale) + end + it "should have Sales that are before end_time" do + skip + some_sales[0].purchase_time.must_be :<=, end_time + end + + it "should have Sales that are after beginning_time" do + skip + some_sales[0].purchase_time.must_be :>=, beginning_time + end end end end - -# self.between(beginning_time, end_time): returns a collection of FarMar::Sale objects where the purchase time is between the two times given as arguments From 9a37fa5c1bfcb6229407d956957754fddc4a7c8f Mon Sep 17 00:00:00 2001 From: Karin Kubischta Date: Fri, 9 Sep 2016 11:19:29 -0700 Subject: [PATCH 39/45] wrote Sale.between(b,e) method, tests pass --- lib/farmar_sale.rb | 7 +++++++ specs/sale_spec.rb | 25 +++++++++++++++---------- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/lib/farmar_sale.rb b/lib/farmar_sale.rb index f7edb797..e7206b33 100644 --- a/lib/farmar_sale.rb +++ b/lib/farmar_sale.rb @@ -50,5 +50,12 @@ def product #product: returns the FarMar::Product instance that is associated with this sale using the FarMar::Sale product_id field Product.find(@product_id) end + + def self.between(beginning_time, end_time) + between_sales = Sale.all.select do |key, value| + value.purchase_time >= beginning_time && value.purchase_time <= end_time + end + between_sales + end end end diff --git a/specs/sale_spec.rb b/specs/sale_spec.rb index 7a5886ec..0b6fe300 100644 --- a/specs/sale_spec.rb +++ b/specs/sale_spec.rb @@ -75,28 +75,33 @@ module FarMar end describe "self.between(begin,end)" do - let(:beginning_time) {DateTime.new()} - let(:end_time) {DateTime.new()} + let(:beginning_time) {DateTime.parse("2013-11-13 01:48:15 -0800")} + let(:end_time) {DateTime.parse("2013-11-13 01:49:37 -0800")} let(:some_sales) {Sale.between(beginning_time, end_time)} # self.between(beginning_time, end_time): returns a collection of FarMar::Sale objects where the purchase time is between the two times given as arguments - it "should return an array" do + it "should return an Hash" do #should return an array of sale objects - some_sales.must_be_instance_of(Array) + some_sales.must_be_instance_of(Hash) + end + + it "should return a Hash where the keys are Sale IDs" do + some_sales.keys[0].must_be_instance_of(Fixnum) + end + + it "should return the correct Sale ID as the key" do + some_sales[3].sale_id.must_equal(3) end it "should return an array of Sale objects" do - skip - some_sales[0].must_be_instance_of(Sale) + some_sales.values[0].must_be_instance_of(Sale) end it "should have Sales that are before end_time" do - skip - some_sales[0].purchase_time.must_be :<=, end_time + some_sales.values[0].purchase_time.must_be :<=, end_time end it "should have Sales that are after beginning_time" do - skip - some_sales[0].purchase_time.must_be :>=, beginning_time + some_sales.values[0].purchase_time.must_be :>=, beginning_time end end end From 1bd8d8d17bb52589210b9d42343809818eb8b321 Mon Sep 17 00:00:00 2001 From: Karin Kubischta Date: Fri, 9 Sep 2016 11:40:14 -0700 Subject: [PATCH 40/45] primary requirements complete --- lib/farmar_product.rb | 2 +- lib/farmar_vendor.rb | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/farmar_product.rb b/lib/farmar_product.rb index fc76223d..beef16b6 100644 --- a/lib/farmar_product.rb +++ b/lib/farmar_product.rb @@ -40,7 +40,7 @@ def vendor def self.by_vendor(vendor_id) # self.by_vendor(vendor_id): returns all of the products with the given vendor_id (will call this in the Vendor #products method. ) - all.values.group_by { |product| product.vendor_id }[vendor_id] + all.values.select { |product| product.vendor_id == vendor_id } #I don't like that these things are called the same thing -- I have an instance variable @vendor_id for a product instance, and the argument vendor_id that is getting passed in. Seems to be working though. #TODO: rather than using group_by and getting all the product groups by vendor_id, and then throwing away all the groups except the one I care about, research using "filter". end diff --git a/lib/farmar_vendor.rb b/lib/farmar_vendor.rb index 65ce5f67..f137695e 100644 --- a/lib/farmar_vendor.rb +++ b/lib/farmar_vendor.rb @@ -44,9 +44,9 @@ def market def self.by_market(market_id) # self.by_market(market_id): returns all of the vendors with the given market_id - Vendor.all.values.group_by { |vendor| vendor.market_id }[market_id] + Vendor.all.values.select { |vendor| vendor.market_id == market_id } + #I don't like that these things are called the same thing -- I have an instance variable @market_id for a vendor instance, and the argument market_id that is getting passed in. Seems to be working though. - #TODO: rather than using group_by and getting all the vendor groups by market_id, and then throwing away all the groups except the one I care about, research using "filter". end def products From 17908d24e2c7f53852e7b525edb0debea38cfe4e Mon Sep 17 00:00:00 2001 From: Karin Kubischta Date: Fri, 9 Sep 2016 12:32:51 -0700 Subject: [PATCH 41/45] prefered vendor method works, slowly. --- lib/farmar_market.rb | 7 +++++++ specs/market_spec.rb | 17 +++++++++++++++-- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/lib/farmar_market.rb b/lib/farmar_market.rb index 9eb2d1fb..a82a4917 100644 --- a/lib/farmar_market.rb +++ b/lib/farmar_market.rb @@ -52,6 +52,13 @@ def vendors # The Vaules of the Vendor.all hash is an array of Vendors, so want to group those on their market_id. # Then, want only the vendors that correspond to the market instance's id. end + + def prefered_vendor + #prefered_vendor: returns the vendor with the highest revenue + # I'm going to start with an array of all vendors for the MARKET. (market.vendors), then I want to calculate the revenue for each vendor, and store it with the corresponding vendor (hash, where revenue is the key, value is an array of vendors with that revenue, in case there are more than one). then, call .max on the keys of the hash, get the key, call the value. Or I can just use max_by. + vendors.max_by { |vendor| vendor.revenue } + + end end end diff --git a/specs/market_spec.rb b/specs/market_spec.rb index 3e28b468..30b3c50b 100644 --- a/specs/market_spec.rb +++ b/specs/market_spec.rb @@ -3,6 +3,7 @@ module FarMar describe Market do let(:market_test) {Market.new(1, "name", "address", "city", "county", "ST", 98017)} + let(:coop_market) {Market.new(1,"People's Co-op Farmers Market","30th and Burnside","Portland","Multnomah","Oregon",97202)} describe "#initialize" do it "should make a new instance of market" do # this just tests the basic "I can make a new instance of this class." @@ -43,7 +44,6 @@ module FarMar end end describe "#vendors" do - let(:coop_market) {Market.new(1,"People's Co-op Farmers Market","30th and Burnside","Portland","Multnomah","Oregon",97202)} it "should return an array" do coop_market.vendors.must_be_instance_of(Array) # I'm going to make it an array of Vendors. @@ -54,10 +54,23 @@ module FarMar end it "the vendor returned, should have a market_id that matches the market" do # Checking that the objects returned have the same id as the market instance I'm working with. - # NOTE: It's not ACTUALLY the same object (same data, different object id), since I have created coop_market here. I wonder if I should check for the same object. I'm not sure how. + # NOTE: It's not ACTUALLY the same object (same data, different object id), since I have created coop_market here. I wonder if I should check for the same object. I'm not sure how. # This will check using the market method in Vendor. coop_market.vendors[0].market_id.must_be_same_as(coop_market.id) end end + describe "#prefered_vendor" do + + #prefered_vendor: returns the vendor with the highest revenue + # I'm going to start with an array of all vendors for the MARKET. (market.vendors), then I want to calculate the revenue for each vendor, and store it with the corresponding vendor (hash, where revenue is the key, value is an array of vendors with that revenue, in case there are more than one). then, call .max on the keys of the hash, get the key, call the value. + it "should return a Vendor" do + coop_market.prefered_vendor.must_be_instance_of(Vendor) + end + it "should return the vendor with highest revenue" do + #not sure how to check this + skip + + end + end end end From ba35b1c8a624edff027ee33521069ce6c142022c Mon Sep 17 00:00:00 2001 From: Karin Kubischta Date: Fri, 9 Sep 2016 15:41:12 -0700 Subject: [PATCH 42/45] updated notes around products without sales --- lib/farmar_market.rb | 2 +- lib/farmar_product.rb | 2 +- lib/farmar_vendor.rb | 1 + 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/farmar_market.rb b/lib/farmar_market.rb index a82a4917..e10a4719 100644 --- a/lib/farmar_market.rb +++ b/lib/farmar_market.rb @@ -55,7 +55,7 @@ def vendors def prefered_vendor #prefered_vendor: returns the vendor with the highest revenue - # I'm going to start with an array of all vendors for the MARKET. (market.vendors), then I want to calculate the revenue for each vendor, and store it with the corresponding vendor (hash, where revenue is the key, value is an array of vendors with that revenue, in case there are more than one). then, call .max on the keys of the hash, get the key, call the value. Or I can just use max_by. + # I'm going to start with an array of all vendors for the MARKET. (market.vendors), then I want to calculate the revenue for each vendor, and store it with the corresponding vendor (hash, where revenue is the key, value is an array of vendors with that revenue, in case there are more than one). then, call .max on the keys of the hash, get the key, call the value. Or I can just use max_by. vendors.max_by { |vendor| vendor.revenue } end diff --git a/lib/farmar_product.rb b/lib/farmar_product.rb index beef16b6..973d4872 100644 --- a/lib/farmar_product.rb +++ b/lib/farmar_product.rb @@ -48,7 +48,7 @@ def self.by_vendor(vendor_id) def sales #sales: returns an array of FarMar::Sale instances that are associated using the FarMar::Sale product_id field. Sale.all.values.group_by {|sale| sale.product_id}[@product_id] - #NOTE: Is it possible that a given product has NO sales? Seems like yes. + #NOTE: It possible that a given product has NO sales. end def number_of_sales diff --git a/lib/farmar_vendor.rb b/lib/farmar_vendor.rb index f137695e..87bbf9f7 100644 --- a/lib/farmar_vendor.rb +++ b/lib/farmar_vendor.rb @@ -62,6 +62,7 @@ def sales products.each do |product| #for each product do the following. # it is possible for there to be no sales for a given product unless product.sales == nil + #product.sales will return nil for products that have no sales. sales_hash[product] = product.sales end end From d71c9712292faebeaaf81ab1497b561d0972db98 Mon Sep 17 00:00:00 2001 From: Karin Kubischta Date: Fri, 9 Sep 2016 15:44:14 -0700 Subject: [PATCH 43/45] switched product.sales from group_by to select --- lib/farmar_product.rb | 5 +++-- lib/farmar_vendor.rb | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/farmar_product.rb b/lib/farmar_product.rb index 973d4872..94f8f54e 100644 --- a/lib/farmar_product.rb +++ b/lib/farmar_product.rb @@ -47,8 +47,9 @@ def self.by_vendor(vendor_id) def sales #sales: returns an array of FarMar::Sale instances that are associated using the FarMar::Sale product_id field. - Sale.all.values.group_by {|sale| sale.product_id}[@product_id] - #NOTE: It possible that a given product has NO sales. + Sale.all.values.select {|sale| sale.product_id == @product_id} + # Sale.all.values.group_by {|sale| sale.product_id}[@product_id] + #NOTE: It possible that a given product has NO sales. end def number_of_sales diff --git a/lib/farmar_vendor.rb b/lib/farmar_vendor.rb index 87bbf9f7..d93ee7b8 100644 --- a/lib/farmar_vendor.rb +++ b/lib/farmar_vendor.rb @@ -61,8 +61,9 @@ def sales products.each do |product| #for each product do the following. # it is possible for there to be no sales for a given product - unless product.sales == nil - #product.sales will return nil for products that have no sales. + # unless product.sales == nil + unless product.sales == [] + #product.sales will return nil for products that have no sales. sales_hash[product] = product.sales end end From c4124d316032b91b7ec8155141defb9f1c3fe887 Mon Sep 17 00:00:00 2001 From: Karin Kubischta Date: Fri, 9 Sep 2016 15:45:10 -0700 Subject: [PATCH 44/45] oops, now fixed comments around vendor sales method --- lib/farmar_vendor.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/farmar_vendor.rb b/lib/farmar_vendor.rb index d93ee7b8..0fc2be87 100644 --- a/lib/farmar_vendor.rb +++ b/lib/farmar_vendor.rb @@ -63,7 +63,7 @@ def sales # it is possible for there to be no sales for a given product # unless product.sales == nil unless product.sales == [] - #product.sales will return nil for products that have no sales. + #product.sales will return an empty array for products that have no sales. sales_hash[product] = product.sales end end From 5b4f709e318a5afcca24320e3429cff16f0e023a Mon Sep 17 00:00:00 2001 From: Karin Kubischta Date: Mon, 12 Sep 2016 21:38:04 -0700 Subject: [PATCH 45/45] Updating Sale with a class variable for .all method --- lib/farmar_sale.rb | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/lib/farmar_sale.rb b/lib/farmar_sale.rb index e7206b33..dadc6fbe 100644 --- a/lib/farmar_sale.rb +++ b/lib/farmar_sale.rb @@ -3,6 +3,7 @@ module FarMar class Sale + @@all_sales = nil attr_reader :sale_id, :amount, :vendor_id, :product_id, :purchase_time def initialize(sale_id, amount, purchase_time, vendor_id, product_id) # sale_id, amount, purchase_time, vendor_id, product_id) @@ -20,19 +21,22 @@ def initialize(sale_id, amount, purchase_time, vendor_id, product_id) def self.all # self.all: returns a collection of instances, representing all of the objects described in the CSV - sales = {} + if @@all_sales == nil - CSV.read("support/sales.csv").each do |line| - sale_id, amount, purchase_time, vendor_id, product_id = line # parallel assignment! - sale_id = sale_id.to_i # need sale_id to be a fixnum - amount = amount.to_i # want amount to be a fixnum also - vendor_id = vendor_id.to_i # want vendor_id to be a fixnum - product_id = product_id.to_i # want product_id also to be a fixnum + @@all_sales = {} - sales[sale_id] = self.new(sale_id, amount, purchase_time, vendor_id, product_id) + CSV.read("support/sales.csv").each do |line| + sale_id, amount, purchase_time, vendor_id, product_id = line # parallel assignment! + sale_id = sale_id.to_i # need sale_id to be a fixnum + amount = amount.to_i # want amount to be a fixnum also + vendor_id = vendor_id.to_i # want vendor_id to be a fixnum + product_id = product_id.to_i # want product_id also to be a fixnum + + @@all_sales[sale_id] = self.new(sale_id, amount, purchase_time, vendor_id, product_id) + end end - return sales + return @@all_sales end def self.find(id)