From 98b51d4ad1921222e2fb6588bc6848f36a4b8cad Mon Sep 17 00:00:00 2001 From: Allison Hoke Date: Tue, 6 Sep 2016 13:52:37 -0700 Subject: [PATCH 01/42] Completed baseline requirements. Need to do better at committing to git more often. --- .gitignore | 1 + Rakefile | 7 +++++++ far_mar.rb | 8 ++++++++ lib/market.rb | 35 +++++++++++++++++++++++++++++++++++ lib/product.rb | 22 ++++++++++++++++++++++ lib/sale.rb | 23 +++++++++++++++++++++++ lib/vendor.rb | 23 +++++++++++++++++++++++ specs/market_spec.rb | 22 ++++++++++++++++++++++ specs/product_spec.rb | 23 +++++++++++++++++++++++ specs/sale_spec.rb | 23 +++++++++++++++++++++++ specs/spec_helper.rb | 13 +++++++++++++ specs/vendor_spec.rb | 24 ++++++++++++++++++++++++ 12 files changed, 224 insertions(+) create mode 100644 .gitignore create mode 100644 Rakefile create mode 100644 far_mar.rb create mode 100644 lib/market.rb create mode 100644 lib/product.rb create mode 100644 lib/sale.rb create mode 100644 lib/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/.gitignore b/.gitignore new file mode 100644 index 00000000..7053dc17 --- /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..04dd7fab --- /dev/null +++ b/far_mar.rb @@ -0,0 +1,8 @@ +require 'csv' +require_relative './lib/market' +require_relative './lib/product' +require_relative './lib/sale' +require_relative './lib/vendor' + +module FarMar +end diff --git a/lib/market.rb b/lib/market.rb new file mode 100644 index 00000000..efe852dc --- /dev/null +++ b/lib/market.rb @@ -0,0 +1,35 @@ +require 'csv' + +module FarMar + class Market + + def initialize(market_hash) + # + # @id = market_hash[:id] + # @name = market_hash[:name] + # @address = market_hash[:address] + # @city = market_hash[:city] + # @state = market_hash[:state] + # @zip = market_hash[:zip] + + end + + def self.all + markets = [] #array to store all of the hashes with market info + CSV.read("../FarMar/support/markets.csv").each do |line| + market = {} #create a new hash for each market to store specific info + + market[:id] = line[0].to_i + market[:name] = line[1] + market[:address] = line[2] + market[:city] = line[3] + market[:state] = line[4] + market[:zip] = line[5] + + markets << self.new(market) #creates a new instance with the hash info and puts it in the array to be returned + end + markets #returns this array + end + + end +end diff --git a/lib/product.rb b/lib/product.rb new file mode 100644 index 00000000..09f7a739 --- /dev/null +++ b/lib/product.rb @@ -0,0 +1,22 @@ +module FarMar + class Product + + def initialize(product_hash) + end + + def self.all + products = [] #array to store all of the hashes with product info + CSV.read("../FarMar/support/products.csv").each do |line| + product = {} #create a new hash for each product to store specific info + + product[:id] = line[0].to_i + product[:name] = line[1] + product[:vendor_id] = line[2].to_i + + products << self.new(product) #creates a new instance with the hash info and puts it in the array to be returned + end + products #returns this array + end + + end +end diff --git a/lib/sale.rb b/lib/sale.rb new file mode 100644 index 00000000..32959ea1 --- /dev/null +++ b/lib/sale.rb @@ -0,0 +1,23 @@ +module FarMar + class Sale + + def initialize(sale_hash) + end + + def self.all + sales = [] #array to store all of the hashes with sale info + CSV.read("../FarMar/support/sales.csv").each do |line| + sale = {} #create a new hash for each sale to store specific info + + sale[:id] = line[0].to_i + sale[:name] = line[1] + sale[:num_employees] = line[2].to_i + sale[:sale_id] = line[3].to_i + + sales << self.new(sale) #creates a new instance with the hash info and puts it in the array to be returned + end + sales #returns this array + end + + end +end diff --git a/lib/vendor.rb b/lib/vendor.rb new file mode 100644 index 00000000..6bd1316c --- /dev/null +++ b/lib/vendor.rb @@ -0,0 +1,23 @@ +module FarMar + class Vendor + + def initialize(vendor_hash) + end + + def self.all + vendors = [] #array to store all of the hashes with vendor info + CSV.read("../FarMar/support/vendors.csv").each do |line| + vendor = {} #create a new hash for each vendor to store specific info + + vendor[:id] = line[0].to_i + vendor[:name] = line[1] + vendor[:num_employees] = line[2].to_i + vendor[:vendor_id] = line[3].to_i + + vendors << self.new(vendor) #creates a new instance with the hash info and puts it in the array to be returned + end + vendors #returns this array + end + + end +end diff --git a/specs/market_spec.rb b/specs/market_spec.rb new file mode 100644 index 00000000..9b59856c --- /dev/null +++ b/specs/market_spec.rb @@ -0,0 +1,22 @@ +require_relative 'spec_helper' + +module FarMar + describe Market do + describe "#initialize" do + let(:new_market) { Market.new({}) } + + it "should create a new instace of Market" do + new_market.must_be_instance_of(Market) + end + + end + + describe ".all" do + let(:all_markets) { Market.all } + it "should return an array" do + all_markets.must_be_kind_of(Array) + end + end + + end +end diff --git a/specs/product_spec.rb b/specs/product_spec.rb new file mode 100644 index 00000000..9451d24c --- /dev/null +++ b/specs/product_spec.rb @@ -0,0 +1,23 @@ +require_relative 'spec_helper' + +module FarMar + describe Product do + + describe "#initialize" do + let(:new_product) { Product.new({}) } + + it "should create a new instace of Product" do + new_product.must_be_instance_of(Product) + end + end + + describe ".all" do + let(:all_products) { Product.all } + + it "should return an array" do + all_products.must_be_kind_of(Array) + end + end + + end +end diff --git a/specs/sale_spec.rb b/specs/sale_spec.rb new file mode 100644 index 00000000..d5c87b54 --- /dev/null +++ b/specs/sale_spec.rb @@ -0,0 +1,23 @@ +require_relative 'spec_helper' + +module FarMar + describe Sale do + + describe "#initialize" do + let(:new_sale) { Sale.new({}) } + + it "should create a new instace of Sale" do + new_sale.must_be_instance_of(Sale) + end + end + + describe ".all" do + let(:all_sales) { Sale.all } + + it "should return an array" do + all_sales.must_be_kind_of(Array) + end + end + + end +end diff --git a/specs/spec_helper.rb b/specs/spec_helper.rb new file mode 100644 index 00000000..6e851fe8 --- /dev/null +++ b/specs/spec_helper.rb @@ -0,0 +1,13 @@ +require 'simplecov' +SimpleCov.start + + +require 'minitest' +require 'minitest/spec' +require "minitest/autorun" +require "minitest/reporters" +require 'minitest/pride' + +require_relative "../far_mar" + +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..d361fcd4 --- /dev/null +++ b/specs/vendor_spec.rb @@ -0,0 +1,24 @@ +require_relative 'spec_helper' + +module FarMar + describe Vendor do + + describe "#initialize" do + let(:new_vendor) { Vendor.new({}) } + + it "should create a new instace of Vendor" do + new_vendor.must_be_instance_of(Vendor) + end + end + + describe ".all" do + let(:all_vendors) { Vendor.all } + + it "should return an array" do + all_vendors.must_be_kind_of(Array) + end + end + + + end +end From cc6b878dfc92961c106a80acc4f4d458c25ac637 Mon Sep 17 00:00:00 2001 From: Allison Hoke Date: Tue, 6 Sep 2016 15:39:40 -0700 Subject: [PATCH 02/42] created self.find methods for all classes --- lib/market.rb | 34 +++++++++++++++++++--------------- lib/product.rb | 18 +++++++++++++----- lib/sale.rb | 22 ++++++++++++++++------ lib/vendor.rb | 20 ++++++++++++++------ 4 files changed, 62 insertions(+), 32 deletions(-) diff --git a/lib/market.rb b/lib/market.rb index efe852dc..a7d3bf7f 100644 --- a/lib/market.rb +++ b/lib/market.rb @@ -2,34 +2,38 @@ module FarMar class Market + attr_reader :id, :name def initialize(market_hash) - # - # @id = market_hash[:id] - # @name = market_hash[:name] - # @address = market_hash[:address] - # @city = market_hash[:city] - # @state = market_hash[:state] - # @zip = market_hash[:zip] + + @id = market_hash[:id] + @name = market_hash[:name] + @address = market_hash[:address] + @city = market_hash[:city] + @county = market_hash[:county] + @state = market_hash[:state] + @zip = market_hash[:zip] end def self.all markets = [] #array to store all of the hashes with market info CSV.read("../FarMar/support/markets.csv").each do |line| - market = {} #create a new hash for each market to store specific info - - market[:id] = line[0].to_i - market[:name] = line[1] - market[:address] = line[2] - market[:city] = line[3] - market[:state] = line[4] - market[:zip] = line[5] + market = {id: line[0].to_i, name: line[1], address: line[2], city: line[3], county: line[4], state: line[5], zip: line[6]} #create a new hash for each market to store specific info markets << self.new(market) #creates a new instance with the hash info and puts it in the array to be returned end markets #returns this array end + def self.find(id) + self.all.each do |m| + if m.id == id + return m #returns the object whose id matches the argument + end + end + end + + end end diff --git a/lib/product.rb b/lib/product.rb index 09f7a739..bda81be2 100644 --- a/lib/product.rb +++ b/lib/product.rb @@ -1,22 +1,30 @@ module FarMar class Product + attr_reader :id, :name def initialize(product_hash) + @id = product_hash[:id] + @name = product_hash[:name] + @vendor_id = product_hash[:vendor_id] end def self.all products = [] #array to store all of the hashes with product info CSV.read("../FarMar/support/products.csv").each do |line| - product = {} #create a new hash for each product to store specific info - - product[:id] = line[0].to_i - product[:name] = line[1] - product[:vendor_id] = line[2].to_i + product = {id: line[0].to_i, name: line[1], vendor_id: line[2].to_i} #create a new hash for each product to store specific info products << self.new(product) #creates a new instance with the hash info and puts it in the array to be returned end products #returns this array end + def self.find(id) + self.all.each do |pro| + if pro.id == id + return pro #returns the object whose id matches the argument + end + end + end + end end diff --git a/lib/sale.rb b/lib/sale.rb index 32959ea1..dfef07bc 100644 --- a/lib/sale.rb +++ b/lib/sale.rb @@ -1,23 +1,33 @@ module FarMar class Sale + attr_reader :id, :amount def initialize(sale_hash) + @id = sale_hash[:id] + @amount = sale_hash[:amount] + @purchase_time = sale_hash[:purchase_time] + @vendor_id = sale_hash[:vendor_id] + @product_id = sale_hash[:product_id] end +# TO DO - figure out how to convert purchase_time to datetime data type def self.all sales = [] #array to store all of the hashes with sale info CSV.read("../FarMar/support/sales.csv").each do |line| - sale = {} #create a new hash for each sale to store specific info - - sale[:id] = line[0].to_i - sale[:name] = line[1] - sale[:num_employees] = line[2].to_i - sale[:sale_id] = line[3].to_i + sale = {id: line[0].to_i, amount: line[1].to_i, purchase_time: line[2], vendor_id: line[3].to_i, product_id: line[4].to_i } #create a new hash for each sale to store specific info sales << self.new(sale) #creates a new instance with the hash info and puts it in the array to be returned end sales #returns this array end + def self.find(id) + self.all.each do |s| + if s.id == id + return s #returns the object whose id matches the argument + end + end + end + end end diff --git a/lib/vendor.rb b/lib/vendor.rb index 6bd1316c..304dab34 100644 --- a/lib/vendor.rb +++ b/lib/vendor.rb @@ -1,23 +1,31 @@ module FarMar class Vendor + attr_reader :id, :name def initialize(vendor_hash) + @id = vendor_hash[:id] + @name = vendor_hash[:name] + @num_employees = vendor_hash[:num_employees] + @vendor_id = vendor_hash[:vendor_id] end def self.all vendors = [] #array to store all of the hashes with vendor info CSV.read("../FarMar/support/vendors.csv").each do |line| - vendor = {} #create a new hash for each vendor to store specific info - - vendor[:id] = line[0].to_i - vendor[:name] = line[1] - vendor[:num_employees] = line[2].to_i - vendor[:vendor_id] = line[3].to_i + vendor = {id: line[0].to_i, name: line[1], num_employees: line[2].to_i, vendor_id: line[3].to_i} #create a new hash for each vendor to store specific info vendors << self.new(vendor) #creates a new instance with the hash info and puts it in the array to be returned end vendors #returns this array end + def self.find(id) + self.all.each do |v| + if v.id == id + return v #returns the object whose id matches the argument + end + end + end + end end From 9c83981b7e1f7bf0b1854bf1b8902917feacd909 Mon Sep 17 00:00:00 2001 From: Allison Hoke Date: Tue, 6 Sep 2016 15:40:31 -0700 Subject: [PATCH 03/42] created tests for self.find methods for each class. all tests passed. --- specs/market_spec.rb | 18 ++++++++++++++++-- specs/product_spec.rb | 15 +++++++++++++++ specs/sale_spec.rb | 15 +++++++++++++++ specs/vendor_spec.rb | 15 +++++++++++++++ 4 files changed, 61 insertions(+), 2 deletions(-) diff --git a/specs/market_spec.rb b/specs/market_spec.rb index 9b59856c..efc5d8e5 100644 --- a/specs/market_spec.rb +++ b/specs/market_spec.rb @@ -12,11 +12,25 @@ module FarMar end describe ".all" do - let(:all_markets) { Market.all } + it "should return an array" do - all_markets.must_be_kind_of(Array) + Market.all.must_be_kind_of(Array) + end + + it "should return an object: Market at any index of the array" do + Market.all[0].must_be_instance_of(Market) + Market.all[10].must_be_instance_of(Market) + Market.all[20].must_be_instance_of(Market) end end + describe ".find(id)" do + it "should return an object: Market with name Scottdale Farmers Market at ID 20" do + scots = Market.find(20) + + scots.must_be_instance_of(Market) + scots.name.must_equal("Scottdale Farmers Market") + end + end end end diff --git a/specs/product_spec.rb b/specs/product_spec.rb index 9451d24c..6df09bed 100644 --- a/specs/product_spec.rb +++ b/specs/product_spec.rb @@ -17,6 +17,21 @@ module FarMar it "should return an array" do all_products.must_be_kind_of(Array) end + + it "should return an object: Product at any index of the array" do + all_products[0].must_be_instance_of(Product) + all_products[10].must_be_instance_of(Product) + all_products[20].must_be_instance_of(Product) + end + end + + describe ".find(id)" do + it "should return an object: Product with name Tall Pretzel at ID 20" do + tp = Product.find(20) + + tp.must_be_instance_of(Product) + tp.name.must_equal("Tall Pretzel") + end end end diff --git a/specs/sale_spec.rb b/specs/sale_spec.rb index d5c87b54..038e8229 100644 --- a/specs/sale_spec.rb +++ b/specs/sale_spec.rb @@ -17,6 +17,21 @@ module FarMar it "should return an array" do all_sales.must_be_kind_of(Array) end + + it "should return an object: Sale at any index of the array" do + all_sales[0].must_be_instance_of(Sale) + all_sales[10].must_be_instance_of(Sale) + all_sales[20].must_be_instance_of(Sale) + end + end + + describe ".find(id)" do + it "should return an object: Sale with amount 51 at ID 20" do + tp = Sale.find(20) + + tp.must_be_instance_of(Sale) + tp.amount.must_equal(51) + end end end diff --git a/specs/vendor_spec.rb b/specs/vendor_spec.rb index d361fcd4..adc1c38a 100644 --- a/specs/vendor_spec.rb +++ b/specs/vendor_spec.rb @@ -17,6 +17,21 @@ module FarMar it "should return an array" do all_vendors.must_be_kind_of(Array) end + + it "should return an object: Vendor at any index of the array" do + all_vendors[0].must_be_instance_of(Vendor) + all_vendors[10].must_be_instance_of(Vendor) + all_vendors[20].must_be_instance_of(Vendor) + end + end + + describe ".find(id)" do + it "should return an object: Vendor with name Ledner Group at ID 20" do + led = Vendor.find(20) + + led.must_be_instance_of(Vendor) + led.name.must_equal("Ledner Group") + end end From 9d032a89cf4d973751c99bd28405b47ff31d5836 Mon Sep 17 00:00:00 2001 From: Allison Hoke Date: Tue, 6 Sep 2016 17:06:43 -0700 Subject: [PATCH 04/42] added new methods up to Vendor #products adn tests. all tests passing. 100% covered. --- lib/market.rb | 23 ++++++++++++++++++++--- lib/vendor.rb | 18 +++++++++++++++--- specs/market_spec.rb | 27 +++++++++++++++++++++++++-- specs/vendor_spec.rb | 13 +++++++++++++ 4 files changed, 73 insertions(+), 8 deletions(-) diff --git a/lib/market.rb b/lib/market.rb index a7d3bf7f..1b19646f 100644 --- a/lib/market.rb +++ b/lib/market.rb @@ -1,5 +1,3 @@ -require 'csv' - module FarMar class Market attr_reader :id, :name @@ -19,7 +17,15 @@ def initialize(market_hash) def self.all markets = [] #array to store all of the hashes with market info CSV.read("../FarMar/support/markets.csv").each do |line| - market = {id: line[0].to_i, name: line[1], address: line[2], city: line[3], county: line[4], state: line[5], zip: line[6]} #create a new hash for each market to store specific info + market = { + id: line[0].to_i, + name: line[1], + address: line[2], + city: line[3], + county: line[4], + state: line[5], + zip: line[6] + } #create a new hash for each market to store specific info markets << self.new(market) #creates a new instance with the hash info and puts it in the array to be returned end @@ -34,6 +40,17 @@ def self.find(id) end end + def vendors(market_id) #returns a collection of FarMar::Vendor instances that are associated with the market by the market_id field + market_vendors = [] + + vendors = FarMar::Vendor.all #iterates over all vendors + vendors.each do |v| + if v.market_id == market_id #finds vendors whose ids match argument + market_vendors << v #pushes them to array of all vendors at that market + end + end + return market_vendors + end end end diff --git a/lib/vendor.rb b/lib/vendor.rb index 304dab34..cc5ae61f 100644 --- a/lib/vendor.rb +++ b/lib/vendor.rb @@ -1,18 +1,18 @@ module FarMar class Vendor - attr_reader :id, :name + attr_reader :id, :name, :num_employees, :market_id def initialize(vendor_hash) @id = vendor_hash[:id] @name = vendor_hash[:name] @num_employees = vendor_hash[:num_employees] - @vendor_id = vendor_hash[:vendor_id] + @market_id = vendor_hash[:market_id] end def self.all vendors = [] #array to store all of the hashes with vendor info CSV.read("../FarMar/support/vendors.csv").each do |line| - vendor = {id: line[0].to_i, name: line[1], num_employees: line[2].to_i, vendor_id: line[3].to_i} #create a new hash for each vendor to store specific info + vendor = {id: line[0].to_i, name: line[1], num_employees: line[2].to_i, market_id: line[3].to_i} #create a new hash for each vendor to store specific info vendors << self.new(vendor) #creates a new instance with the hash info and puts it in the array to be returned end @@ -27,5 +27,17 @@ def self.find(id) end end + def market + market_id = @market_id + + all_markets = FarMar::Market.all + all_markets.each do |m| + if m.id == market_id + return m + end + end + end + + end end diff --git a/specs/market_spec.rb b/specs/market_spec.rb index efc5d8e5..78a60842 100644 --- a/specs/market_spec.rb +++ b/specs/market_spec.rb @@ -2,17 +2,16 @@ module FarMar describe Market do + describe "#initialize" do let(:new_market) { Market.new({}) } it "should create a new instace of Market" do new_market.must_be_instance_of(Market) end - end describe ".all" do - it "should return an array" do Market.all.must_be_kind_of(Array) end @@ -32,5 +31,29 @@ module FarMar scots.name.must_equal("Scottdale Farmers Market") end end + + describe "#vendors" do + let(:example_market) { Market.new({}) } + + it "returns an array" do + example_market.vendors(4).must_be_kind_of(Array) + end + + it "should return an object: Vendor at any index of the array" do + four = example_market.vendors(4) + + four[0].must_be_instance_of(Vendor) + four[1].must_be_instance_of(Vendor) + four[2].must_be_instance_of(Vendor) + end + + it "should return the correct market_id that matches the one searched" do + four = example_market.vendors(4) + + four[0].market_id.must_equal(4) + four[1].market_id.must_equal(4) + four[2].market_id.must_equal(4) + end + end end end diff --git a/specs/vendor_spec.rb b/specs/vendor_spec.rb index adc1c38a..755e5850 100644 --- a/specs/vendor_spec.rb +++ b/specs/vendor_spec.rb @@ -34,6 +34,19 @@ module FarMar end end + describe "#market" do + let(:example_vendor) { Vendor.new({id: 9876, name: "Allison", num_employees: 75, market_id: 6}) } + + it "should return an object: Market" do + example_vendor.market.must_be_instance_of(Market) + end + + it "should return the correct instance of Market" do + jcfm = example_vendor.market + jcfm.name.must_equal("Jefferson City Farmer's Market") + jcfm.id.must_equal(6) + end + end end end From c8513942908035ba602a958c3160f133a86e568c Mon Sep 17 00:00:00 2001 From: Allison Hoke Date: Wed, 7 Sep 2016 13:49:37 -0700 Subject: [PATCH 05/42] deleted a space --- lib/vendor.rb | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/lib/vendor.rb b/lib/vendor.rb index cc5ae61f..9843b6a2 100644 --- a/lib/vendor.rb +++ b/lib/vendor.rb @@ -9,7 +9,7 @@ def initialize(vendor_hash) @market_id = vendor_hash[:market_id] end - def self.all + def self.all #returns an array of objects:Vendors vendors = [] #array to store all of the hashes with vendor info CSV.read("../FarMar/support/vendors.csv").each do |line| vendor = {id: line[0].to_i, name: line[1], num_employees: line[2].to_i, market_id: line[3].to_i} #create a new hash for each vendor to store specific info @@ -19,7 +19,7 @@ def self.all vendors #returns this array end - def self.find(id) + def self.find(id) #returns the object:Vendor with arg. id self.all.each do |v| if v.id == id return v #returns the object whose id matches the argument @@ -27,17 +27,20 @@ def self.find(id) end end - def market - market_id = @market_id + def market #returns the object:Market that this vendor belongs to + market_id = @market_id #this instance's market_id all_markets = FarMar::Market.all all_markets.each do |m| - if m.id == market_id + if m.id == market_id #find the market with this id return m end end end - + def products #returns an array of object:Products that belong to this Vendor + + end + end end From 7a838142a22c7de84effb37a901646287e0d5649 Mon Sep 17 00:00:00 2001 From: Allison Hoke Date: Wed, 7 Sep 2016 14:23:09 -0700 Subject: [PATCH 06/42] added vendor_id to attr_reader --- lib/sale.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/sale.rb b/lib/sale.rb index dfef07bc..2b439f11 100644 --- a/lib/sale.rb +++ b/lib/sale.rb @@ -1,6 +1,6 @@ module FarMar class Sale - attr_reader :id, :amount + attr_reader :id, :amount, :vendor_id def initialize(sale_hash) @id = sale_hash[:id] From 2255bf9ac7e32f3fd60ef0354414861513f13bbb Mon Sep 17 00:00:00 2001 From: Allison Hoke Date: Wed, 7 Sep 2016 14:23:30 -0700 Subject: [PATCH 07/42] added vendor_id to attr_reader --- lib/product.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/product.rb b/lib/product.rb index bda81be2..f7488b2a 100644 --- a/lib/product.rb +++ b/lib/product.rb @@ -1,6 +1,6 @@ module FarMar class Product - attr_reader :id, :name + attr_reader :id, :name, :vendor_id def initialize(product_hash) @id = product_hash[:id] From 199c4c7c49ca011dc7db6c525dcf3671a6b5fdc7 Mon Sep 17 00:00:00 2001 From: Allison Hoke Date: Wed, 7 Sep 2016 14:23:58 -0700 Subject: [PATCH 08/42] added products and sales methods --- lib/vendor.rb | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/lib/vendor.rb b/lib/vendor.rb index 9843b6a2..376c7975 100644 --- a/lib/vendor.rb +++ b/lib/vendor.rb @@ -39,7 +39,27 @@ def market #returns the object:Market that this vendor belongs to end def products #returns an array of object:Products that belong to this Vendor + ven_id = @id + vendor_products = [] + FarMar::Product.all.each do |pro| + if pro.vendor_id == ven_id + vendor_products << pro + end + end + return vendor_products + end + + def sales #returns a collection of object:Sales that are associated with this vendor + ven_id = @id + vendor_sales = [] + + FarMar::Sales.all.each do |s| + if s.vendor_id == ven_id + vendor_sales << s + end + end + return vendor_sales end end From 1eb7aa14d5b6844bdbcabdc9f31ed663a3776da5 Mon Sep 17 00:00:00 2001 From: Allison Hoke Date: Wed, 7 Sep 2016 14:24:19 -0700 Subject: [PATCH 09/42] created tests for products and sales methods. all tests pass --- specs/vendor_spec.rb | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/specs/vendor_spec.rb b/specs/vendor_spec.rb index 755e5850..8eff3ebe 100644 --- a/specs/vendor_spec.rb +++ b/specs/vendor_spec.rb @@ -48,5 +48,20 @@ module FarMar end end + describe "#products" do + let(:example_vendor) { Vendor.new({id: 5, name: "Allison", num_employees: 75, market_id: 6}) } + + it "should return an array" do + example_vendor.products.must_be_kind_of(Array) + end + + it "should return an object: Product at any index of the array" do + #puts example_vendor.products + #puts example_vendor.products[0] + + example_vendor.products[0].must_be_instance_of(Product) + example_vendor.products[2].must_be_instance_of(Product) + end + end end end From d6c2d2cc59fa95685de3fbca179b65918487f960 Mon Sep 17 00:00:00 2001 From: Allison Hoke Date: Wed, 7 Sep 2016 14:54:45 -0700 Subject: [PATCH 10/42] created a revenue method --- lib/vendor.rb | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/vendor.rb b/lib/vendor.rb index 376c7975..0eabcbe1 100644 --- a/lib/vendor.rb +++ b/lib/vendor.rb @@ -54,7 +54,7 @@ def sales #returns a collection of object:Sales that are associated with this ve ven_id = @id vendor_sales = [] - FarMar::Sales.all.each do |s| + FarMar::Sale.all.each do |s| if s.vendor_id == ven_id vendor_sales << s end @@ -62,5 +62,15 @@ def sales #returns a collection of object:Sales that are associated with this ve return vendor_sales end + def revenue #returns the sum of all the vendor's sales + all_sales = self.sales + total = 0 + + all_sales.each do |s| + total += s.amount + end + return total + end + end end From aeba3e41b209232028028ec86e7dde7870b88429 Mon Sep 17 00:00:00 2001 From: Allison Hoke Date: Wed, 7 Sep 2016 14:55:48 -0700 Subject: [PATCH 11/42] created test for revenue method. WIP, write a better test --- specs/vendor_spec.rb | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/specs/vendor_spec.rb b/specs/vendor_spec.rb index 8eff3ebe..d4c8f769 100644 --- a/specs/vendor_spec.rb +++ b/specs/vendor_spec.rb @@ -61,7 +61,40 @@ module FarMar example_vendor.products[0].must_be_instance_of(Product) example_vendor.products[2].must_be_instance_of(Product) + #REVIEW should I do better at calling an index - for example, should I say .products[rand(0..length-1??)] end + + it "each object should have the correct vendor id" do #IS THIS A BETTER TEST? + example_vendor.products[0].vendor_id.must_equal(5) + example_vendor.products[2].vendor_id.must_equal(5) + end + end + + describe "#sales" do + let(:example_vendor) { Vendor.new({id: 5, name: "Allison", num_employees: 75, market_id: 6}) } + + it "should return an array" do + example_vendor.sales.must_be_kind_of(Array) + end + + it "should return an object:Sale at any index of the array" do + #puts example_vendor.sales + + example_vendor.sales[0].must_be_instance_of(Sale) + example_vendor.sales[8].must_be_instance_of(Sale) + end + end + + describe "#revenue" do + let(:example_vendor) { Vendor.new({id: 5, name: "Allison", num_employees: 75, market_id: 6}) } + + it "should return a fixnum" do + example_vendor.revenue.must_be_kind_of(Fixnum) + end + + # it "should return the correct amount" do #is this a good test since it relys on this particular set of data + # puts example_vendor.revenue + # end end end end From ae74db9a93fb5f772a668864809be22ee328c5b0 Mon Sep 17 00:00:00 2001 From: Allison Hoke Date: Wed, 7 Sep 2016 16:47:28 -0700 Subject: [PATCH 12/42] changed #vendors method to call class method from Vendors class --- lib/market.rb | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/lib/market.rb b/lib/market.rb index 1b19646f..a46d373b 100644 --- a/lib/market.rb +++ b/lib/market.rb @@ -23,7 +23,7 @@ def self.all address: line[2], city: line[3], county: line[4], - state: line[5], + state: line[5], zip: line[6] } #create a new hash for each market to store specific info @@ -40,16 +40,18 @@ def self.find(id) end end - def vendors(market_id) #returns a collection of FarMar::Vendor instances that are associated with the market by the market_id field - market_vendors = [] - - vendors = FarMar::Vendor.all #iterates over all vendors - vendors.each do |v| - if v.market_id == market_id #finds vendors whose ids match argument - market_vendors << v #pushes them to array of all vendors at that market - end - end - return market_vendors + def vendors #returns a collection of FarMar::Vendor instances that are associated with the market by the market_id field + FarMar::Vendor.by_market(@id) + + # market_vendors = [] + # #use .select + # vendors = FarMar::Vendor.all #iterates over all vendors + # vendors.each do |v| + # if v.market_id == market_id #finds vendors whose ids match argument + # market_vendors << v #pushes them to array of all vendors at that market + # end + # end + # return market_vendors end end From 2f9ed1a5f1bd66a41ed6c8b32c0fb577f1818845 Mon Sep 17 00:00:00 2001 From: Allison Hoke Date: Wed, 7 Sep 2016 16:48:14 -0700 Subject: [PATCH 13/42] adjusted test for vendors method due to addition of class method in Vendor that does the same job - new # of arguments --- specs/market_spec.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/specs/market_spec.rb b/specs/market_spec.rb index 78a60842..8486b88b 100644 --- a/specs/market_spec.rb +++ b/specs/market_spec.rb @@ -33,14 +33,14 @@ module FarMar end describe "#vendors" do - let(:example_market) { Market.new({}) } + let(:example_market) { Market.new({id: 4, name: "Allison's Market", address: "address", city: "city", county: "county", state: "state", zip: "zip"}) } it "returns an array" do - example_market.vendors(4).must_be_kind_of(Array) + example_market.vendors.must_be_kind_of(Array) end it "should return an object: Vendor at any index of the array" do - four = example_market.vendors(4) + four = example_market.vendors four[0].must_be_instance_of(Vendor) four[1].must_be_instance_of(Vendor) @@ -48,7 +48,7 @@ module FarMar end it "should return the correct market_id that matches the one searched" do - four = example_market.vendors(4) + four = example_market.vendors four[0].market_id.must_equal(4) four[1].market_id.must_equal(4) From 008feff76b1b757d9efc020b4ee3eec2ba2b4462 Mon Sep 17 00:00:00 2001 From: Allison Hoke Date: Wed, 7 Sep 2016 16:48:52 -0700 Subject: [PATCH 14/42] added self.by_market(market_id) method --- lib/vendor.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/lib/vendor.rb b/lib/vendor.rb index 0eabcbe1..ab9ddef1 100644 --- a/lib/vendor.rb +++ b/lib/vendor.rb @@ -72,5 +72,15 @@ def revenue #returns the sum of all the vendor's sales return total end + def self.by_market(market_id) #returns an array of the vendors with the given market_id + market_vendors = [] + #use .select + self.all.each do |v| + if v.market_id == market_id #finds vendors whose ids match argument + market_vendors << v #pushes them to array of all vendors at that market + end + end + return market_vendors + end end end From c86c2416f8978964c574a8ae62627377439badb1 Mon Sep 17 00:00:00 2001 From: Allison Hoke Date: Wed, 7 Sep 2016 16:49:27 -0700 Subject: [PATCH 15/42] finished test for revenue - all tests pass. created tests for self.by_market method. all tests pass --- specs/vendor_spec.rb | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/specs/vendor_spec.rb b/specs/vendor_spec.rb index d4c8f769..57030726 100644 --- a/specs/vendor_spec.rb +++ b/specs/vendor_spec.rb @@ -65,8 +65,8 @@ module FarMar end it "each object should have the correct vendor id" do #IS THIS A BETTER TEST? - example_vendor.products[0].vendor_id.must_equal(5) - example_vendor.products[2].vendor_id.must_equal(5) + example_vendor.products[0].vendor_id.must_equal(example_vendor.id) + example_vendor.products[2].vendor_id.must_equal(example_vendor.id) end end @@ -83,6 +83,10 @@ module FarMar example_vendor.sales[0].must_be_instance_of(Sale) example_vendor.sales[8].must_be_instance_of(Sale) end + + it "each sale should have the correct vendor id" do + example_vendor.sales[0].vendor_id.must_equal(example_vendor.id) + end end describe "#revenue" do @@ -92,9 +96,22 @@ module FarMar example_vendor.revenue.must_be_kind_of(Fixnum) end - # it "should return the correct amount" do #is this a good test since it relys on this particular set of data - # puts example_vendor.revenue - # end + it "should return the correct amount" do + example_vendor.revenue.must_equal(61749) + end + end + + describe "self.by_market(market_id)" do + let(:vendors_by_market) { Vendor.by_market(4) } + + it "should return an array" do + vendors_by_market.must_be_kind_of(Array) + end + + it "should have an object:Vendor at any index of the array" do + vendors_by_market[0].must_be_instance_of(Vendor) + vendors_by_market[3].must_be_instance_of(Vendor) + end end end end From cfc5de59b5f7d627a2d77a89841c85f45be02558 Mon Sep 17 00:00:00 2001 From: Allison Hoke Date: Wed, 7 Sep 2016 16:53:43 -0700 Subject: [PATCH 16/42] wrote additional/more specific test for self.by_market method --- specs/vendor_spec.rb | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/specs/vendor_spec.rb b/specs/vendor_spec.rb index 57030726..16c00a10 100644 --- a/specs/vendor_spec.rb +++ b/specs/vendor_spec.rb @@ -112,6 +112,12 @@ module FarMar vendors_by_market[0].must_be_instance_of(Vendor) vendors_by_market[3].must_be_instance_of(Vendor) end + + it "should return the correct vendors" do + vendors_by_market[0].id.must_equal(13) + vendors_by_market[2].id.must_equal(15) + vendors_by_market[3].id.must_equal(16) + end end end end From 1937ef94f9743182dfd98f2eea5d1724e9a415b2 Mon Sep 17 00:00:00 2001 From: Allison Hoke Date: Wed, 7 Sep 2016 16:54:35 -0700 Subject: [PATCH 17/42] deleted unnecessary comments --- lib/market.rb | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/lib/market.rb b/lib/market.rb index a46d373b..b0670ebf 100644 --- a/lib/market.rb +++ b/lib/market.rb @@ -40,18 +40,8 @@ def self.find(id) end end - def vendors #returns a collection of FarMar::Vendor instances that are associated with the market by the market_id field + def vendors #returns a collection of FarMar::Vendor instances that are associated with the market FarMar::Vendor.by_market(@id) - - # market_vendors = [] - # #use .select - # vendors = FarMar::Vendor.all #iterates over all vendors - # vendors.each do |v| - # if v.market_id == market_id #finds vendors whose ids match argument - # market_vendors << v #pushes them to array of all vendors at that market - # end - # end - # return market_vendors end end From a6add833b054ebdbf2a76ac9f60d12f8464bd650 Mon Sep 17 00:00:00 2001 From: Allison Hoke Date: Thu, 8 Sep 2016 09:06:37 -0700 Subject: [PATCH 18/42] wrote comment about idea to improve code --- lib/sale.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/sale.rb b/lib/sale.rb index 2b439f11..fdda0bf0 100644 --- a/lib/sale.rb +++ b/lib/sale.rb @@ -12,6 +12,7 @@ def initialize(sale_hash) # TO DO - figure out how to convert purchase_time to datetime data type def self.all + # IDEA make this an instance variable so you only have to make it run once sales = [] #array to store all of the hashes with sale info CSV.read("../FarMar/support/sales.csv").each do |line| sale = {id: line[0].to_i, amount: line[1].to_i, purchase_time: line[2], vendor_id: line[3].to_i, product_id: line[4].to_i } #create a new hash for each sale to store specific info From 8a941bdcddce76b6913aa01121789ff4173338be Mon Sep 17 00:00:00 2001 From: Allison Hoke Date: Thu, 8 Sep 2016 11:03:13 -0700 Subject: [PATCH 19/42] created vendor method --- lib/product.rb | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/lib/product.rb b/lib/product.rb index f7488b2a..84277d39 100644 --- a/lib/product.rb +++ b/lib/product.rb @@ -26,5 +26,16 @@ def self.find(id) end end + def vendor #returns the object:Vendor instance that is associated with Product + all_vendors = FarMar::Vendor.all + + all_vendors.each do |v| + if v.id == @vendor_id + return v + end + end + end + + end end From b2f3931091b261ec99ebff56f024a8df13b9b828 Mon Sep 17 00:00:00 2001 From: Allison Hoke Date: Thu, 8 Sep 2016 11:03:32 -0700 Subject: [PATCH 20/42] created tests for vendor method. all tests pass. --- specs/product_spec.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/specs/product_spec.rb b/specs/product_spec.rb index 6df09bed..b92d401f 100644 --- a/specs/product_spec.rb +++ b/specs/product_spec.rb @@ -34,5 +34,17 @@ module FarMar end end + describe "#vendor" do + let(:new_product) { Product.new({id: 213, name: "greens", vendor_id: 4}) } + + it "should return a object:Vendor" do + new_product.vendor.must_be_instance_of(Vendor) + end + + it "should return the correct Vendor" do + new_product.vendor.name.must_equal("Kris and Sons") + end + end + end end From 704a6b940ffa86be952cd0dffabd0312bc92dee2 Mon Sep 17 00:00:00 2001 From: Allison Hoke Date: Thu, 8 Sep 2016 12:19:29 -0700 Subject: [PATCH 21/42] added require 'time' --- far_mar.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/far_mar.rb b/far_mar.rb index 04dd7fab..cbaf6d9c 100644 --- a/far_mar.rb +++ b/far_mar.rb @@ -1,4 +1,5 @@ require 'csv' +require 'time' require_relative './lib/market' require_relative './lib/product' require_relative './lib/sale' From 8c9cb24cc697d0cfbc282c639061642f8e8c6937 Mon Sep 17 00:00:00 2001 From: Allison Hoke Date: Thu, 8 Sep 2016 12:20:21 -0700 Subject: [PATCH 22/42] bad git message - cleaned up method code, used .find method --- lib/product.rb | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/lib/product.rb b/lib/product.rb index 84277d39..d9797f52 100644 --- a/lib/product.rb +++ b/lib/product.rb @@ -27,15 +27,34 @@ def self.find(id) end def vendor #returns the object:Vendor instance that is associated with Product - all_vendors = FarMar::Vendor.all + FarMar::Vendor.find(@vendor_id) + end + + def sales #returns an array of Sale objects that are accociated with this product + all_sales = FarMar::Sale.all + product_sales = [] - all_vendors.each do |v| - if v.id == @vendor_id - return v + all_sales.each do |s| + if s.product_id == @id + product_sales << s end end + return product_sales + end + + def number_of_sales #returns the number of times this product has been sold + sales.length end + def self.by_vendor(vendor_id) #returns all the products associated with the given vendor_id + vendor_products = [] + FarMar::Product.all.each do |pro| + if pro.vendor_id == vendor_id + vendor_products << pro + end + end + return vendor_products + end end end From 6794b233f622c549d207ea7d135a669412307ee8 Mon Sep 17 00:00:00 2001 From: Allison Hoke Date: Thu, 8 Sep 2016 12:20:52 -0700 Subject: [PATCH 23/42] bad commit message - cleans up method code. used .find method where applicable --- lib/vendor.rb | 19 ++----------------- 1 file changed, 2 insertions(+), 17 deletions(-) diff --git a/lib/vendor.rb b/lib/vendor.rb index ab9ddef1..33906496 100644 --- a/lib/vendor.rb +++ b/lib/vendor.rb @@ -28,26 +28,11 @@ def self.find(id) #returns the object:Vendor with arg. id end def market #returns the object:Market that this vendor belongs to - market_id = @market_id #this instance's market_id - - all_markets = FarMar::Market.all - all_markets.each do |m| - if m.id == market_id #find the market with this id - return m - end - end + FarMar::Market.find(@market_id) end def products #returns an array of object:Products that belong to this Vendor - ven_id = @id - vendor_products = [] - - FarMar::Product.all.each do |pro| - if pro.vendor_id == ven_id - vendor_products << pro - end - end - return vendor_products + FarMar::Product.by_vendor(@id) end def sales #returns a collection of object:Sales that are associated with this vendor From 380d0c982a5d4e59c444637acddd30042a12e535 Mon Sep 17 00:00:00 2001 From: Allison Hoke Date: Thu, 8 Sep 2016 12:21:20 -0700 Subject: [PATCH 24/42] adjusted tests for cleaned up methods --- specs/product_spec.rb | 53 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/specs/product_spec.rb b/specs/product_spec.rb index b92d401f..94a89307 100644 --- a/specs/product_spec.rb +++ b/specs/product_spec.rb @@ -46,5 +46,58 @@ module FarMar end end + describe "#sales" do + let(:new_product) { Product.new({id: 21, name: "greens", vendor_id: 4}) } + + it "should return an array" do + new_product.sales.must_be_kind_of(Array) + end + + it "should have an object:Sale at any index of that array" do + #puts new_product.sales + new_product.sales[0].must_be_instance_of(Sale) + new_product.sales[1].must_be_instance_of(Sale) + end + + it "should have the correct sales in the array" do + new_product.sales[0].amount.must_equal(2510) + new_product.sales[1].amount.must_equal(9341) + end + end + + describe "#number_of_sales" do + let(:new_product) { Product.new({id: 21, name: "greens", vendor_id: 4}) } + + it "should return a fixnum" do + #puts new_product.number_of_sales + new_product.number_of_sales.must_be_kind_of(Fixnum) + end + + it "should return the correct number of sales" do + new_product.number_of_sales.must_equal(2) + end + end + + describe "self.by_vendor(vendor_id)" do + let(:example_vendor) { Vendor.new({id: 5, name: "Allison", num_employees: 75, market_id: 6}) } + + it "should return an array" do + FarMar::Product.by_vendor(example_vendor.id).must_be_kind_of(Array) + end + + it "should return an object: Product at any index of the array" do + puts example_vendor.products + puts "SPACE" + puts example_vendor.products[1] + + FarMar::Product.by_vendor(example_vendor.id)[0].must_be_instance_of(Product) + FarMar::Product.by_vendor(example_vendor.id)[2].must_be_instance_of(Product) + end + + it "each object returned should be associated with the correct vendor_id by checking the name" do #IS THIS A BETTER TEST? + FarMar::Product.by_vendor(example_vendor.id)[0].name.must_equal("Shaky Honey") + FarMar::Product.by_vendor(example_vendor.id)[2].name.must_equal("Black Apples") + end + end end end From 65feaf289aae0afd776b431f29f11d0c7aef76e1 Mon Sep 17 00:00:00 2001 From: Allison Hoke Date: Thu, 8 Sep 2016 12:21:44 -0700 Subject: [PATCH 25/42] adjusted tests for cleaned up methods --- specs/vendor_spec.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/specs/vendor_spec.rb b/specs/vendor_spec.rb index 16c00a10..4ecd23bc 100644 --- a/specs/vendor_spec.rb +++ b/specs/vendor_spec.rb @@ -56,12 +56,12 @@ module FarMar end it "should return an object: Product at any index of the array" do - #puts example_vendor.products - #puts example_vendor.products[0] + # puts example_vendor.products + # puts "SPACE" + # puts example_vendor.products[1] example_vendor.products[0].must_be_instance_of(Product) example_vendor.products[2].must_be_instance_of(Product) - #REVIEW should I do better at calling an index - for example, should I say .products[rand(0..length-1??)] end it "each object should have the correct vendor id" do #IS THIS A BETTER TEST? From ae0da7b08209c635281c9a2f54c1908408c203e4 Mon Sep 17 00:00:00 2001 From: Allison Hoke Date: Thu, 8 Sep 2016 12:29:50 -0700 Subject: [PATCH 26/42] created vendor method --- lib/sale.rb | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/sale.rb b/lib/sale.rb index fdda0bf0..0008bb59 100644 --- a/lib/sale.rb +++ b/lib/sale.rb @@ -1,6 +1,6 @@ module FarMar class Sale - attr_reader :id, :amount, :vendor_id + attr_reader :id, :amount, :vendor_id, :product_id def initialize(sale_hash) @id = sale_hash[:id] @@ -15,7 +15,7 @@ def self.all # IDEA make this an instance variable so you only have to make it run once sales = [] #array to store all of the hashes with sale info CSV.read("../FarMar/support/sales.csv").each do |line| - sale = {id: line[0].to_i, amount: line[1].to_i, purchase_time: line[2], vendor_id: line[3].to_i, product_id: line[4].to_i } #create a new hash for each sale to store specific info + sale = {id: line[0].to_i, amount: line[1].to_i, purchase_time: Time.parse(line[2]), vendor_id: line[3].to_i, product_id: line[4].to_i } #create a new hash for each sale to store specific info sales << self.new(sale) #creates a new instance with the hash info and puts it in the array to be returned end @@ -30,5 +30,8 @@ def self.find(id) end end + def vendor #returns the Vendor instance that is associated with this sale + FarMar::Vendor.find(@vendor_id) + end end end From 2631802b9c6e7ecdbccdc26801a9a200bea5f072 Mon Sep 17 00:00:00 2001 From: Allison Hoke Date: Thu, 8 Sep 2016 12:30:09 -0700 Subject: [PATCH 27/42] created tests for vendor method. all tests pass --- specs/sale_spec.rb | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/specs/sale_spec.rb b/specs/sale_spec.rb index 038e8229..f9be2c1f 100644 --- a/specs/sale_spec.rb +++ b/specs/sale_spec.rb @@ -34,5 +34,16 @@ module FarMar end end + describe "#vendor" do + let(:new_sale) { Sale.new({vendor_id: 5}) } + + it "should return an object:Vendor" do + new_sale.vendor.must_be_instance_of(Vendor) + end + + it "should return the correct vendor" do + new_sale.vendor.name.must_equal("Reynolds, Schmitt and Klocko") + end + end end end From 2726649d1223185b6d086b4fb85638c12f31c426 Mon Sep 17 00:00:00 2001 From: Allison Hoke Date: Thu, 8 Sep 2016 14:13:10 -0700 Subject: [PATCH 28/42] added product method and self.between method --- lib/sale.rb | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/lib/sale.rb b/lib/sale.rb index 0008bb59..c17ffbe0 100644 --- a/lib/sale.rb +++ b/lib/sale.rb @@ -1,6 +1,6 @@ module FarMar class Sale - attr_reader :id, :amount, :vendor_id, :product_id + attr_reader :id, :amount, :vendor_id, :product_id, :purchase_time def initialize(sale_hash) @id = sale_hash[:id] @@ -15,7 +15,7 @@ def self.all # IDEA make this an instance variable so you only have to make it run once sales = [] #array to store all of the hashes with sale info CSV.read("../FarMar/support/sales.csv").each do |line| - sale = {id: line[0].to_i, amount: line[1].to_i, purchase_time: Time.parse(line[2]), vendor_id: line[3].to_i, product_id: line[4].to_i } #create a new hash for each sale to store specific info + sale = {id: line[0].to_i, amount: line[1].to_i, purchase_time: DateTime.parse(line[2]), vendor_id: line[3].to_i, product_id: line[4].to_i } #create a new hash for each sale to store specific info sales << self.new(sale) #creates a new instance with the hash info and puts it in the array to be returned end @@ -33,5 +33,20 @@ def self.find(id) def vendor #returns the Vendor instance that is associated with this sale FarMar::Vendor.find(@vendor_id) end + + def product + FarMar::Product.find(@product_id) + end + + def self.between(beginning_time, end_time) + sales_between = [] + + self.all.each do |s| + if s.purchase_time >= beginning_time && s.purchase_time <= end_time + sales_between << s + end + end + return sales_between + end end end From 10ddc90f354164b10ff2b024296fa974d626bf14 Mon Sep 17 00:00:00 2001 From: Allison Hoke Date: Thu, 8 Sep 2016 14:13:43 -0700 Subject: [PATCH 29/42] created tests for product method and self.between method. all tests pass --- specs/sale_spec.rb | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/specs/sale_spec.rb b/specs/sale_spec.rb index f9be2c1f..9e96c1d4 100644 --- a/specs/sale_spec.rb +++ b/specs/sale_spec.rb @@ -15,6 +15,7 @@ module FarMar let(:all_sales) { Sale.all } it "should return an array" do + #puts FarMar::Sale.all.length all_sales.must_be_kind_of(Array) end @@ -45,5 +46,39 @@ module FarMar new_sale.vendor.name.must_equal("Reynolds, Schmitt and Klocko") end end + + describe "#product" do + let(:new_sale) { Sale.new({product_id: 5}) } + it "should return an object:Product" do + new_sale.product.must_be_instance_of(Product) + end + + it "should return the correct Product" do + new_sale.product.name.must_equal("Green Apples") + end + end + + describe "self.between(beginning_time, end_time)" do + let(:sales_between) { Sale.between(DateTime.parse("2013-11-10 05:19:05 -0800"), DateTime.parse("2013-11-13 01:48:15 -0800")) } + + it "should return an array" do + sales_between.must_be_kind_of(Array) + end + + it "should have an object:Sale at any index of the array" do + #puts sales_between.length + sales_between[0].must_be_instance_of(Sale) + sales_between[10].must_be_instance_of(Sale) + sales_between[50].must_be_instance_of(Sale) + sales_between[100].must_be_instance_of(Sale) + end + + it "the time of the sale at any index should be between the arguments(beg_time, end_time)" do + sales_between[0].purchase_time.must_be_close_to(DateTime.parse("2013-11-10 05:19:05 -0800"), DateTime.parse("2013-11-13 01:48:15 -0800")) + sales_between[10].purchase_time.must_be_close_to(DateTime.parse("2013-11-10 05:19:05 -0800"), DateTime.parse("2013-11-13 01:48:15 -0800")) + sales_between[50].purchase_time.must_be_close_to(DateTime.parse("2013-11-10 05:19:05 -0800"), DateTime.parse("2013-11-13 01:48:15 -0800")) + sales_between[100].purchase_time.must_be_close_to(DateTime.parse("2013-11-10 05:19:05 -0800"), DateTime.parse("2013-11-13 01:48:15 -0800")) + end + end end end From 40445d6b08949c25f2302c5b844bcc3c9c6bfa05 Mon Sep 17 00:00:00 2001 From: Allison Hoke Date: Thu, 8 Sep 2016 14:46:51 -0700 Subject: [PATCH 30/42] used correct mthod calls on third test for self.between --- specs/sale_spec.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/specs/sale_spec.rb b/specs/sale_spec.rb index 9e96c1d4..8a35c972 100644 --- a/specs/sale_spec.rb +++ b/specs/sale_spec.rb @@ -74,10 +74,10 @@ module FarMar end it "the time of the sale at any index should be between the arguments(beg_time, end_time)" do - sales_between[0].purchase_time.must_be_close_to(DateTime.parse("2013-11-10 05:19:05 -0800"), DateTime.parse("2013-11-13 01:48:15 -0800")) - sales_between[10].purchase_time.must_be_close_to(DateTime.parse("2013-11-10 05:19:05 -0800"), DateTime.parse("2013-11-13 01:48:15 -0800")) - sales_between[50].purchase_time.must_be_close_to(DateTime.parse("2013-11-10 05:19:05 -0800"), DateTime.parse("2013-11-13 01:48:15 -0800")) - sales_between[100].purchase_time.must_be_close_to(DateTime.parse("2013-11-10 05:19:05 -0800"), DateTime.parse("2013-11-13 01:48:15 -0800")) + (sales_between[0].purchase_time.between?(DateTime.parse("2013-11-10 05:19:05 -0800"), DateTime.parse("2013-11-13 01:48:15 -0800"))).must_equal(true) + (sales_between[10].purchase_time.between?(DateTime.parse("2013-11-10 05:19:05 -0800"), DateTime.parse("2013-11-13 01:48:15 -0800"))).must_equal(true) + (sales_between[50].purchase_time.between?(DateTime.parse("2013-11-10 05:19:05 -0800"), DateTime.parse("2013-11-13 01:48:15 -0800"))).must_equal(true) + (sales_between[100].purchase_time.between?(DateTime.parse("2013-11-10 05:19:05 -0800"), DateTime.parse("2013-11-13 01:48:15 -0800"))).must_equal(true) end end end From 7a61d56e5625c3a46be7a28fcd4d751aee8685d2 Mon Sep 17 00:00:00 2001 From: Allison Hoke Date: Thu, 8 Sep 2016 14:56:33 -0700 Subject: [PATCH 31/42] changed loop to enumerable in sales method --- lib/vendor.rb | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/lib/vendor.rb b/lib/vendor.rb index 33906496..9576d7f9 100644 --- a/lib/vendor.rb +++ b/lib/vendor.rb @@ -36,15 +36,7 @@ def products #returns an array of object:Products that belong to this Vendor end def sales #returns a collection of object:Sales that are associated with this vendor - ven_id = @id - vendor_sales = [] - - FarMar::Sale.all.each do |s| - if s.vendor_id == ven_id - vendor_sales << s - end - end - return vendor_sales + FarMar::Sale.all.select { |s| s.vendor_id == @id } end def revenue #returns the sum of all the vendor's sales From e637c3f0eeff873e1cafdff591bee8ce2c89bb7b Mon Sep 17 00:00:00 2001 From: Allison Hoke Date: Thu, 8 Sep 2016 15:03:35 -0700 Subject: [PATCH 32/42] changed loop to enumerable in self.by_market method --- lib/vendor.rb | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/lib/vendor.rb b/lib/vendor.rb index 9576d7f9..abf29728 100644 --- a/lib/vendor.rb +++ b/lib/vendor.rb @@ -40,24 +40,15 @@ def sales #returns a collection of object:Sales that are associated with this ve end def revenue #returns the sum of all the vendor's sales - all_sales = self.sales total = 0 - - all_sales.each do |s| + self.sales.each do |s| total += s.amount end return total end def self.by_market(market_id) #returns an array of the vendors with the given market_id - market_vendors = [] - #use .select - self.all.each do |v| - if v.market_id == market_id #finds vendors whose ids match argument - market_vendors << v #pushes them to array of all vendors at that market - end - end - return market_vendors + self.all.select { |v| v.market_id == market_id } end end end From 9f069d7e9fb13ce602b9bc30598c06a3e716a1ce Mon Sep 17 00:00:00 2001 From: Allison Hoke Date: Thu, 8 Sep 2016 15:06:33 -0700 Subject: [PATCH 33/42] changed loop to enumerable in sales method. --- lib/product.rb | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/lib/product.rb b/lib/product.rb index d9797f52..679499f9 100644 --- a/lib/product.rb +++ b/lib/product.rb @@ -31,15 +31,7 @@ def vendor #returns the object:Vendor instance that is associated with Product end def sales #returns an array of Sale objects that are accociated with this product - all_sales = FarMar::Sale.all - product_sales = [] - - all_sales.each do |s| - if s.product_id == @id - product_sales << s - end - end - return product_sales + FarMar::Sale.all.select { |s| s.product_id == @id } end def number_of_sales #returns the number of times this product has been sold From 8360f59cff6eb7dda885b8f4d8b5fae20b0c7c47 Mon Sep 17 00:00:00 2001 From: Allison Hoke Date: Thu, 8 Sep 2016 15:08:11 -0700 Subject: [PATCH 34/42] changed loop to enumerable in self.by_vendor method --- lib/product.rb | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/lib/product.rb b/lib/product.rb index 679499f9..c1e949ce 100644 --- a/lib/product.rb +++ b/lib/product.rb @@ -39,14 +39,7 @@ def number_of_sales #returns the number of times this product has been sold end def self.by_vendor(vendor_id) #returns all the products associated with the given vendor_id - vendor_products = [] - - FarMar::Product.all.each do |pro| - if pro.vendor_id == vendor_id - vendor_products << pro - end - end - return vendor_products + FarMar::Product.all.select { |pro| pro.vendor_id == vendor_id } end end end From da9fa1cd53c00b22049e8eba0dee3108dbdd3a9b Mon Sep 17 00:00:00 2001 From: Allison Hoke Date: Thu, 8 Sep 2016 15:11:52 -0700 Subject: [PATCH 35/42] changed loop to enumerable in self.between method - is this a good idea? old code still there but commented --- lib/sale.rb | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/sale.rb b/lib/sale.rb index c17ffbe0..40e9f16f 100644 --- a/lib/sale.rb +++ b/lib/sale.rb @@ -10,7 +10,6 @@ def initialize(sale_hash) @product_id = sale_hash[:product_id] end -# TO DO - figure out how to convert purchase_time to datetime data type def self.all # IDEA make this an instance variable so you only have to make it run once sales = [] #array to store all of the hashes with sale info @@ -39,14 +38,15 @@ def product end def self.between(beginning_time, end_time) - sales_between = [] - - self.all.each do |s| - if s.purchase_time >= beginning_time && s.purchase_time <= end_time - sales_between << s - end - end - return sales_between + self.all.select { |s| beginning_time <= s.purchase_time && s.purchase_time <= end_time } + # sales_between = [] + # + # self.all.each do |s| + # if s.purchase_time >= beginning_time && s.purchase_time <= end_time + # sales_between << s + # end + # end + # return sales_between end end end From ddf18cb36665d85e113eb3389bafa44f78b2c638 Mon Sep 17 00:00:00 2001 From: Allison Hoke Date: Thu, 8 Sep 2016 15:53:16 -0700 Subject: [PATCH 36/42] adjusted test in vendors method to use before instead of let (this is unnecessary because the variable that is assigned is never mutated, but i wanted to try before --- specs/market_spec.rb | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/specs/market_spec.rb b/specs/market_spec.rb index 8486b88b..1db743bf 100644 --- a/specs/market_spec.rb +++ b/specs/market_spec.rb @@ -33,26 +33,25 @@ module FarMar end describe "#vendors" do - let(:example_market) { Market.new({id: 4, name: "Allison's Market", address: "address", city: "city", county: "county", state: "state", zip: "zip"}) } + before(:each) do #unnecessary because the tests dont change the values of this variable, but I wanted to try it out + @example_market = Market.new({id: 4, name: "Allison's Market", address: "address", city: "city", county: "county", state: "state", zip: "zip"}) + @four = @example_market.vendors + end it "returns an array" do - example_market.vendors.must_be_kind_of(Array) + @example_market.vendors.must_be_kind_of(Array) end it "should return an object: Vendor at any index of the array" do - four = example_market.vendors - - four[0].must_be_instance_of(Vendor) - four[1].must_be_instance_of(Vendor) - four[2].must_be_instance_of(Vendor) + @four[0].must_be_instance_of(Vendor) + @four[1].must_be_instance_of(Vendor) + @four[2].must_be_instance_of(Vendor) end it "should return the correct market_id that matches the one searched" do - four = example_market.vendors - - four[0].market_id.must_equal(4) - four[1].market_id.must_equal(4) - four[2].market_id.must_equal(4) + @four[0].market_id.must_equal(4) + @four[1].market_id.must_equal(4) + @four[2].market_id.must_equal(4) end end end From 86ff37af3752a9847b75301c257713e8eae6b483 Mon Sep 17 00:00:00 2001 From: Allison Hoke Date: Thu, 8 Sep 2016 15:54:45 -0700 Subject: [PATCH 37/42] clean up --- specs/vendor_spec.rb | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/specs/vendor_spec.rb b/specs/vendor_spec.rb index 4ecd23bc..636bae8a 100644 --- a/specs/vendor_spec.rb +++ b/specs/vendor_spec.rb @@ -12,16 +12,14 @@ module FarMar end describe ".all" do - let(:all_vendors) { Vendor.all } - it "should return an array" do - all_vendors.must_be_kind_of(Array) + Vendor.all.must_be_kind_of(Array) end it "should return an object: Vendor at any index of the array" do - all_vendors[0].must_be_instance_of(Vendor) - all_vendors[10].must_be_instance_of(Vendor) - all_vendors[20].must_be_instance_of(Vendor) + Vendor.all[0].must_be_instance_of(Vendor) + Vendor.all[10].must_be_instance_of(Vendor) + Vendor.all[20].must_be_instance_of(Vendor) end end From 69e45e8bd2d7982f9f73a3eb43da9bbca355692c Mon Sep 17 00:00:00 2001 From: Allison Hoke Date: Thu, 8 Sep 2016 15:55:33 -0700 Subject: [PATCH 38/42] confusion about what must be required --- far_mar.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/far_mar.rb b/far_mar.rb index cbaf6d9c..04dd7fab 100644 --- a/far_mar.rb +++ b/far_mar.rb @@ -1,5 +1,4 @@ require 'csv' -require 'time' require_relative './lib/market' require_relative './lib/product' require_relative './lib/sale' From 427575f8fb3c2728d0ad82f1305ead2f877fe64d Mon Sep 17 00:00:00 2001 From: Allison Hoke Date: Thu, 8 Sep 2016 16:25:36 -0700 Subject: [PATCH 39/42] minor clean up --- lib/market.rb | 7 +++++++ specs/market_spec.rb | 6 ++++++ specs/vendor_spec.rb | 18 ++++++++---------- 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/lib/market.rb b/lib/market.rb index b0670ebf..f9d26667 100644 --- a/lib/market.rb +++ b/lib/market.rb @@ -41,7 +41,14 @@ def self.find(id) end def vendors #returns a collection of FarMar::Vendor instances that are associated with the market + #vendors = FarMar::Vendor.by_market(@id) + + # if vendors == nil + # raise ArgumentError.new("There are no vendors associated with this market") + # else + # return vendors + # end end end diff --git a/specs/market_spec.rb b/specs/market_spec.rb index 1db743bf..9fb24f69 100644 --- a/specs/market_spec.rb +++ b/specs/market_spec.rb @@ -53,6 +53,12 @@ module FarMar @four[1].market_id.must_equal(4) @four[2].market_id.must_equal(4) end + + # it "should raise and ArgumentError if there is no vendor with that market id" do + # another_market = Market.new({id: 98765432}) + # + # another_market.vendors.must_raise(ArgumentError) + # end end end end diff --git a/specs/vendor_spec.rb b/specs/vendor_spec.rb index 636bae8a..e3f2727b 100644 --- a/specs/vendor_spec.rb +++ b/specs/vendor_spec.rb @@ -47,24 +47,22 @@ module FarMar end describe "#products" do - let(:example_vendor) { Vendor.new({id: 5, name: "Allison", num_employees: 75, market_id: 6}) } + before(:each) do + @example_vendor = Vendor.new({id: 5, name: "Allison", num_employees: 75, market_id: 6}) + end it "should return an array" do - example_vendor.products.must_be_kind_of(Array) + @example_vendor.products.must_be_kind_of(Array) end it "should return an object: Product at any index of the array" do - # puts example_vendor.products - # puts "SPACE" - # puts example_vendor.products[1] - - example_vendor.products[0].must_be_instance_of(Product) - example_vendor.products[2].must_be_instance_of(Product) + @example_vendor.products[0].must_be_instance_of(Product) + @example_vendor.products[2].must_be_instance_of(Product) end it "each object should have the correct vendor id" do #IS THIS A BETTER TEST? - example_vendor.products[0].vendor_id.must_equal(example_vendor.id) - example_vendor.products[2].vendor_id.must_equal(example_vendor.id) + @example_vendor.products[0].vendor_id.must_equal(@example_vendor.id) + @example_vendor.products[2].vendor_id.must_equal(@example_vendor.id) end end From 7606a7a0923123024095d5059898bea96eda5a25 Mon Sep 17 00:00:00 2001 From: Allison Hoke Date: Thu, 8 Sep 2016 16:55:07 -0700 Subject: [PATCH 40/42] WIP trying to create ArgErr. code in comments needs to be figure out --- lib/market.rb | 7 ------- lib/vendor.rb | 7 +++++++ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/market.rb b/lib/market.rb index f9d26667..b0670ebf 100644 --- a/lib/market.rb +++ b/lib/market.rb @@ -41,14 +41,7 @@ def self.find(id) end def vendors #returns a collection of FarMar::Vendor instances that are associated with the market - #vendors = FarMar::Vendor.by_market(@id) - - # if vendors == nil - # raise ArgumentError.new("There are no vendors associated with this market") - # else - # return vendors - # end end end diff --git a/lib/vendor.rb b/lib/vendor.rb index abf29728..a5ab7a8f 100644 --- a/lib/vendor.rb +++ b/lib/vendor.rb @@ -48,7 +48,14 @@ def revenue #returns the sum of all the vendor's sales end def self.by_market(market_id) #returns an array of the vendors with the given market_id + #vendors_by_market = self.all.select { |v| v.market_id == market_id } + + # if vendors_by_market.length != 4 + # raise ArgumentError.new("There are no vendors associated with this market") + # else + # return vendors_by_market + # end end end end From 3e1ed98caef0f8c92b1601d2bb28f3f19ee1521a Mon Sep 17 00:00:00 2001 From: Allison Hoke Date: Fri, 9 Sep 2016 14:05:31 -0700 Subject: [PATCH 41/42] added argument errors on methods that take in arguments. --- lib/market.rb | 24 +++++++++++++++++++----- lib/product.rb | 23 ++++++++++++++++++----- lib/sale.rb | 25 +++++++++++++------------ lib/vendor.rb | 38 +++++++++++++++++++++++++++----------- specs/market_spec.rb | 27 ++++++++++++++++++++++----- specs/product_spec.rb | 12 ++++++++---- specs/sale_spec.rb | 22 ++++++++++++++++++++++ specs/vendor_spec.rb | 27 +++++++++++++++++++++++++++ 8 files changed, 156 insertions(+), 42 deletions(-) diff --git a/lib/market.rb b/lib/market.rb index b0670ebf..1450e991 100644 --- a/lib/market.rb +++ b/lib/market.rb @@ -33,16 +33,30 @@ def self.all end def self.find(id) - self.all.each do |m| - if m.id == id - return m #returns the object whose id matches the argument - end + if FarMar::Market.ids.include?(id) + self.all.find { |m| m.id == id } + else + raise ArgumentError.new("There are no vendors with that id") end + # OLD CODE WITH .each LOOP + # self.all.each do |m| + # if m.id == id + # return m #returns the object whose id matches the argument + # end + # end end def vendors #returns a collection of FarMar::Vendor instances that are associated with the market - FarMar::Vendor.by_market(@id) + FarMar::Vendor.by_market(@id) #has clause for ArgError in method end + def self.ids + market_ids = [] + + self.all.each do |m| + market_ids << m.id + end + return market_ids + end end end diff --git a/lib/product.rb b/lib/product.rb index c1e949ce..7f605786 100644 --- a/lib/product.rb +++ b/lib/product.rb @@ -19,10 +19,10 @@ def self.all end def self.find(id) - self.all.each do |pro| - if pro.id == id - return pro #returns the object whose id matches the argument - end + if FarMar::Product.ids.include?(id) + self.all.find { |pro| pro.id == id } + else + raise ArgumentError.new("There are no products with that id") end end @@ -39,7 +39,20 @@ def number_of_sales #returns the number of times this product has been sold end def self.by_vendor(vendor_id) #returns all the products associated with the given vendor_id - FarMar::Product.all.select { |pro| pro.vendor_id == vendor_id } + if FarMar::Vendor.ids.include?(vendor_id) + self.all.select { |pro| pro.vendor_id == vendor_id } + else + raise ArgumentError.new("There are no vendors with that id") + end + end + + def self.ids + product_ids = [] + + self.all.each do |pro| + product_ids << pro.id + end + return product_ids end end end diff --git a/lib/sale.rb b/lib/sale.rb index 40e9f16f..6b8e1267 100644 --- a/lib/sale.rb +++ b/lib/sale.rb @@ -22,10 +22,10 @@ def self.all end def self.find(id) - self.all.each do |s| - if s.id == id - return s #returns the object whose id matches the argument - end + if FarMar::Sale.ids.include?(id) + self.all.find { |s| s.id == id } + else + raise ArgumentError.new("There are no sales with that id") end end @@ -39,14 +39,15 @@ def product def self.between(beginning_time, end_time) self.all.select { |s| beginning_time <= s.purchase_time && s.purchase_time <= end_time } - # sales_between = [] - # - # self.all.each do |s| - # if s.purchase_time >= beginning_time && s.purchase_time <= end_time - # sales_between << s - # end - # end - # return sales_between + end + + def self.ids + sale_ids = [] + + self.all.each do |s| + sale_ids << s.id + end + return sale_ids end end end diff --git a/lib/vendor.rb b/lib/vendor.rb index a5ab7a8f..b40e3921 100644 --- a/lib/vendor.rb +++ b/lib/vendor.rb @@ -20,11 +20,18 @@ def self.all #returns an array of objects:Vendors end def self.find(id) #returns the object:Vendor with arg. id - self.all.each do |v| - if v.id == id - return v #returns the object whose id matches the argument - end + if FarMar::Vendor.ids.include?(id) + self.all.find { |v| v.id == id } + else + raise ArgumentError.new("There are no vendors with that id") end + + #OLD CODE WITH .each LOOP + # self.all.each do |v| + # if v.id == id + # return v #returns the object whose id matches the argument + # end + # end end def market #returns the object:Market that this vendor belongs to @@ -48,14 +55,23 @@ def revenue #returns the sum of all the vendor's sales end def self.by_market(market_id) #returns an array of the vendors with the given market_id - #vendors_by_market = - self.all.select { |v| v.market_id == market_id } + # self.all.select { |v| v.market_id == market_id } - # if vendors_by_market.length != 4 - # raise ArgumentError.new("There are no vendors associated with this market") - # else - # return vendors_by_market - # end + if FarMar::Market.ids.include?(market_id) + self.all.select { |v| v.market_id == market_id } + else + raise ArgumentError.new("There are no vendors associated with this market") + end end + + def self.ids + vendor_ids = [] + + self.all.each do |v| + vendor_ids << v.id + end + return vendor_ids + end + end end diff --git a/specs/market_spec.rb b/specs/market_spec.rb index 9fb24f69..71c9dd7a 100644 --- a/specs/market_spec.rb +++ b/specs/market_spec.rb @@ -30,12 +30,17 @@ module FarMar scots.must_be_instance_of(Market) scots.name.must_equal("Scottdale Farmers Market") end + + it "should raise argument error when given an invalid id" do + proc { Market.find(987654321) }.must_raise(ArgumentError) + end end describe "#vendors" do before(:each) do #unnecessary because the tests dont change the values of this variable, but I wanted to try it out @example_market = Market.new({id: 4, name: "Allison's Market", address: "address", city: "city", county: "county", state: "state", zip: "zip"}) @four = @example_market.vendors + @another_market = Market.new({id: 987654321987654321, name: "Dustin's Market"}) end it "returns an array" do @@ -53,12 +58,24 @@ module FarMar @four[1].market_id.must_equal(4) @four[2].market_id.must_equal(4) end + end - # it "should raise and ArgumentError if there is no vendor with that market id" do - # another_market = Market.new({id: 98765432}) - # - # another_market.vendors.must_raise(ArgumentError) - # end + describe "self.ids" do + it "should return an array" do + Market.ids.must_be_kind_of(Array) + end + + it "should have a fixnum at any index of the array" do + Market.ids[0].must_be_kind_of(Fixnum) + Market.ids[10].must_be_kind_of(Fixnum) + Market.ids[50].must_be_kind_of(Fixnum) + end + + it "should have id numbers at the correct index of the array" do + Market.ids[0].must_equal(Market.all[0].id) + Market.ids[10].must_equal(Market.all[10].id) + Market.ids[50].must_equal(Market.all[50].id) + end end end end diff --git a/specs/product_spec.rb b/specs/product_spec.rb index 94a89307..7c1cca89 100644 --- a/specs/product_spec.rb +++ b/specs/product_spec.rb @@ -32,6 +32,10 @@ module FarMar tp.must_be_instance_of(Product) tp.name.must_equal("Tall Pretzel") end + + it "should raise argument error when given an invalid id" do + proc { Product.find(987654321) }.must_raise(ArgumentError) + end end describe "#vendor" do @@ -86,10 +90,6 @@ module FarMar end it "should return an object: Product at any index of the array" do - puts example_vendor.products - puts "SPACE" - puts example_vendor.products[1] - FarMar::Product.by_vendor(example_vendor.id)[0].must_be_instance_of(Product) FarMar::Product.by_vendor(example_vendor.id)[2].must_be_instance_of(Product) end @@ -98,6 +98,10 @@ module FarMar FarMar::Product.by_vendor(example_vendor.id)[0].name.must_equal("Shaky Honey") FarMar::Product.by_vendor(example_vendor.id)[2].name.must_equal("Black Apples") end + + it "should raise an argument error if invalid vendor id is supplied" do + proc { FarMar::Product.by_vendor(98765432) }.must_raise(ArgumentError) + end end end end diff --git a/specs/sale_spec.rb b/specs/sale_spec.rb index 8a35c972..d5652130 100644 --- a/specs/sale_spec.rb +++ b/specs/sale_spec.rb @@ -33,6 +33,10 @@ module FarMar tp.must_be_instance_of(Sale) tp.amount.must_equal(51) end + + it "should raise argument error when given an invalid id" do + proc { Sale.find(987654321) }.must_raise(ArgumentError) + end end describe "#vendor" do @@ -80,5 +84,23 @@ module FarMar (sales_between[100].purchase_time.between?(DateTime.parse("2013-11-10 05:19:05 -0800"), DateTime.parse("2013-11-13 01:48:15 -0800"))).must_equal(true) end end + + describe "self.ids" do + it "should return an array" do + Sale.ids.must_be_kind_of(Array) + end + + it "should have a fixnum at any index of the array" do + Sale.ids[0].must_be_kind_of(Fixnum) + Sale.ids[10].must_be_kind_of(Fixnum) + Sale.ids[50].must_be_kind_of(Fixnum) + end + + it "should have id numbers at the correct index of the array" do + Sale.ids[0].must_equal(Sale.all[0].id) + Sale.ids[10].must_equal(Sale.all[10].id) + Sale.ids[50].must_equal(Sale.all[50].id) + end + end end end diff --git a/specs/vendor_spec.rb b/specs/vendor_spec.rb index e3f2727b..7f9cdd72 100644 --- a/specs/vendor_spec.rb +++ b/specs/vendor_spec.rb @@ -30,6 +30,10 @@ module FarMar led.must_be_instance_of(Vendor) led.name.must_equal("Ledner Group") end + + it "should raise argument error when given an invalid id" do + proc { Vendor.find(987654321) }.must_raise(ArgumentError) + end end describe "#market" do @@ -99,6 +103,7 @@ module FarMar describe "self.by_market(market_id)" do let(:vendors_by_market) { Vendor.by_market(4) } + let(:example_market) { Market.new({id: 987654321987654321})} it "should return an array" do vendors_by_market.must_be_kind_of(Array) @@ -114,6 +119,28 @@ module FarMar vendors_by_market[2].id.must_equal(15) vendors_by_market[3].id.must_equal(16) end + + it "should raise and ArgumentError if there is no vendor with that market id" do + proc{ Vendor.by_market(example_market.id) }.must_raise(ArgumentError) + end + end + + describe "self.ids" do + it "should return an array" do + Vendor.ids.must_be_kind_of(Array) + end + + it "should have a fixnum at any index of the array" do + Vendor.ids[0].must_be_kind_of(Fixnum) + Vendor.ids[10].must_be_kind_of(Fixnum) + Vendor.ids[50].must_be_kind_of(Fixnum) + end + + it "should have id numbers at the correct index of the array" do + Vendor.ids[0].must_equal(Vendor.all[0].id) + Vendor.ids[10].must_equal(Vendor.all[10].id) + Vendor.ids[50].must_equal(Vendor.all[50].id) + end end end end From f7004d6b5e814dd28b5a5ceb26aefc6871e77ea5 Mon Sep 17 00:00:00 2001 From: Allison Hoke Date: Fri, 9 Sep 2016 15:48:14 -0700 Subject: [PATCH 42/42] Commits got bad at the end. Changed .all methods in all classes to use class variables. Added the first optional enhancement #products in market class. --- .gitignore | 1 + far_mar.rb | 6 ++++++ lib/market.rb | 31 ++++++++++++++++++++++++------- lib/product.rb | 7 ++++++- lib/sale.rb | 7 ++++++- lib/vendor.rb | 7 ++++++- specs/market_spec.rb | 34 +++++++++++++++++++++++++++++++++- specs/product_spec.rb | 14 ++++++++++++++ specs/sale_spec.rb | 15 +++++++++++++++ specs/vendor_spec.rb | 14 +++++++++++++- 10 files changed, 124 insertions(+), 12 deletions(-) diff --git a/.gitignore b/.gitignore index 7053dc17..19c66501 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /coverage/ +.DS_Store diff --git a/far_mar.rb b/far_mar.rb index 04dd7fab..cab74027 100644 --- a/far_mar.rb +++ b/far_mar.rb @@ -6,3 +6,9 @@ module FarMar end + +a = FarMar::Market.all.sample +puts a.vendors.length +puts a.products +puts a.products[0].vendor_id +puts a.products[2].vendor_id diff --git a/lib/market.rb b/lib/market.rb index 1450e991..634b69e6 100644 --- a/lib/market.rb +++ b/lib/market.rb @@ -14,7 +14,7 @@ def initialize(market_hash) end - def self.all + def self.read markets = [] #array to store all of the hashes with market info CSV.read("../FarMar/support/markets.csv").each do |line| market = { @@ -32,18 +32,17 @@ def self.all markets #returns this array end + def self.all #what happens if the csv file is updated - this variable will not include new data + @@all_markets ||= self.read + return @@all_markets + end + def self.find(id) if FarMar::Market.ids.include?(id) self.all.find { |m| m.id == id } else raise ArgumentError.new("There are no vendors with that id") end - # OLD CODE WITH .each LOOP - # self.all.each do |m| - # if m.id == id - # return m #returns the object whose id matches the argument - # end - # end end def vendors #returns a collection of FarMar::Vendor instances that are associated with the market @@ -58,5 +57,23 @@ def self.ids end return market_ids end + + def products #returns an array of Products that are associated to the market via the Vendor class + all_products = [] + + this_markets_vendors = FarMar::Vendor.by_market(@id) #returns array of Vendors + + this_markets_vendors.each do |v| + vendor_products = FarMar::Product.by_vendor(v.id) #returns array of Products + vendor_products.each do |pro| + all_products << pro + end + end + return all_products + end + + #find all vendors associated with this market + #find all products associated with those vendors + #end end end diff --git a/lib/product.rb b/lib/product.rb index 7f605786..05e4f872 100644 --- a/lib/product.rb +++ b/lib/product.rb @@ -8,7 +8,7 @@ def initialize(product_hash) @vendor_id = product_hash[:vendor_id] end - def self.all + def self.read products = [] #array to store all of the hashes with product info CSV.read("../FarMar/support/products.csv").each do |line| product = {id: line[0].to_i, name: line[1], vendor_id: line[2].to_i} #create a new hash for each product to store specific info @@ -18,6 +18,11 @@ def self.all products #returns this array end + def self.all #what happens if the csv file is updated - this variable will not include new data + @@all_products ||= self.read + return @@all_products + end + def self.find(id) if FarMar::Product.ids.include?(id) self.all.find { |pro| pro.id == id } diff --git a/lib/sale.rb b/lib/sale.rb index 6b8e1267..248f5693 100644 --- a/lib/sale.rb +++ b/lib/sale.rb @@ -10,7 +10,7 @@ def initialize(sale_hash) @product_id = sale_hash[:product_id] end - def self.all + def self.read # IDEA make this an instance variable so you only have to make it run once sales = [] #array to store all of the hashes with sale info CSV.read("../FarMar/support/sales.csv").each do |line| @@ -21,6 +21,11 @@ def self.all sales #returns this array end + def self.all #what happens if the csv file is updated - this variable will not include new data + @@all_sales ||= self.read + return @@all_sales + end + def self.find(id) if FarMar::Sale.ids.include?(id) self.all.find { |s| s.id == id } diff --git a/lib/vendor.rb b/lib/vendor.rb index b40e3921..0f5887c5 100644 --- a/lib/vendor.rb +++ b/lib/vendor.rb @@ -9,7 +9,7 @@ def initialize(vendor_hash) @market_id = vendor_hash[:market_id] end - def self.all #returns an array of objects:Vendors + def self.read #returns an array of objects:Vendors vendors = [] #array to store all of the hashes with vendor info CSV.read("../FarMar/support/vendors.csv").each do |line| vendor = {id: line[0].to_i, name: line[1], num_employees: line[2].to_i, market_id: line[3].to_i} #create a new hash for each vendor to store specific info @@ -19,6 +19,11 @@ def self.all #returns an array of objects:Vendors vendors #returns this array end + def self.all #what happens if the csv file is updated - this variable will not include new data + @@all_vendors ||= self.read + return @@all_vendors + end + def self.find(id) #returns the object:Vendor with arg. id if FarMar::Vendor.ids.include?(id) self.all.find { |v| v.id == id } diff --git a/specs/market_spec.rb b/specs/market_spec.rb index 71c9dd7a..5f561626 100644 --- a/specs/market_spec.rb +++ b/specs/market_spec.rb @@ -11,7 +11,19 @@ module FarMar end end - describe ".all" do + describe ".read" do + it "should return an array" do + Market.read.must_be_kind_of(Array) + end + + it "should return an object: Market at any index of the array" do + Market.read[0].must_be_instance_of(Market) + Market.read[10].must_be_instance_of(Market) + Market.read[20].must_be_instance_of(Market) + end + end + + describe "self.all" do it "should return an array" do Market.all.must_be_kind_of(Array) end @@ -77,5 +89,25 @@ module FarMar Market.ids[50].must_equal(Market.all[50].id) end end + + describe "#products" do + before(:each) do #unnecessary because the tests dont change the values of this variable, but I wanted to try it out + @example_market = Market.new({id: 4, name: "Allison's Market", address: "address", city: "city", county: "county", state: "state", zip: "zip"}) + end + + it "should return an array" do + @example_market.products.must_be_kind_of(Array) + end + + it "should have an object:Product at any index of the array" do + @example_market.products[0].must_be_instance_of(Product) + @example_market.products[2].must_be_instance_of(Product) + end + + it "all products should have the correct market id" do + @example_market.products[0].vendor_id.between?(13, 16).must_equal(true) + @example_market.products[3].vendor_id.between?(13, 16).must_equal(true) + end + end end end diff --git a/specs/product_spec.rb b/specs/product_spec.rb index 7c1cca89..1d7d1f02 100644 --- a/specs/product_spec.rb +++ b/specs/product_spec.rb @@ -11,6 +11,20 @@ module FarMar end end + describe ".read" do + let(:read_products) { Product.read } + + it "should return an array" do + read_products.must_be_kind_of(Array) + end + + it "should return an object: Product at any index of the array" do + read_products[0].must_be_instance_of(Product) + read_products[10].must_be_instance_of(Product) + read_products[20].must_be_instance_of(Product) + end + end + describe ".all" do let(:all_products) { Product.all } diff --git a/specs/sale_spec.rb b/specs/sale_spec.rb index d5652130..1e003358 100644 --- a/specs/sale_spec.rb +++ b/specs/sale_spec.rb @@ -11,6 +11,21 @@ module FarMar end end + describe ".read" do + let(:read_sales) { Sale.read } + + it "should return an array" do + #puts FarMar::Sale.read.length + read_sales.must_be_kind_of(Array) + end + + it "should return an object: Sale at any index of the array" do + read_sales[0].must_be_instance_of(Sale) + read_sales[10].must_be_instance_of(Sale) + read_sales[20].must_be_instance_of(Sale) + end + end + describe ".all" do let(:all_sales) { Sale.all } diff --git a/specs/vendor_spec.rb b/specs/vendor_spec.rb index 7f9cdd72..6f910711 100644 --- a/specs/vendor_spec.rb +++ b/specs/vendor_spec.rb @@ -11,7 +11,19 @@ module FarMar end end - describe ".all" do + describe ".read" do + it "should return an array" do + Vendor.read.must_be_kind_of(Array) + end + + it "should return an object: Vendor at any index of the array" do + Vendor.read[0].must_be_instance_of(Vendor) + Vendor.read[10].must_be_instance_of(Vendor) + Vendor.read[20].must_be_instance_of(Vendor) + end + end + + describe "self.all" do it "should return an array" do Vendor.all.must_be_kind_of(Array) end