From 81cfa04297477990d8d89032270e827c8b27a31d Mon Sep 17 00:00:00 2001 From: Danielle Schrimmer Date: Tue, 6 Sep 2016 12:34:15 -0700 Subject: [PATCH 01/33] File structure set up --- .gitignore | 1 + Rakefile | 7 +++++++ far_mar.rb | 10 ++++++++++ lib/farmar_market.rb | 28 ++++++++++++++++++++++++++++ lib/farmar_product.rb | 8 ++++++++ lib/farmar_sale.rb | 8 ++++++++ lib/farmar_vendor.rb | 8 ++++++++ specs/farmar_market_spec.rb | 19 +++++++++++++++++++ specs/farmar_product_spec.rb | 19 +++++++++++++++++++ specs/farmar_sale_spec.rb | 19 +++++++++++++++++++ specs/farmar_vendor_spec.rb | 19 +++++++++++++++++++ specs/spec_helper.rb | 12 ++++++++++++ 12 files changed, 158 insertions(+) create mode 100644 .gitignore 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/farmar_market_spec.rb create mode 100644 specs/farmar_product_spec.rb create mode 100644 specs/farmar_sale_spec.rb create mode 100644 specs/farmar_vendor_spec.rb create mode 100644 specs/spec_helper.rb diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..404abb22 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +coverage/ diff --git a/Rakefile b/Rakefile new file mode 100644 index 00000000..82e5a661 --- /dev/null +++ b/Rakefile @@ -0,0 +1,7 @@ +require 'rake/testtask' + +Rake::TestTask.new do |t| + t.test_files = FileList['specs/*_spec.rb'] + end + +task default: :test diff --git a/far_mar.rb b/far_mar.rb new file mode 100644 index 00000000..718bb16a --- /dev/null +++ b/far_mar.rb @@ -0,0 +1,10 @@ +require 'csv' + +module FarMar + require_relative 'lib/farmar_market' + require_relative 'lib/farmar_vendor' + require_relative 'lib/farmar_product' + require_relative 'lib/farmar_sale' + + +end diff --git a/lib/farmar_market.rb b/lib/farmar_market.rb new file mode 100644 index 00000000..ba8cf4bd --- /dev/null +++ b/lib/farmar_market.rb @@ -0,0 +1,28 @@ +require 'csv' + +class Market + + attr_accessor :market_hash, :id, :name, :city, :county, :state, :zip + + def initialize + end + + # def initialize(id, name, address, city, county, state, zip) + # @id = id + # @name = name + # @city = city + # @county = county + # @state = state + # @zip = zip + # end + + # def self.all + # markets = {} + # CSV.read('../support/markets.csv').each do |line| + # markets = self.new(line[0].to_i, line[1], line[2], line[3], line[4], line[5], line[6]) + # # markets = {(line[0].to_i): name: line[1], address: line[2], city: line[3], county: line[4], state: line[5], zip: line[6]} + # # ) + # end + # end + +end diff --git a/lib/farmar_product.rb b/lib/farmar_product.rb new file mode 100644 index 00000000..0b330ab0 --- /dev/null +++ b/lib/farmar_product.rb @@ -0,0 +1,8 @@ +require 'csv' + + +class Product + + + +end diff --git a/lib/farmar_sale.rb b/lib/farmar_sale.rb new file mode 100644 index 00000000..988ce2fa --- /dev/null +++ b/lib/farmar_sale.rb @@ -0,0 +1,8 @@ +require 'csv' + + +class Sale + + + +end diff --git a/lib/farmar_vendor.rb b/lib/farmar_vendor.rb new file mode 100644 index 00000000..e04a838c --- /dev/null +++ b/lib/farmar_vendor.rb @@ -0,0 +1,8 @@ +require 'csv' + + +class Vendor + + + +end diff --git a/specs/farmar_market_spec.rb b/specs/farmar_market_spec.rb new file mode 100644 index 00000000..b0595d8f --- /dev/null +++ b/specs/farmar_market_spec.rb @@ -0,0 +1,19 @@ +require_relative 'spec_helper' +# require '../lib/farmar_market' + +describe Market do + + describe "#initialize" do + it "should create an instance of Market" do + m = Market.new + m.must_be_instance_of(Market) + end + + # it "should know about associated data file" do + # m = FarMar::Market.new + # m.all.class.must_equal(Hash) + # end + end + + +end diff --git a/specs/farmar_product_spec.rb b/specs/farmar_product_spec.rb new file mode 100644 index 00000000..bd858ac5 --- /dev/null +++ b/specs/farmar_product_spec.rb @@ -0,0 +1,19 @@ +require_relative 'spec_helper' +# require '../lib/farmar_product' + +describe Product do + + describe "#initialize" do + it "should create an instance of Product" do + p = Product.new + p.must_be_instance_of (Product) + end + + # it "should know about associated data file" do + # m = FarMar::Market.new + # m.all.class.must_equal(Hash) + # end + end + + +end diff --git a/specs/farmar_sale_spec.rb b/specs/farmar_sale_spec.rb new file mode 100644 index 00000000..02221b94 --- /dev/null +++ b/specs/farmar_sale_spec.rb @@ -0,0 +1,19 @@ +require_relative 'spec_helper' +# require '../lib/farmar_sale' + +describe Sale do + + describe "#initialize" do + it "should create an instance of Sale" do + m = Sale.new + m.must_be_instance_of(Sale) + end + # + # it "should know about associated data file" do + # m = FarMar::Market.new + # m.all.class.must_equal(Hash) + # end + end + + +end diff --git a/specs/farmar_vendor_spec.rb b/specs/farmar_vendor_spec.rb new file mode 100644 index 00000000..f174e5ea --- /dev/null +++ b/specs/farmar_vendor_spec.rb @@ -0,0 +1,19 @@ +require_relative 'spec_helper' +# require '../lib/farmar_vendor' + +describe Vendor do + + describe "#initialize" do + it "should create an instance of Vendor" do + m = Vendor.new + m.must_be_instance_of(Vendor) + end + # + # it "should know about associated data file" do + # m = FarMar::Market.new + # m.all.class.must_equal(Hash) + # end + end + + +end diff --git a/specs/spec_helper.rb b/specs/spec_helper.rb new file mode 100644 index 00000000..13857109 --- /dev/null +++ b/specs/spec_helper.rb @@ -0,0 +1,12 @@ +require_relative'../far_mar' + +require 'simplecov' +SimpleCov.start + +require 'minitest' +require 'minitest/spec' +require "minitest/autorun" +require "minitest/reporters" +require 'minitest/pride' + +Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new From 62eb8302dfe77c8edaf86a25de8eb22101277645 Mon Sep 17 00:00:00 2001 From: Danielle Schrimmer Date: Tue, 6 Sep 2016 17:01:32 -0700 Subject: [PATCH 02/33] Add all/find methods to market, vendor, and product files. Sale tests currently failing. Revisit datetime functionality. --- lib/farmar_market.rb | 45 +++++++++++++++++++++--------------- lib/farmar_product.rb | 27 ++++++++++++++++++++-- lib/farmar_sale.rb | 9 ++++++++ lib/farmar_vendor.rb | 24 ++++++++++++++++++- specs/farmar_market_spec.rb | 25 ++++++++++++++++---- specs/farmar_product_spec.rb | 26 +++++++++++++++++---- specs/farmar_sale_spec.rb | 15 ++++++------ specs/farmar_vendor_spec.rb | 22 ++++++++++++------ 8 files changed, 148 insertions(+), 45 deletions(-) diff --git a/lib/farmar_market.rb b/lib/farmar_market.rb index ba8cf4bd..a3691540 100644 --- a/lib/farmar_market.rb +++ b/lib/farmar_market.rb @@ -2,27 +2,36 @@ class Market - attr_accessor :market_hash, :id, :name, :city, :county, :state, :zip + attr_reader :id, :name, :city, :county, :state, :zip - def initialize + def initialize(id, name, address, city, county, state, zip) + @id = id + @name = name + @city = city + @county = county + @state = state + @zip = zip end - # def initialize(id, name, address, city, county, state, zip) - # @id = id - # @name = name - # @city = city - # @county = county - # @state = state - # @zip = zip - # end + def self.all + markets = {} + CSV.read('../support/markets.csv').each do |line| + id = line[0].to_i + name = line[1] + address = line[2] + city = line[3] + county = line[4] + state = line[5] + zip = line[6] - # def self.all - # markets = {} - # CSV.read('../support/markets.csv').each do |line| - # markets = self.new(line[0].to_i, line[1], line[2], line[3], line[4], line[5], line[6]) - # # markets = {(line[0].to_i): name: line[1], address: line[2], city: line[3], county: line[4], state: line[5], zip: line[6]} - # # ) - # end - # end + markets[id] = self.new(id, name, address, city, county, state, zip) + end + return markets + end + + def self.find(id) + markets = Market.all + return markets[id] + end end diff --git a/lib/farmar_product.rb b/lib/farmar_product.rb index 0b330ab0..2226cc9c 100644 --- a/lib/farmar_product.rb +++ b/lib/farmar_product.rb @@ -1,8 +1,31 @@ require 'csv' - class Product + attr_reader :id, :name, :vendor_id + + def initialize(id, name, vendor_id) + @id = id + @name = name + @vendor_id = vendor_id + end + + def self.all + products = {} + CSV.read('../support/products.csv').each do |line| + id = line[0].to_i + name = line[1] + vendor_id = line[2].to_i + + products[id] = self.new(id, name, vendor_id) + + # products[line[0].to_i] = (self.new(id: line[0].to_i, name: line[1], vendor_id: line[2].to_i)) + end + return products + end + def self.find(id) + p = Product.all + return p[id] + end - end diff --git a/lib/farmar_sale.rb b/lib/farmar_sale.rb index 988ce2fa..7517b09a 100644 --- a/lib/farmar_sale.rb +++ b/lib/farmar_sale.rb @@ -2,6 +2,15 @@ class Sale + attr_reader :id, :amount, :purchase_time, :vendor_id, :product_id + + def initialize(id, amount, purchase_time, vendor_id, product_id) + @id = id + @amount = amount + @purchase_time = purchase_time + @vendor_id = vendor_id + @product_id = product_id + end diff --git a/lib/farmar_vendor.rb b/lib/farmar_vendor.rb index e04a838c..674d8c05 100644 --- a/lib/farmar_vendor.rb +++ b/lib/farmar_vendor.rb @@ -1,8 +1,30 @@ require 'csv' - class Vendor + attr_reader :id, :name, :employees, :market_id + + def initialize(id, name, employees, market_id) + @id = id + @name = name + @employees = employees + @market_id = market_id + end + def self.all + vendors = {} + CSV.read('../support/vendors.csv').each do |line| + id = line[0].to_i + name = line[1] + employees = line[2].to_i + market_id = line[3].to_i + vendors[id] = self.new(id, name, employees, market_id) + end + return vendors + end + def self.find(id) + v = Vendor.all + return v[id] + end end diff --git a/specs/farmar_market_spec.rb b/specs/farmar_market_spec.rb index b0595d8f..3e670e45 100644 --- a/specs/farmar_market_spec.rb +++ b/specs/farmar_market_spec.rb @@ -5,14 +5,29 @@ describe "#initialize" do it "should create an instance of Market" do - m = Market.new + m = Market.new(123, "name", "address", "city", "county", "state", "zip") m.must_be_instance_of(Market) end - # it "should know about associated data file" do - # m = FarMar::Market.new - # m.all.class.must_equal(Hash) - # end + it "should know about associated data file" do + m = Market.all + m[500].city.must_equal("Bronx") + end + end + + describe "all" do + it "should return a hash" do + m = Market.all + m.class.must_equal(Hash) + end + end + + describe "find" do + it "should return an instance of the object" do + m = Market.find(500) + m.must_be_instance_of(Market) + m.id.must_equal(500) + end end diff --git a/specs/farmar_product_spec.rb b/specs/farmar_product_spec.rb index bd858ac5..fd62d2a4 100644 --- a/specs/farmar_product_spec.rb +++ b/specs/farmar_product_spec.rb @@ -5,14 +5,30 @@ describe "#initialize" do it "should create an instance of Product" do - p = Product.new + p = Product.new(123, "fruits", 12) p.must_be_instance_of (Product) end - # it "should know about associated data file" do - # m = FarMar::Market.new - # m.all.class.must_equal(Hash) - # end + it "should know about associated data file" do + p = Product.all + p[4].name.must_equal("Yummy Fruit") + end + end + + describe "all" do + it "should return a hash" do + p = Product.all + p.class.must_equal(Hash) + end + end + + describe "find" do + it "should return an instance of the object" do + p = Product.find(11) + p.id.must_equal(11) + p.must_be_instance_of(Product) + p.name.must_equal("Gigantic Bread") + end end diff --git a/specs/farmar_sale_spec.rb b/specs/farmar_sale_spec.rb index 02221b94..5ad401da 100644 --- a/specs/farmar_sale_spec.rb +++ b/specs/farmar_sale_spec.rb @@ -5,14 +5,15 @@ describe "#initialize" do it "should create an instance of Sale" do - m = Sale.new - m.must_be_instance_of(Sale) + time = DateTime.strptime(2013-11-13 01:49:37 -0800, '%Y-%m-%d %H:%M:%S %z') + s = Sale.new(12, 100, time, 3, 1) + s.must_be_instance_of(Sale) + end + + it "should know about associated data file" do + s = Sale.new + s[id].must_equal(Hash) end - # - # it "should know about associated data file" do - # m = FarMar::Market.new - # m.all.class.must_equal(Hash) - # end end diff --git a/specs/farmar_vendor_spec.rb b/specs/farmar_vendor_spec.rb index f174e5ea..c0b9cf6a 100644 --- a/specs/farmar_vendor_spec.rb +++ b/specs/farmar_vendor_spec.rb @@ -5,14 +5,22 @@ describe "#initialize" do it "should create an instance of Vendor" do - m = Vendor.new - m.must_be_instance_of(Vendor) + v = Vendor.new(123, "name", 24, 31) + v.must_be_instance_of(Vendor) + end + + it "should know about associated data file" do + v = Vendor.all + v.class.must_equal(Hash) + end + end + + describe "find" do + it "should return an instance of the object" do + v = Vendor.find(5) + v.must_be_instance_of(Vendor) + v.employees.must_equal(3) end - # - # it "should know about associated data file" do - # m = FarMar::Market.new - # m.all.class.must_equal(Hash) - # end end From a82084ff92a3f122fe95dde151a64f3e1e36149f Mon Sep 17 00:00:00 2001 From: Danielle Schrimmer Date: Wed, 7 Sep 2016 15:02:17 -0700 Subject: [PATCH 03/33] Datetime value retrieved accurately --- lib/farmar_sale.rb | 20 ++++++++++++++++++-- specs/farmar_sale_spec.rb | 23 +++++++++++++++++++---- 2 files changed, 37 insertions(+), 6 deletions(-) diff --git a/lib/farmar_sale.rb b/lib/farmar_sale.rb index 7517b09a..75d40047 100644 --- a/lib/farmar_sale.rb +++ b/lib/farmar_sale.rb @@ -1,5 +1,5 @@ require 'csv' - +require 'date' class Sale attr_reader :id, :amount, :purchase_time, :vendor_id, :product_id @@ -7,11 +7,27 @@ class Sale def initialize(id, amount, purchase_time, vendor_id, product_id) @id = id @amount = amount - @purchase_time = purchase_time + @purchase_time = DateTime.strptime(purchase_time, '%Y-%m-%d %H:%M:%S %z') @vendor_id = vendor_id @product_id = product_id end + def self.all + sale = {} + CSV.read('../support/sales.csv').each do |line| + id = line[0].to_i + amount = line[1] + purchase_time = line[2] + vendor_id = line[3] + product_id = line[4] + sale[id] = self.new(id, amount, purchase_time, vendor_id, product_id) + end + return sale + end + def self.find(id) + sale = Sale.all + return sale[id] + end end diff --git a/specs/farmar_sale_spec.rb b/specs/farmar_sale_spec.rb index 5ad401da..f5e698d0 100644 --- a/specs/farmar_sale_spec.rb +++ b/specs/farmar_sale_spec.rb @@ -5,16 +5,31 @@ describe "#initialize" do it "should create an instance of Sale" do - time = DateTime.strptime(2013-11-13 01:49:37 -0800, '%Y-%m-%d %H:%M:%S %z') - s = Sale.new(12, 100, time, 3, 1) + # time = DateTime.strptime('2013-11-13 01:49:37 -0800', '%Y-%m-%d %H:%M:%S %z') + s = Sale.new(12, 100, '2013-11-13 01:49:37 -0800', 3, 1) s.must_be_instance_of(Sale) end it "should know about associated data file" do - s = Sale.new - s[id].must_equal(Hash) + # s = Sale.new(12, 100, '2013-11-13 01:49:37 -0800', 3, 1) + s = Sale.all + s.class.must_equal(Hash) end end + describe "all" do + it "should return a hash" do + s = Sale.all + s.class.must_equal(Hash) + end + end + + describe "find" do + it "should return an instance of the object" do + s = Sale.find(3) + # s.must_be_instance_of(Sale) + s.id.must_equal(3) + end + end end From 5b2204b65dde532b5120eba224a7d6ebd34dceed Mon Sep 17 00:00:00 2001 From: Danielle Schrimmer Date: Wed, 7 Sep 2016 15:02:44 -0700 Subject: [PATCH 04/33] Working on vendors method, currently broken. --- lib/farmar_market.rb | 14 ++++++++++++-- specs/farmar_market_spec.rb | 10 ++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/lib/farmar_market.rb b/lib/farmar_market.rb index a3691540..8be5906e 100644 --- a/lib/farmar_market.rb +++ b/lib/farmar_market.rb @@ -1,4 +1,5 @@ require 'csv' +require '../far_mar' class Market @@ -30,8 +31,17 @@ def self.all end def self.find(id) - markets = Market.all - return markets[id] + market = Market.all + return market[id] + end + + def vendors + vendor_list = Vendor.all + vendor_list.find_all { |n| n[market_id] == @id} + # vendor[n].market_id == market + # + # ehash['employee'].find_all { |e| e['level'] == 2} + end end diff --git a/specs/farmar_market_spec.rb b/specs/farmar_market_spec.rb index 3e670e45..c5ddbe76 100644 --- a/specs/farmar_market_spec.rb +++ b/specs/farmar_market_spec.rb @@ -30,5 +30,15 @@ end end + describe "#vendors" do + m = Market.all + it "should return a collection of FarMar::Vendor instances that are associated with the market" do + m.vendors.length.must_equal(10) + end + end + + # #vendors: returns a collection of FarMar::Vendor instances that are associated with the market by the market_id field. + + end From 33fa8ec9fab4f8dfad6524c09cab5699e70150f4 Mon Sep 17 00:00:00 2001 From: Danielle Schrimmer Date: Wed, 7 Sep 2016 16:27:48 -0700 Subject: [PATCH 05/33] Working market vendors method --- lib/farmar_market.rb | 6 +----- specs/farmar_market_spec.rb | 8 ++------ 2 files changed, 3 insertions(+), 11 deletions(-) diff --git a/lib/farmar_market.rb b/lib/farmar_market.rb index 8be5906e..9872b014 100644 --- a/lib/farmar_market.rb +++ b/lib/farmar_market.rb @@ -37,11 +37,7 @@ def self.find(id) def vendors vendor_list = Vendor.all - vendor_list.find_all { |n| n[market_id] == @id} - # vendor[n].market_id == market - # - # ehash['employee'].find_all { |e| e['level'] == 2} - + vendor_list.find_all { |n| n[1].market_id == @id } end end diff --git a/specs/farmar_market_spec.rb b/specs/farmar_market_spec.rb index c5ddbe76..b5734d61 100644 --- a/specs/farmar_market_spec.rb +++ b/specs/farmar_market_spec.rb @@ -31,14 +31,10 @@ end describe "#vendors" do - m = Market.all it "should return a collection of FarMar::Vendor instances that are associated with the market" do + m = Market.new(500, "name", "address", "city", "county", "state", "zip") m.vendors.length.must_equal(10) end end - - # #vendors: returns a collection of FarMar::Vendor instances that are associated with the market by the market_id field. - - - + end From 6949b4174cb5667191fe5e0346521e4960ad4564 Mon Sep 17 00:00:00 2001 From: Danielle Schrimmer Date: Wed, 7 Sep 2016 16:28:21 -0700 Subject: [PATCH 06/33] Working Vendor market method. --- lib/farmar_vendor.rb | 7 +++++++ specs/farmar_vendor_spec.rb | 8 ++++++++ 2 files changed, 15 insertions(+) diff --git a/lib/farmar_vendor.rb b/lib/farmar_vendor.rb index 674d8c05..b44a6322 100644 --- a/lib/farmar_vendor.rb +++ b/lib/farmar_vendor.rb @@ -1,4 +1,5 @@ require 'csv' +require '../far_mar' class Vendor attr_reader :id, :name, :employees, :market_id @@ -27,4 +28,10 @@ def self.find(id) v = Vendor.all return v[id] end + + def market + market_list = Market.all + match = market_list.find_all { |n| n[1].id == @market_id } + return match[0][1] + end end diff --git a/specs/farmar_vendor_spec.rb b/specs/farmar_vendor_spec.rb index c0b9cf6a..3d62f263 100644 --- a/specs/farmar_vendor_spec.rb +++ b/specs/farmar_vendor_spec.rb @@ -23,5 +23,13 @@ end end + describe "#market" do + it "should return the Market instance associated with this vendor" do + a = Vendor.new(293,"Kerluke-Denesik",11,54) + b = a.market + b.must_be_instance_of(Market) + b.name.must_equal("Crescent City Farmers Market") + end + end end From c7b13412a0d5b26023631299a45cdfd471524635 Mon Sep 17 00:00:00 2001 From: Danielle Schrimmer Date: Wed, 7 Sep 2016 16:47:49 -0700 Subject: [PATCH 07/33] Added working Vendor products method and associated test. --- lib/farmar_vendor.rb | 14 ++++++++++++-- specs/farmar_vendor_spec.rb | 10 ++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/lib/farmar_vendor.rb b/lib/farmar_vendor.rb index b44a6322..e1499fd0 100644 --- a/lib/farmar_vendor.rb +++ b/lib/farmar_vendor.rb @@ -31,7 +31,17 @@ def self.find(id) def market market_list = Market.all - match = market_list.find_all { |n| n[1].id == @market_id } - return match[0][1] + match = market_list.find { |n| n[1].id == @market_id } + return match[1] + end + + def products + product_list = Product.all + prods = product_list.find_all { |n| n[1].vendor_id == @id} + product_instances = [] + prods.length.times do |i| + product_instances << prods[i][1] + end + return product_instances end end diff --git a/specs/farmar_vendor_spec.rb b/specs/farmar_vendor_spec.rb index 3d62f263..be1a8af1 100644 --- a/specs/farmar_vendor_spec.rb +++ b/specs/farmar_vendor_spec.rb @@ -32,4 +32,14 @@ end end + describe "#products" do + it "should return a collection of product instances associated by product vendor id" do + a = Vendor.new(293,"Kerluke-Denesik",11,54) + b = a.products + b.class.must_equal(Array) + b.length.must_equal(4) + end + end + + end From 7eb6a990008b1340dcb1f4615ae889f6d5510ae8 Mon Sep 17 00:00:00 2001 From: Danielle Schrimmer Date: Wed, 7 Sep 2016 16:54:14 -0700 Subject: [PATCH 08/33] Edited Market vendors method to return an array of objects instead of array of arrays with keys and values. --- lib/farmar_market.rb | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/farmar_market.rb b/lib/farmar_market.rb index 9872b014..5c55f24d 100644 --- a/lib/farmar_market.rb +++ b/lib/farmar_market.rb @@ -37,7 +37,13 @@ def self.find(id) def vendors vendor_list = Vendor.all - vendor_list.find_all { |n| n[1].market_id == @id } + instances = vendor_list.find_all { |n| n[1].market_id == @id } + + vendor_instances = [] + instances.length.times do |i| + vendor_instances << instances[i][1] + end + return vendor_instances end end From e97be33b1fe9cf40bb79fcf57caef95446445667 Mon Sep 17 00:00:00 2001 From: Danielle Schrimmer Date: Thu, 8 Sep 2016 11:37:25 -0700 Subject: [PATCH 09/33] Edited files to allow for raking. Removed superfluous requiring FarMar instances and changed file references to be accurate for where rake is called from. --- lib/farmar_market.rb | 3 +-- lib/farmar_product.rb | 2 +- lib/farmar_sale.rb | 2 +- lib/farmar_vendor.rb | 3 +-- specs/farmar_vendor_spec.rb | 4 ++++ specs/spec_helper.rb | 2 +- 6 files changed, 9 insertions(+), 7 deletions(-) diff --git a/lib/farmar_market.rb b/lib/farmar_market.rb index 5c55f24d..29b49f54 100644 --- a/lib/farmar_market.rb +++ b/lib/farmar_market.rb @@ -1,5 +1,4 @@ require 'csv' -require '../far_mar' class Market @@ -16,7 +15,7 @@ def initialize(id, name, address, city, county, state, zip) def self.all markets = {} - CSV.read('../support/markets.csv').each do |line| + CSV.read('support/markets.csv').each do |line| id = line[0].to_i name = line[1] address = line[2] diff --git a/lib/farmar_product.rb b/lib/farmar_product.rb index 2226cc9c..bbb8049b 100644 --- a/lib/farmar_product.rb +++ b/lib/farmar_product.rb @@ -11,7 +11,7 @@ def initialize(id, name, vendor_id) def self.all products = {} - CSV.read('../support/products.csv').each do |line| + CSV.read('support/products.csv').each do |line| id = line[0].to_i name = line[1] vendor_id = line[2].to_i diff --git a/lib/farmar_sale.rb b/lib/farmar_sale.rb index 75d40047..d897626d 100644 --- a/lib/farmar_sale.rb +++ b/lib/farmar_sale.rb @@ -14,7 +14,7 @@ def initialize(id, amount, purchase_time, vendor_id, product_id) def self.all sale = {} - CSV.read('../support/sales.csv').each do |line| + CSV.read('support/sales.csv').each do |line| id = line[0].to_i amount = line[1] purchase_time = line[2] diff --git a/lib/farmar_vendor.rb b/lib/farmar_vendor.rb index e1499fd0..950210ce 100644 --- a/lib/farmar_vendor.rb +++ b/lib/farmar_vendor.rb @@ -1,5 +1,4 @@ require 'csv' -require '../far_mar' class Vendor attr_reader :id, :name, :employees, :market_id @@ -13,7 +12,7 @@ def initialize(id, name, employees, market_id) def self.all vendors = {} - CSV.read('../support/vendors.csv').each do |line| + CSV.read('support/vendors.csv').each do |line| id = line[0].to_i name = line[1] employees = line[2].to_i diff --git a/specs/farmar_vendor_spec.rb b/specs/farmar_vendor_spec.rb index be1a8af1..af130010 100644 --- a/specs/farmar_vendor_spec.rb +++ b/specs/farmar_vendor_spec.rb @@ -41,5 +41,9 @@ end end + describe "#sales" do + # it "should " + end + end diff --git a/specs/spec_helper.rb b/specs/spec_helper.rb index 13857109..e16cd3ae 100644 --- a/specs/spec_helper.rb +++ b/specs/spec_helper.rb @@ -1,4 +1,4 @@ -require_relative'../far_mar' +require_relative '../far_mar' require 'simplecov' SimpleCov.start From 90a576b31d4d40f2736fa862ea8ca7456e092e5c Mon Sep 17 00:00:00 2001 From: Danielle Schrimmer Date: Thu, 8 Sep 2016 12:09:10 -0700 Subject: [PATCH 10/33] Working Vendor sales method and test. Note to self: double check .to_is when everything seems like it should work. --- lib/farmar_sale.rb | 6 +++--- lib/farmar_vendor.rb | 22 +++++++++++++++++++--- specs/farmar_vendor_spec.rb | 15 ++++++++++++++- 3 files changed, 36 insertions(+), 7 deletions(-) diff --git a/lib/farmar_sale.rb b/lib/farmar_sale.rb index d897626d..21290536 100644 --- a/lib/farmar_sale.rb +++ b/lib/farmar_sale.rb @@ -16,10 +16,10 @@ def self.all sale = {} CSV.read('support/sales.csv').each do |line| id = line[0].to_i - amount = line[1] + amount = line[1].to_i purchase_time = line[2] - vendor_id = line[3] - product_id = line[4] + vendor_id = line[3].to_i + product_id = line[4].to_i sale[id] = self.new(id, amount, purchase_time, vendor_id, product_id) end diff --git a/lib/farmar_vendor.rb b/lib/farmar_vendor.rb index 950210ce..9606f151 100644 --- a/lib/farmar_vendor.rb +++ b/lib/farmar_vendor.rb @@ -36,11 +36,27 @@ def market def products product_list = Product.all - prods = product_list.find_all { |n| n[1].vendor_id == @id} + match = product_list.find_all { |n| n[1].vendor_id == @id } product_instances = [] - prods.length.times do |i| - product_instances << prods[i][1] + match.length.times do |i| + product_instances << match[i][1] end return product_instances end + + def sales + sales_list = Sale.all + # print sales_list + match = sales_list.find_all { |n| n[1].vendor_id == @id } + puts match + sales_instances = [] + match.length.times do |i| + sales_instances << match[i][1] + end + return sales_instances + end + + # def revenue + # + # end end diff --git a/specs/farmar_vendor_spec.rb b/specs/farmar_vendor_spec.rb index af130010..85c464c6 100644 --- a/specs/farmar_vendor_spec.rb +++ b/specs/farmar_vendor_spec.rb @@ -42,8 +42,21 @@ end describe "#sales" do - # it "should " + it "should return a collection of Sale instances that are associated by vendor id" do + a = Vendor.new(1,"Feil-Farrell",8,1) + b = a.sales + b.class.must_equal(Array) + b[0].must_be_instance_of(Sale) + b.length.must_equal(7) + end end + # describe "#revenue" do + # it "should return the sum of all the vendor's sales (in cents)" do + # a = Vendor.new(1,"Feil-Farrell",8,1) + # b = a.sales + # b.revenue.must_equal(38259) + # end + # end end From 3bd7a599058ebf6a7004e3daa90114033ca0db16 Mon Sep 17 00:00:00 2001 From: Danielle Schrimmer Date: Thu, 8 Sep 2016 12:30:32 -0700 Subject: [PATCH 11/33] Working vendor Revenue method. --- lib/farmar_vendor.rb | 12 ++++++++---- specs/farmar_vendor_spec.rb | 14 +++++++------- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/lib/farmar_vendor.rb b/lib/farmar_vendor.rb index 9606f151..d717f5ff 100644 --- a/lib/farmar_vendor.rb +++ b/lib/farmar_vendor.rb @@ -48,7 +48,6 @@ def sales sales_list = Sale.all # print sales_list match = sales_list.find_all { |n| n[1].vendor_id == @id } - puts match sales_instances = [] match.length.times do |i| sales_instances << match[i][1] @@ -56,7 +55,12 @@ def sales return sales_instances end - # def revenue - # - # end + def revenue + total_revenue = 0 + sales_instances = sales + sales_instances.each do |i| + total_revenue += i.amount + end + return total_revenue + end end diff --git a/specs/farmar_vendor_spec.rb b/specs/farmar_vendor_spec.rb index 85c464c6..a5a92741 100644 --- a/specs/farmar_vendor_spec.rb +++ b/specs/farmar_vendor_spec.rb @@ -51,12 +51,12 @@ end end - # describe "#revenue" do - # it "should return the sum of all the vendor's sales (in cents)" do - # a = Vendor.new(1,"Feil-Farrell",8,1) - # b = a.sales - # b.revenue.must_equal(38259) - # end - # end + describe "#revenue" do + it "should return the sum of all the vendor's sales (in cents)" do + a = Vendor.new(1,"Feil-Farrell",8,1) + a.sales + a.revenue.must_equal(38259) + end + end end From 922f36ecc89c4a47c85ff7a490282bee3c49cf83 Mon Sep 17 00:00:00 2001 From: Danielle Schrimmer Date: Thu, 8 Sep 2016 12:32:11 -0700 Subject: [PATCH 12/33] Print total revenue --- lib/farmar_vendor.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/farmar_vendor.rb b/lib/farmar_vendor.rb index d717f5ff..88af2d25 100644 --- a/lib/farmar_vendor.rb +++ b/lib/farmar_vendor.rb @@ -61,6 +61,7 @@ def revenue sales_instances.each do |i| total_revenue += i.amount end + puts "The total revenue is #{ total_revenue }" return total_revenue end end From afd69d4cce28c4a07e8dc431e39a8ca3a46c3f79 Mon Sep 17 00:00:00 2001 From: Danielle Schrimmer Date: Thu, 8 Sep 2016 13:59:35 -0700 Subject: [PATCH 13/33] Working Vendor by_market method and test --- lib/farmar_vendor.rb | 11 +++++++++++ specs/farmar_vendor_spec.rb | 9 +++++++++ 2 files changed, 20 insertions(+) diff --git a/lib/farmar_vendor.rb b/lib/farmar_vendor.rb index 88af2d25..bcce5164 100644 --- a/lib/farmar_vendor.rb +++ b/lib/farmar_vendor.rb @@ -64,4 +64,15 @@ def revenue puts "The total revenue is #{ total_revenue }" return total_revenue end + + def self.by_market(market_id) + a = all.find_all { |n| n[1].market_id == market_id } + puts a + + vendors_by_market = [] + a.length.times do |i| + vendors_by_market << a[i][1] + end + return vendors_by_market + end end diff --git a/specs/farmar_vendor_spec.rb b/specs/farmar_vendor_spec.rb index a5a92741..60630c1f 100644 --- a/specs/farmar_vendor_spec.rb +++ b/specs/farmar_vendor_spec.rb @@ -59,4 +59,13 @@ end end + describe "by_market" do + it "should return all of the vendors with the given market_id" do + a = Vendor.by_market(1) + a.class.must_equal(Array) + a[0].class.must_equal(Vendor) + a.length.must_equal(6) + end + end + end From 17a5fe43574bdbc18e7518c02b67830d22b8003c Mon Sep 17 00:00:00 2001 From: Danielle Schrimmer Date: Thu, 8 Sep 2016 14:13:11 -0700 Subject: [PATCH 14/33] Removed test printing --- lib/farmar_vendor.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/farmar_vendor.rb b/lib/farmar_vendor.rb index bcce5164..2d4ae55c 100644 --- a/lib/farmar_vendor.rb +++ b/lib/farmar_vendor.rb @@ -67,7 +67,6 @@ def revenue def self.by_market(market_id) a = all.find_all { |n| n[1].market_id == market_id } - puts a vendors_by_market = [] a.length.times do |i| From d4a23f2c3358bf2feb2231904a67ed183d848c7e Mon Sep 17 00:00:00 2001 From: Danielle Schrimmer Date: Thu, 8 Sep 2016 14:15:56 -0700 Subject: [PATCH 15/33] Working Product vendor method and test --- lib/farmar_product.rb | 5 +++++ specs/farmar_product_spec.rb | 8 ++++++++ 2 files changed, 13 insertions(+) diff --git a/lib/farmar_product.rb b/lib/farmar_product.rb index bbb8049b..256b764f 100644 --- a/lib/farmar_product.rb +++ b/lib/farmar_product.rb @@ -28,4 +28,9 @@ def self.find(id) return p[id] end + def vendor + v = Vendor.all.find { |n| n[1].id == @vendor_id} + return v[1] + end + end diff --git a/specs/farmar_product_spec.rb b/specs/farmar_product_spec.rb index fd62d2a4..11d718ae 100644 --- a/specs/farmar_product_spec.rb +++ b/specs/farmar_product_spec.rb @@ -31,5 +31,13 @@ end end + describe "#vendor" do + it "should return the Vendor instance that is associated with the product vendor_id" do + p = Product.new(1,"Dry Beets",1).vendor + p.must_be_instance_of(Vendor) + p.name.must_equal("Feil-Farrell") + end + end + end From 44b7117e17c869de8624ed859c2c757e0aa8c239 Mon Sep 17 00:00:00 2001 From: Danielle Schrimmer Date: Thu, 8 Sep 2016 14:22:45 -0700 Subject: [PATCH 16/33] Working Product number_of_sales method and test --- lib/farmar_product.rb | 7 +++++++ specs/farmar_product_spec.rb | 6 ++++++ 2 files changed, 13 insertions(+) diff --git a/lib/farmar_product.rb b/lib/farmar_product.rb index 256b764f..88b0d49c 100644 --- a/lib/farmar_product.rb +++ b/lib/farmar_product.rb @@ -33,4 +33,11 @@ def vendor return v[1] end + def number_of_sales + s = Sale.all + sale_instances = s.find_all { |n| n[1].product_id == @id } + + return sale_instances.length + end + end diff --git a/specs/farmar_product_spec.rb b/specs/farmar_product_spec.rb index 11d718ae..e488e692 100644 --- a/specs/farmar_product_spec.rb +++ b/specs/farmar_product_spec.rb @@ -39,5 +39,11 @@ end end + describe "#number_of_sales" do + it "should return the number of times this product has been sold" do + Product.new(1,"Dry Beets",1).number_of_sales.must_equal(7) + + end + end end From edd455950d23ffcf7a5552290c68796b01764a76 Mon Sep 17 00:00:00 2001 From: Danielle Schrimmer Date: Thu, 8 Sep 2016 14:42:29 -0700 Subject: [PATCH 17/33] Refactor product specs to reuse Product instances. --- specs/farmar_product_spec.rb | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/specs/farmar_product_spec.rb b/specs/farmar_product_spec.rb index e488e692..929325af 100644 --- a/specs/farmar_product_spec.rb +++ b/specs/farmar_product_spec.rb @@ -2,47 +2,47 @@ # require '../lib/farmar_product' describe Product do + let all = Product.all + let prod = Product.new(1,"Dry Beets",1) + describe "#initialize" do it "should create an instance of Product" do - p = Product.new(123, "fruits", 12) - p.must_be_instance_of (Product) + # p = Product.new(123, "fruits", 12) + prod.must_be_instance_of(Product) end it "should know about associated data file" do - p = Product.all - p[4].name.must_equal("Yummy Fruit") + all[4].name.must_equal("Yummy Fruit") end end describe "all" do it "should return a hash" do - p = Product.all - p.class.must_equal(Hash) + all.class.must_equal(Hash) end end describe "find" do it "should return an instance of the object" do - p = Product.find(11) - p.id.must_equal(11) - p.must_be_instance_of(Product) - p.name.must_equal("Gigantic Bread") + finder = Product.find(11) + finder.id.must_equal(11) + finder.must_be_instance_of(Product) + finder.name.must_equal("Gigantic Bread") end end describe "#vendor" do it "should return the Vendor instance that is associated with the product vendor_id" do - p = Product.new(1,"Dry Beets",1).vendor - p.must_be_instance_of(Vendor) - p.name.must_equal("Feil-Farrell") + vend = prod.vendor + vend.must_be_instance_of(Vendor) + vend.name.must_equal("Feil-Farrell") end end describe "#number_of_sales" do it "should return the number of times this product has been sold" do - Product.new(1,"Dry Beets",1).number_of_sales.must_equal(7) - + prod.number_of_sales.must_equal(7) end end From f6c3383b6ca69b46ac0c182c43b0c0aa9ec95531 Mon Sep 17 00:00:00 2001 From: Danielle Schrimmer Date: Thu, 8 Sep 2016 15:10:47 -0700 Subject: [PATCH 18/33] Added working Product by_vendor method --- lib/farmar_product.rb | 16 ++++++++++++++++ specs/farmar_product_spec.rb | 12 ++++++++++++ 2 files changed, 28 insertions(+) diff --git a/lib/farmar_product.rb b/lib/farmar_product.rb index 88b0d49c..380430dc 100644 --- a/lib/farmar_product.rb +++ b/lib/farmar_product.rb @@ -33,6 +33,12 @@ def vendor return v[1] end + def sales + s = Sale.all + sale_instances = s.find_all { |n| n[1].product_id == @id } + return sale_instances + end + def number_of_sales s = Sale.all sale_instances = s.find_all { |n| n[1].product_id == @id } @@ -40,4 +46,14 @@ def number_of_sales return sale_instances.length end + def self.by_vendor(vendor_id) + p = all.find_all { |n| n[1].vendor_id == vendor_id } + + product_list = [] + p.length.times do |i| + product_list << p[i][1] + end + return product_list + end + end diff --git a/specs/farmar_product_spec.rb b/specs/farmar_product_spec.rb index 929325af..9487dcae 100644 --- a/specs/farmar_product_spec.rb +++ b/specs/farmar_product_spec.rb @@ -40,10 +40,22 @@ end end + describe "#sales" do + it "should return a collection of Sale instances that are associated with the Sale product_id field" do + prod.sales.length.must_equal(7) + end + end + describe "#number_of_sales" do it "should return the number of times this product has been sold" do prod.number_of_sales.must_equal(7) end end + describe "by_vendor" do + it "should return all products with the given vendor id" do + Product.by_vendor(4).length.must_equal(3) + end + end + end From 179e783fc309d3f1e99ce72f38eed02a5a971f4d Mon Sep 17 00:00:00 2001 From: Danielle Schrimmer Date: Thu, 8 Sep 2016 15:31:14 -0700 Subject: [PATCH 19/33] Working Sale vendor method --- lib/farmar_sale.rb | 4 ++++ specs/farmar_sale_spec.rb | 37 +++++++++++++++++++++++++++++-------- 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/lib/farmar_sale.rb b/lib/farmar_sale.rb index 21290536..e8b8a349 100644 --- a/lib/farmar_sale.rb +++ b/lib/farmar_sale.rb @@ -30,4 +30,8 @@ def self.find(id) sale = Sale.all return sale[id] end + + def vendor + Vendor.find(@vendor_id) + end end diff --git a/specs/farmar_sale_spec.rb b/specs/farmar_sale_spec.rb index f5e698d0..70b76656 100644 --- a/specs/farmar_sale_spec.rb +++ b/specs/farmar_sale_spec.rb @@ -2,34 +2,55 @@ # require '../lib/farmar_sale' describe Sale do + let s = Sale.new(12, 100, '2013-11-13 01:49:37 -0800', 3, 1) + let all = Sale.all describe "#initialize" do it "should create an instance of Sale" do # time = DateTime.strptime('2013-11-13 01:49:37 -0800', '%Y-%m-%d %H:%M:%S %z') - s = Sale.new(12, 100, '2013-11-13 01:49:37 -0800', 3, 1) + # s = Sale.new(12, 100, '2013-11-13 01:49:37 -0800', 3, 1) s.must_be_instance_of(Sale) end it "should know about associated data file" do # s = Sale.new(12, 100, '2013-11-13 01:49:37 -0800', 3, 1) - s = Sale.all - s.class.must_equal(Hash) + # s = Sale.all + all.class.must_equal(Hash) end end describe "all" do it "should return a hash" do - s = Sale.all - s.class.must_equal(Hash) + # s = Sale.all + all.class.must_equal(Hash) end end describe "find" do it "should return an instance of the object" do - s = Sale.find(3) - # s.must_be_instance_of(Sale) - s.id.must_equal(3) + find_s = Sale.find(3) + find_s.must_be_instance_of(Sale) + find_s.id.must_equal(3) end end + describe "#vendor" do + it "should return the Vendor instance that is associated with the sale using Sale vendor_id" do + vend = s.vendor + vend.id.must_equal(3) + vend.name.must_equal("Breitenberg Inc") + end + end + + # describe "#product" do + # it "should return the Product instance associated with the sale using the Sale product id" do + # s.product.must_be_instance_of(Product) + # s.product.length.must_equal(1) + # end + # end + # + # describe "between" do + # + # end + end From 84009e6402f0dd4482519637a67cb9c091303ed6 Mon Sep 17 00:00:00 2001 From: Danielle Schrimmer Date: Thu, 8 Sep 2016 15:33:42 -0700 Subject: [PATCH 20/33] Working Sale product method --- lib/farmar_sale.rb | 4 ++++ specs/farmar_sale_spec.rb | 12 ++++++------ 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/lib/farmar_sale.rb b/lib/farmar_sale.rb index e8b8a349..054444ba 100644 --- a/lib/farmar_sale.rb +++ b/lib/farmar_sale.rb @@ -34,4 +34,8 @@ def self.find(id) def vendor Vendor.find(@vendor_id) end + + def product + Product.find(@product_id) + end end diff --git a/specs/farmar_sale_spec.rb b/specs/farmar_sale_spec.rb index 70b76656..c3548857 100644 --- a/specs/farmar_sale_spec.rb +++ b/specs/farmar_sale_spec.rb @@ -42,12 +42,12 @@ end end - # describe "#product" do - # it "should return the Product instance associated with the sale using the Sale product id" do - # s.product.must_be_instance_of(Product) - # s.product.length.must_equal(1) - # end - # end + describe "#product" do + it "should return the Product instance associated with the sale using the Sale product id" do + s.product.must_be_instance_of(Product) + s.product.id.must_equal(1) + end + end # # describe "between" do # From f2f8988ef8c45b7a836ac75c0655ceb88b509c24 Mon Sep 17 00:00:00 2001 From: Danielle Schrimmer Date: Thu, 8 Sep 2016 16:16:30 -0700 Subject: [PATCH 21/33] Working Sale between method and test --- lib/farmar_sale.rb | 10 +++++++++- specs/farmar_sale_spec.rb | 10 ++++++---- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/lib/farmar_sale.rb b/lib/farmar_sale.rb index 054444ba..f341e504 100644 --- a/lib/farmar_sale.rb +++ b/lib/farmar_sale.rb @@ -7,7 +7,7 @@ class Sale def initialize(id, amount, purchase_time, vendor_id, product_id) @id = id @amount = amount - @purchase_time = DateTime.strptime(purchase_time, '%Y-%m-%d %H:%M:%S %z') + @purchase_time = DateTime.parse(purchase_time) @vendor_id = vendor_id @product_id = product_id end @@ -38,4 +38,12 @@ def vendor def product Product.find(@product_id) end + + def self.between(beginning_time, end_time) + beginning_time = DateTime.parse(beginning_time) + end_time = DateTime.parse(end_time) + + Sale.all.find_all { |n| n[1].purchase_time.between?(beginning_time, end_time)} + # n[1].purchase_time is >= beginning_time || n[1].purchase_time is <= end_time} + end end diff --git a/specs/farmar_sale_spec.rb b/specs/farmar_sale_spec.rb index c3548857..3daaf9f6 100644 --- a/specs/farmar_sale_spec.rb +++ b/specs/farmar_sale_spec.rb @@ -48,9 +48,11 @@ s.product.id.must_equal(1) end end - # - # describe "between" do - # - # end + + describe "between" do + it "should return a collection of Sale objects where the purchase time is between the two times given as arguments" do + Sale.between('2013-11-13 01:48:00 -0800', '2013-11-13 01:50:59 -0800').length.must_equal(8) + end + end end From df18ebc620732b9ea440ed79500b5a117c3df549 Mon Sep 17 00:00:00 2001 From: Danielle Schrimmer Date: Thu, 8 Sep 2016 16:18:01 -0700 Subject: [PATCH 22/33] Clean up unused commented tests --- specs/farmar_sale_spec.rb | 6 ------ 1 file changed, 6 deletions(-) diff --git a/specs/farmar_sale_spec.rb b/specs/farmar_sale_spec.rb index 3daaf9f6..ddc1a027 100644 --- a/specs/farmar_sale_spec.rb +++ b/specs/farmar_sale_spec.rb @@ -1,5 +1,4 @@ require_relative 'spec_helper' -# require '../lib/farmar_sale' describe Sale do let s = Sale.new(12, 100, '2013-11-13 01:49:37 -0800', 3, 1) @@ -7,21 +6,16 @@ describe "#initialize" do it "should create an instance of Sale" do - # time = DateTime.strptime('2013-11-13 01:49:37 -0800', '%Y-%m-%d %H:%M:%S %z') - # s = Sale.new(12, 100, '2013-11-13 01:49:37 -0800', 3, 1) s.must_be_instance_of(Sale) end it "should know about associated data file" do - # s = Sale.new(12, 100, '2013-11-13 01:49:37 -0800', 3, 1) - # s = Sale.all all.class.must_equal(Hash) end end describe "all" do it "should return a hash" do - # s = Sale.all all.class.must_equal(Hash) end end From a09b6b0d447498a4bf631056cbf2561dc066c022 Mon Sep 17 00:00:00 2001 From: Danielle Schrimmer Date: Thu, 8 Sep 2016 16:22:22 -0700 Subject: [PATCH 23/33] Refactor to reuse created methods in other methods (Vendor market method) --- lib/farmar_sale.rb | 1 - lib/farmar_vendor.rb | 5 +---- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/lib/farmar_sale.rb b/lib/farmar_sale.rb index f341e504..b78bea68 100644 --- a/lib/farmar_sale.rb +++ b/lib/farmar_sale.rb @@ -44,6 +44,5 @@ def self.between(beginning_time, end_time) end_time = DateTime.parse(end_time) Sale.all.find_all { |n| n[1].purchase_time.between?(beginning_time, end_time)} - # n[1].purchase_time is >= beginning_time || n[1].purchase_time is <= end_time} end end diff --git a/lib/farmar_vendor.rb b/lib/farmar_vendor.rb index 2d4ae55c..2ee98bf3 100644 --- a/lib/farmar_vendor.rb +++ b/lib/farmar_vendor.rb @@ -29,9 +29,7 @@ def self.find(id) end def market - market_list = Market.all - match = market_list.find { |n| n[1].id == @market_id } - return match[1] + Market.find(@market_id) end def products @@ -46,7 +44,6 @@ def products def sales sales_list = Sale.all - # print sales_list match = sales_list.find_all { |n| n[1].vendor_id == @id } sales_instances = [] match.length.times do |i| From 7092ab6203de3bdd0497bb02721f2c6407628ea1 Mon Sep 17 00:00:00 2001 From: Danielle Schrimmer Date: Thu, 8 Sep 2016 16:25:22 -0700 Subject: [PATCH 24/33] Update to reuse previously created methods (in Product vendor method) --- lib/farmar_product.rb | 4 +--- specs/farmar_market_spec.rb | 3 +-- specs/farmar_product_spec.rb | 1 - 3 files changed, 2 insertions(+), 6 deletions(-) diff --git a/lib/farmar_product.rb b/lib/farmar_product.rb index 380430dc..febf3bf8 100644 --- a/lib/farmar_product.rb +++ b/lib/farmar_product.rb @@ -29,8 +29,7 @@ def self.find(id) end def vendor - v = Vendor.all.find { |n| n[1].id == @vendor_id} - return v[1] + Vendor.find(@vendor_id) end def sales @@ -42,7 +41,6 @@ def sales def number_of_sales s = Sale.all sale_instances = s.find_all { |n| n[1].product_id == @id } - return sale_instances.length end diff --git a/specs/farmar_market_spec.rb b/specs/farmar_market_spec.rb index b5734d61..99ef3930 100644 --- a/specs/farmar_market_spec.rb +++ b/specs/farmar_market_spec.rb @@ -1,5 +1,4 @@ require_relative 'spec_helper' -# require '../lib/farmar_market' describe Market do @@ -36,5 +35,5 @@ m.vendors.length.must_equal(10) end end - + end diff --git a/specs/farmar_product_spec.rb b/specs/farmar_product_spec.rb index 9487dcae..e9394491 100644 --- a/specs/farmar_product_spec.rb +++ b/specs/farmar_product_spec.rb @@ -1,5 +1,4 @@ require_relative 'spec_helper' -# require '../lib/farmar_product' describe Product do let all = Product.all From be11cbce6a0c29c159d83b6880a23ab06822e051 Mon Sep 17 00:00:00 2001 From: Danielle Schrimmer Date: Fri, 9 Sep 2016 10:32:40 -0700 Subject: [PATCH 25/33] Working Market products method --- lib/farmar_market.rb | 18 ++++++++++++++++++ specs/farmar_market_spec.rb | 9 ++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/lib/farmar_market.rb b/lib/farmar_market.rb index 29b49f54..55afb8b2 100644 --- a/lib/farmar_market.rb +++ b/lib/farmar_market.rb @@ -45,4 +45,22 @@ def vendors return vendor_instances end + def products + vendor_list = vendors # Vendors associated with market + all_products = Product.all + + product_list = [] + + # find Products that match Vendor ID associated with Market + vendor_list.length.times do |i| + p = all_products.find_all { |n| n[1].vendor_id == vendor_list[i].id} + + # add Product objects to product_list array + p.length.times do |j| + product_list << p[j][1] + end + end + return product_list + end + end diff --git a/specs/farmar_market_spec.rb b/specs/farmar_market_spec.rb index 99ef3930..9325c2b4 100644 --- a/specs/farmar_market_spec.rb +++ b/specs/farmar_market_spec.rb @@ -1,6 +1,7 @@ require_relative 'spec_helper' describe Market do + let (:m) { Market.new(500, "name", "address", "city", "county", "state", "zip") } describe "#initialize" do it "should create an instance of Market" do @@ -31,9 +32,15 @@ describe "#vendors" do it "should return a collection of FarMar::Vendor instances that are associated with the market" do - m = Market.new(500, "name", "address", "city", "county", "state", "zip") + # m = Market.new(500, "name", "address", "city", "county", "state", "zip") m.vendors.length.must_equal(10) end end + describe "#products" do + it "should return a collection of Product instances that are associated to the market through the Vendor class" do + + end + end + end From 5089e6b6bcd3a93ba970135f3720aa53aae33974 Mon Sep 17 00:00:00 2001 From: Danielle Schrimmer Date: Fri, 9 Sep 2016 11:19:14 -0700 Subject: [PATCH 26/33] Working Market search method. --- lib/farmar_market.rb | 24 ++++++++++++++++++++++++ specs/farmar_market_spec.rb | 7 +++++++ 2 files changed, 31 insertions(+) diff --git a/lib/farmar_market.rb b/lib/farmar_market.rb index 55afb8b2..5ea6dc5d 100644 --- a/lib/farmar_market.rb +++ b/lib/farmar_market.rb @@ -63,4 +63,28 @@ def products return product_list end + def self.search(search_term) + matches = [] + v = Vendor.all + m = Market.all + + # find and add matching vendor names + v_matches = v.find_all { |n| n[1].name.downcase.include?(search_term) } + + v_matches.each do |i| + matches << i[1] + end + + # find and add matching market names + m_matches = m.find_all { |n| n[1].name.downcase.include?(search_term)} + + m_matches.each do |i| + matches << i[1] + end + # matches << m_matches + + return matches + + end + end diff --git a/specs/farmar_market_spec.rb b/specs/farmar_market_spec.rb index 9325c2b4..854ef3fc 100644 --- a/specs/farmar_market_spec.rb +++ b/specs/farmar_market_spec.rb @@ -39,7 +39,14 @@ describe "#products" do it "should return a collection of Product instances that are associated to the market through the Vendor class" do + m.products.length.must_equal(34) + end + end + describe "search" do + it "should returna collection of Market instances where the market or vendor name contain the search item" do + # Market.search('school').length.must_equal(3) + Market.search('collins').length.must_equal(12 + 2) # 12 vendors and 2 markets end end From 8928d5bcc31f086af41f406229f5294b6afd4857 Mon Sep 17 00:00:00 2001 From: Danielle Schrimmer Date: Fri, 9 Sep 2016 12:24:54 -0700 Subject: [PATCH 27/33] In progress: prefered_vendor. Need to implement as instance method, checking only vendors from particular market. Removed puts statement from Vendor revenue method. --- lib/farmar_market.rb | 13 +++++++++++++ lib/farmar_vendor.rb | 1 - specs/farmar_market_spec.rb | 6 ++++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/lib/farmar_market.rb b/lib/farmar_market.rb index 5ea6dc5d..3220e319 100644 --- a/lib/farmar_market.rb +++ b/lib/farmar_market.rb @@ -84,7 +84,20 @@ def self.search(search_term) # matches << m_matches return matches + end + + def prefered_vendor + v = Vendor.all.values + revenues = [] + + v.each do |i| + revenues << i.revenue + end + + max_revenue = revenues.max + max_vendor = revenues.index(max_revenue) + return max_vendor end end diff --git a/lib/farmar_vendor.rb b/lib/farmar_vendor.rb index 2ee98bf3..04292d93 100644 --- a/lib/farmar_vendor.rb +++ b/lib/farmar_vendor.rb @@ -58,7 +58,6 @@ def revenue sales_instances.each do |i| total_revenue += i.amount end - puts "The total revenue is #{ total_revenue }" return total_revenue end diff --git a/specs/farmar_market_spec.rb b/specs/farmar_market_spec.rb index 854ef3fc..5ece919d 100644 --- a/specs/farmar_market_spec.rb +++ b/specs/farmar_market_spec.rb @@ -50,4 +50,10 @@ end end + describe "#prefered_vendor" do + it "should return the vendor with the highest revenue" do + Market.prefered_vendor.class.must_equal(Vendor) + end + end + end From f955204c26a477d1dbb74c9a5aac381785b9f6c6 Mon Sep 17 00:00:00 2001 From: Danielle Schrimmer Date: Fri, 9 Sep 2016 13:52:18 -0700 Subject: [PATCH 28/33] Working Market prefered_vendor method --- lib/farmar_market.rb | 12 +----------- specs/farmar_market_spec.rb | 8 +++++++- 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/lib/farmar_market.rb b/lib/farmar_market.rb index 3220e319..670de976 100644 --- a/lib/farmar_market.rb +++ b/lib/farmar_market.rb @@ -87,17 +87,7 @@ def self.search(search_term) end def prefered_vendor - v = Vendor.all.values - revenues = [] - - v.each do |i| - revenues << i.revenue - end - - max_revenue = revenues.max - max_vendor = revenues.index(max_revenue) - - return max_vendor + vendors.max_by { |n| n.revenue } end end diff --git a/specs/farmar_market_spec.rb b/specs/farmar_market_spec.rb index 5ece919d..82fdc8ba 100644 --- a/specs/farmar_market_spec.rb +++ b/specs/farmar_market_spec.rb @@ -52,8 +52,14 @@ describe "#prefered_vendor" do it "should return the vendor with the highest revenue" do - Market.prefered_vendor.class.must_equal(Vendor) + m.prefered_vendor.class.must_equal(Vendor) end end + # describe "#prefered_vendor(date)" do + # it "should return the vendor with the highest revenue for the given date" do + # + # end + # end + end From 891474e32187f796cf527c103ea491683ee508b1 Mon Sep 17 00:00:00 2001 From: Danielle Schrimmer Date: Fri, 9 Sep 2016 14:34:04 -0700 Subject: [PATCH 29/33] Refactor to simplify search, products, and vendors methods --- lib/farmar_market.rb | 52 ++++++++++++++++---------------------------- 1 file changed, 19 insertions(+), 33 deletions(-) diff --git a/lib/farmar_market.rb b/lib/farmar_market.rb index 670de976..94ded9fd 100644 --- a/lib/farmar_market.rb +++ b/lib/farmar_market.rb @@ -35,59 +35,45 @@ def self.find(id) end def vendors - vendor_list = Vendor.all - instances = vendor_list.find_all { |n| n[1].market_id == @id } - - vendor_instances = [] - instances.length.times do |i| - vendor_instances << instances[i][1] - end - return vendor_instances + vendor_list = Vendor.all.values + vendor_list.find_all { |n| n.market_id == @id } end def products - vendor_list = vendors # Vendors associated with market - all_products = Product.all + vendor_list = vendors # Vendors associated with Market instance + all_products = Product.all.values product_list = [] - # find Products that match Vendor ID associated with Market - vendor_list.length.times do |i| - p = all_products.find_all { |n| n[1].vendor_id == vendor_list[i].id} - - # add Product objects to product_list array - p.length.times do |j| - product_list << p[j][1] - end + vendor_list.each do |i| + product_list << all_products.find_all { |n| n.vendor_id == i.id } end - return product_list + + return product_list.flatten end def self.search(search_term) matches = [] - v = Vendor.all - m = Market.all + v = Vendor.all.values + m = Market.all.values # find and add matching vendor names - v_matches = v.find_all { |n| n[1].name.downcase.include?(search_term) } - - v_matches.each do |i| - matches << i[1] - end + matches << v.find_all { |n| n.name.downcase.include?(search_term) } # find and add matching market names - m_matches = m.find_all { |n| n[1].name.downcase.include?(search_term)} - - m_matches.each do |i| - matches << i[1] - end - # matches << m_matches + matches << m.find_all { |n| n.name.downcase.include?(search_term)} - return matches + return matches.flatten end def prefered_vendor vendors.max_by { |n| n.revenue } end + # def prefered_vendor(date) + # date = DateTime.parse(date) + # vendor_list = + # + # end + end From f92144955ca81b35ab4fd69daf216a04478c00a8 Mon Sep 17 00:00:00 2001 From: Danielle Schrimmer Date: Fri, 9 Sep 2016 14:43:17 -0700 Subject: [PATCH 30/33] Refactor find, market, products, sales, revenue, by_market methods to simplify steps. --- lib/farmar_vendor.rb | 36 ++++++++++++++---------------------- 1 file changed, 14 insertions(+), 22 deletions(-) diff --git a/lib/farmar_vendor.rb b/lib/farmar_vendor.rb index 04292d93..558c9de3 100644 --- a/lib/farmar_vendor.rb +++ b/lib/farmar_vendor.rb @@ -24,8 +24,7 @@ def self.all end def self.find(id) - v = Vendor.all - return v[id] + return all[id] end def market @@ -33,41 +32,34 @@ def market end def products - product_list = Product.all - match = product_list.find_all { |n| n[1].vendor_id == @id } + product_list = Product.all.values product_instances = [] - match.length.times do |i| - product_instances << match[i][1] - end - return product_instances + product_instances << product_list.find_all { |n| n.vendor_id == @id } + + return product_instances.flatten end def sales - sales_list = Sale.all - match = sales_list.find_all { |n| n[1].vendor_id == @id } + sales_list = Sale.all.values sales_instances = [] - match.length.times do |i| - sales_instances << match[i][1] - end - return sales_instances + sales_instances << sales_list.find_all { |n| n.vendor_id == @id } + + return sales_instances.flatten end def revenue total_revenue = 0 - sales_instances = sales - sales_instances.each do |i| + sales.each do |i| total_revenue += i.amount end + return total_revenue end def self.by_market(market_id) - a = all.find_all { |n| n[1].market_id == market_id } - vendors_by_market = [] - a.length.times do |i| - vendors_by_market << a[i][1] - end - return vendors_by_market + vendors_by_market << all.values.find_all { |n| n.market_id == market_id } + + return vendors_by_market.flatten end end From 281a29f5f201e516334ea64585aba249773131c1 Mon Sep 17 00:00:00 2001 From: Danielle Schrimmer Date: Fri, 9 Sep 2016 15:00:37 -0700 Subject: [PATCH 31/33] Refactor most product method to simplify. --- lib/farmar_product.rb | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/lib/farmar_product.rb b/lib/farmar_product.rb index febf3bf8..9b530250 100644 --- a/lib/farmar_product.rb +++ b/lib/farmar_product.rb @@ -19,13 +19,13 @@ def self.all products[id] = self.new(id, name, vendor_id) # products[line[0].to_i] = (self.new(id: line[0].to_i, name: line[1], vendor_id: line[2].to_i)) + # ^^ I want this to work but I can't seem to find what's going wrong with the process. end return products end def self.find(id) - p = Product.all - return p[id] + return all[id] end def vendor @@ -33,25 +33,20 @@ def vendor end def sales - s = Sale.all - sale_instances = s.find_all { |n| n[1].product_id == @id } - return sale_instances + return Sale.all.values.find_all { |n| n.product_id == @id } end def number_of_sales - s = Sale.all - sale_instances = s.find_all { |n| n[1].product_id == @id } + sale_instances = Sale.all.values.find_all { |n| n.product_id == @id } + return sale_instances.length end def self.by_vendor(vendor_id) - p = all.find_all { |n| n[1].vendor_id == vendor_id } - product_list = [] - p.length.times do |i| - product_list << p[i][1] - end - return product_list + product_list << all.values.find_all { |n| n.vendor_id == vendor_id } + + return product_list.flatten end end From 4d965df32559f2e0e03ab1bdd3ab4123d7c9c8c6 Mon Sep 17 00:00:00 2001 From: Danielle Schrimmer Date: Fri, 9 Sep 2016 16:00:43 -0700 Subject: [PATCH 32/33] Market prefered_vendor(date) in progress --- lib/farmar_market.rb | 16 +++++++++++++++- lib/farmar_sale.rb | 8 +++++--- specs/farmar_market_spec.rb | 2 +- 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/lib/farmar_market.rb b/lib/farmar_market.rb index 94ded9fd..ea185e47 100644 --- a/lib/farmar_market.rb +++ b/lib/farmar_market.rb @@ -70,10 +70,24 @@ def prefered_vendor vendors.max_by { |n| n.revenue } end + # IN PROGRESS: # def prefered_vendor(date) # date = DateTime.parse(date) - # vendor_list = + # vendor_list = vendors # + # beginning_time = date.to_date + # end_time = beginning_time + 1 + # + # sales = Sale.between(beginning_time, end_time) + # + # market_sales = [] + # + # sales.each do |i| + # if vendor_list.include?(i.vendor_id) + # market_sales << i + # end + # end + # return market_sales.max_by { |n| n.revenue } # end end diff --git a/lib/farmar_sale.rb b/lib/farmar_sale.rb index b78bea68..25437a72 100644 --- a/lib/farmar_sale.rb +++ b/lib/farmar_sale.rb @@ -40,9 +40,11 @@ def product end def self.between(beginning_time, end_time) - beginning_time = DateTime.parse(beginning_time) - end_time = DateTime.parse(end_time) + if beginning_time.class != Date + beginning_time = DateTime.parse(beginning_time) + end_time = DateTime.parse(end_time) + end - Sale.all.find_all { |n| n[1].purchase_time.between?(beginning_time, end_time)} + Sale.all.values.find_all { |n| n.purchase_time.between?(beginning_time, end_time)} end end diff --git a/specs/farmar_market_spec.rb b/specs/farmar_market_spec.rb index 82fdc8ba..8e01435a 100644 --- a/specs/farmar_market_spec.rb +++ b/specs/farmar_market_spec.rb @@ -58,7 +58,7 @@ # describe "#prefered_vendor(date)" do # it "should return the vendor with the highest revenue for the given date" do - # + # m.prefered_vendor(2013-11-11).class.must_equal(Vendor) # end # end From 2758c1dfdf3bf4d9d46507d748549c46717e6a13 Mon Sep 17 00:00:00 2001 From: Danielle Schrimmer Date: Fri, 9 Sep 2016 16:11:44 -0700 Subject: [PATCH 33/33] Edit Market to return all markets in search method, not market/vendor combo --- lib/farmar_market.rb | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/farmar_market.rb b/lib/farmar_market.rb index ea185e47..e00118bd 100644 --- a/lib/farmar_market.rb +++ b/lib/farmar_market.rb @@ -57,8 +57,13 @@ def self.search(search_term) v = Vendor.all.values m = Market.all.values - # find and add matching vendor names - matches << v.find_all { |n| n.name.downcase.include?(search_term) } + # find matching vendor names + vendors = [] + vendors << v.find_all { |n| n.name.downcase.include?(search_term) } + # find and add associated market + vendors.flatten.each do |i| + matches << i.market + end # find and add matching market names matches << m.find_all { |n| n.name.downcase.include?(search_term)} @@ -70,7 +75,7 @@ def prefered_vendor vendors.max_by { |n| n.revenue } end - # IN PROGRESS: + # IN PROGRESS: # def prefered_vendor(date) # date = DateTime.parse(date) # vendor_list = vendors