From 8afacac3682a349c189bfad479a0b67e4286871c Mon Sep 17 00:00:00 2001 From: Wesley Willis Date: Mon, 19 Oct 2015 18:41:57 -0700 Subject: [PATCH 01/13] add self.new and spec for market class --- lib/far_mar/market.rb | 34 +++++++++++++++++++++++++++++++++- spec/far_mar/market_spec.rb | 16 +++++++++++++--- 2 files changed, 46 insertions(+), 4 deletions(-) diff --git a/lib/far_mar/market.rb b/lib/far_mar/market.rb index 9f073ca7..b4367916 100644 --- a/lib/far_mar/market.rb +++ b/lib/far_mar/market.rb @@ -1,7 +1,39 @@ +require "pry" module FarMar + class Market - def initialize + attr_accessor :id, :name, :address, :city, :county, :state, :zip + def initialize(market_hash) + @id = market_hash[:id] + @name = market_hash[:name] + @address = market_hash[:address] + @city = market_hash[:city] + @county = market_hash[:county] + @state = market_hash[:state] + @zip = market_hash[:zip] + end + def self.all + markets_all = [] + markets_data = CSV.read("support/markets.csv") + + markets_data.each do |id, name, address, city, county, state, zip| + #binding.pry + market_hash = {:id => id, :name => name, :address => address, :city => city, :county => county, :state => state, :zip => zip} + market = FarMar::Market.new(market_hash) + markets_all.push(market) + end + return markets_all end + + #def self.all + # markets_csv = CSV.read("./support/markets") + # markets_csv.foreach do |id, name, address, city, county, state, zip| + # Market.new(:id => id, :name => name, :address => address, :city => city, :county => county, :state => state, :zip => zip) + # end + #end end end + #CSV.foreach(file_path, :headers => true, :col_sep => ';') do |row| + #Car.create(:make => row[0], :model => row[1], :year => row[2]) +#end diff --git a/spec/far_mar/market_spec.rb b/spec/far_mar/market_spec.rb index ac7b8e77..84367b9b 100644 --- a/spec/far_mar/market_spec.rb +++ b/spec/far_mar/market_spec.rb @@ -3,13 +3,23 @@ describe FarMar do describe FarMar::Market do before :each do - @market = FarMar::Market.new + @market = FarMar::Market.all end describe "#initialize" do it "creates a new instance of Market" do - expect(@market).to be_an_instance_of FarMar::Market + fake_hash = {:id => 666, :name => "Boo Boo", :address => "123 Haunted House Way", :city => "Spooky Estates", :county => "Cook", :state => "IL", :zip => 60616} + test = FarMar::Market.new(fake_hash) + expect(test).to be_an_instance_of FarMar::Market end end + + describe '#self.all' do + it "returns a collection of Market instances" do + + expect(@market.length).to eq 4 + end + + end end -end +end From 8541653b9df4723d437ef4e0cc36d7d7d6823066 Mon Sep 17 00:00:00 2001 From: Wesley Willis Date: Tue, 20 Oct 2015 18:54:42 -0700 Subject: [PATCH 02/13] change self.all to only load CSV once --- lib/far_mar.rb | 1 + lib/far_mar/market.rb | 36 +++++++++++++++++------------------- spec/far_mar/market_spec.rb | 11 ++++++++--- spec/far_mar/product_spec.rb | 12 ++++++------ template.rb | 0 5 files changed, 32 insertions(+), 28 deletions(-) create mode 100644 template.rb diff --git a/lib/far_mar.rb b/lib/far_mar.rb index b0453783..0fd6abf0 100644 --- a/lib/far_mar.rb +++ b/lib/far_mar.rb @@ -1,5 +1,6 @@ require "csv" require "time" + require "./lib/far_mar/market" require "./lib/far_mar/product" require "./lib/far_mar/sale" diff --git a/lib/far_mar/market.rb b/lib/far_mar/market.rb index b4367916..743d9867 100644 --- a/lib/far_mar/market.rb +++ b/lib/far_mar/market.rb @@ -4,7 +4,7 @@ module FarMar class Market attr_accessor :id, :name, :address, :city, :county, :state, :zip def initialize(market_hash) - @id = market_hash[:id] + @id = market_hash[:id].to_i @name = market_hash[:name] @address = market_hash[:address] @city = market_hash[:city] @@ -13,27 +13,25 @@ def initialize(market_hash) @zip = market_hash[:zip] end - def self.all - markets_all = [] - markets_data = CSV.read("support/markets.csv") + #MARKET_DATA = CSV.read("support/markets.csv") - markets_data.each do |id, name, address, city, county, state, zip| - #binding.pry - market_hash = {:id => id, :name => name, :address => address, :city => city, :county => county, :state => state, :zip => zip} - market = FarMar::Market.new(market_hash) - markets_all.push(market) + def self.all + @@markets_all ||= [] + if @@markets_all == [] + CSV.read("support/markets.csv").each do |id, name, address, city, county, state, zip| + #binding.pry + market_hash = {:id => id.to_i, :name => name, :address => address, :city => city, :county => county, :state => state, :zip => zip} + market = FarMar::Market.new(market_hash) + @@markets_all.push(market) + end end - return markets_all + return @@markets_all end - #def self.all - # markets_csv = CSV.read("./support/markets") - # markets_csv.foreach do |id, name, address, city, county, state, zip| - # Market.new(:id => id, :name => name, :address => address, :city => city, :county => county, :state => state, :zip => zip) - # end - #end + def self.find(id) + Market.all.find do |market| + id == market.id + end + end end end - #CSV.foreach(file_path, :headers => true, :col_sep => ';') do |row| - #Car.create(:make => row[0], :model => row[1], :year => row[2]) -#end diff --git a/spec/far_mar/market_spec.rb b/spec/far_mar/market_spec.rb index 84367b9b..8155c0b4 100644 --- a/spec/far_mar/market_spec.rb +++ b/spec/far_mar/market_spec.rb @@ -3,7 +3,7 @@ describe FarMar do describe FarMar::Market do before :each do - @market = FarMar::Market.all + #@market = FarMar::Market.all end describe "#initialize" do @@ -16,10 +16,15 @@ describe '#self.all' do it "returns a collection of Market instances" do - - expect(@market.length).to eq 4 + expect(FarMar::Market.all.length).to eq CSV.read("support/markets.csv").length end + end + describe "self.find(id)" do + it "returns specific instance of market" do + find_test = FarMar::Market.find(15) + expect(find_test.name).to eq "Farmers Market in Denison" + end end end end diff --git a/spec/far_mar/product_spec.rb b/spec/far_mar/product_spec.rb index 1aecde05..a39710eb 100644 --- a/spec/far_mar/product_spec.rb +++ b/spec/far_mar/product_spec.rb @@ -3,13 +3,13 @@ describe FarMar do describe FarMar::Product do before :each do - @product = FarMar::Product.new + @product = FarMar::Product.all end - describe "#initialize" do - it "creates a new instance of Product" do - expect(@product).to be_an_instance_of FarMar::Product - end - end + #describe "#initialize" do + #it "creates a new instance of Product" do + # expect(@product.length).to be_an_instance_of FarMar::Product + # end + #end end end diff --git a/template.rb b/template.rb new file mode 100644 index 00000000..e69de29b From be1f0676ef8c0025371ddcfc5a692c3a9e297e80 Mon Sep 17 00:00:00 2001 From: Wesley Willis Date: Wed, 21 Oct 2015 14:26:28 -0700 Subject: [PATCH 03/13] add list vendors method to Market Class and spec --- lib/far_mar/market.rb | 10 ++++++++++ lib/far_mar/vendor.rb | 26 +++++++++++++++++++++++++- spec/far_mar/market_spec.rb | 9 +++++++++ spec/far_mar/vendor_spec.rb | 20 ++++++++++++++++++-- 4 files changed, 62 insertions(+), 3 deletions(-) diff --git a/lib/far_mar/market.rb b/lib/far_mar/market.rb index 743d9867..6c24e318 100644 --- a/lib/far_mar/market.rb +++ b/lib/far_mar/market.rb @@ -33,5 +33,15 @@ def self.find(id) id == market.id end end + + def list_vendors + vendor_list = [] + FarMar::Vendor.all.each do |vendor| + if id == vendor.market_id + vendor_list.push(vendor) + end + end + vendor_list + end end end diff --git a/lib/far_mar/vendor.rb b/lib/far_mar/vendor.rb index cbb4dcd0..55663d51 100644 --- a/lib/far_mar/vendor.rb +++ b/lib/far_mar/vendor.rb @@ -1,7 +1,31 @@ module FarMar + class Vendor - def initialize + attr_accessor :id, :name, :team_size, :market_id + def initialize(vendor_hash) + @id = vendor_hash[:id].to_i + @name = vendor_hash[:name] + @team_size = vendor_hash[:team_size] + @market_id = vendor_hash[:market_id].to_i + end + + def self.all + @@vendors_all ||= [] + if @@vendors_all == [] + CSV.read("support/vendors.csv").each do |id, name, team_size, market_id| + #binding.pry + vendor_hash = {:id => id.to_i, :name => name, :team_size => team_size, :market_id => market_id} + market = FarMar::Vendor.new(vendor_hash) + @@vendors_all.push(market) + end + end + return @@vendors_all + end + def self.find(id) + Vendor.all.find do |vendor| + id == vendor.id + end end end end diff --git a/spec/far_mar/market_spec.rb b/spec/far_mar/market_spec.rb index 8155c0b4..ff2bdaf2 100644 --- a/spec/far_mar/market_spec.rb +++ b/spec/far_mar/market_spec.rb @@ -26,5 +26,14 @@ expect(find_test.name).to eq "Farmers Market in Denison" end end + + describe "list_vendors(id)" do + it "returns array of vendors at specific market" do + vendor_list_test = FarMar::Market.find(20) + vendor_list_test.list_vendors.each do |vendor| + expect(vendor.market_id).to eq 20 + end + end + end end end diff --git a/spec/far_mar/vendor_spec.rb b/spec/far_mar/vendor_spec.rb index 6be401c5..8f9f229c 100644 --- a/spec/far_mar/vendor_spec.rb +++ b/spec/far_mar/vendor_spec.rb @@ -3,13 +3,29 @@ describe FarMar do describe FarMar::Vendor do before :each do - @vendor = FarMar::Vendor.new + # @vendor = FarMar::Vendor.new end describe "#initialize" do it "creates a new instance of Vendor" do - expect(@vendor).to be_an_instance_of FarMar::Vendor + fake_hash = {:id => 666, :name => "Boo Boo", :team_size => 12, :market_id => 555} + test = FarMar::Vendor.new(fake_hash) + expect(test).to be_an_instance_of FarMar::Vendor end end + + describe '#self.all' do + it "returns a collection of Vendor instances" do + expect(FarMar::Vendor.all.length).to eq CSV.read("support/vendors.csv").length + end + end + + describe "self.find(id)" do + it "returns specific instance of vendor" do + find_test = FarMar::Vendor.find(15) + expect(find_test.name).to eq "Hyatt-King" + end + end + end end From 76e8121d62b455d4d2b15ca4931937e451d4ebf2 Mon Sep 17 00:00:00 2001 From: Wesley Willis Date: Wed, 21 Oct 2015 15:12:46 -0700 Subject: [PATCH 04/13] add self methods and specs to Vendor and Product --- lib/far_mar/product.rb | 25 ++++++++++++++++++++++++- spec/far_mar/product_spec.rb | 27 +++++++++++++++++++++------ spec/far_mar/vendor_spec.rb | 1 - 3 files changed, 45 insertions(+), 8 deletions(-) diff --git a/lib/far_mar/product.rb b/lib/far_mar/product.rb index e4d3b79a..a8239dbc 100644 --- a/lib/far_mar/product.rb +++ b/lib/far_mar/product.rb @@ -1,7 +1,30 @@ module FarMar + class Product - def initialize + attr_accessor :id, :name, :vendor_id + def initialize(product_hash) + @id = product_hash[:id] + @name= product_hash[:name] + @vendor_id= product_hash[:vendor_id] + end + + def self.all + @@products_all ||= [] + if @@products_all == [] + CSV.read("support/products.csv").each do |id, name, vendor_id| + #binding.pry + product_hash = {:id => id.to_i, :name => name, :vendor_id => vendor_id} + product = FarMar::Product.new(product_hash) + @@products_all.push(product) + end + end + return @@products_all + end + def self.find(id) + Product.all.find do |product| + id == product.id + end end end end diff --git a/spec/far_mar/product_spec.rb b/spec/far_mar/product_spec.rb index a39710eb..28f7efe0 100644 --- a/spec/far_mar/product_spec.rb +++ b/spec/far_mar/product_spec.rb @@ -3,13 +3,28 @@ describe FarMar do describe FarMar::Product do before :each do - @product = FarMar::Product.all + #@product = FarMar::Product.all end - #describe "#initialize" do - #it "creates a new instance of Product" do - # expect(@product.length).to be_an_instance_of FarMar::Product - # end - #end + describe "#initialize" do + it "creates a new instance of Product" do + fake_hash = {:id => 111, :name => "Snickers", :vendor_id => 909} + test = FarMar::Product.new(fake_hash) + expect(test).to be_an_instance_of FarMar::Product + end + end + + describe '#self.all' do + it "returns a collection of Product instances" do + expect(FarMar::Product.all.length).to eq CSV.read("support/products.csv").length + end + end + + describe "self.find(id)" do + it "returns specific instance of product" do + find_test = FarMar::Product.find(15) + expect(find_test.name).to eq "Comfortable Pretzel" + end + end end end diff --git a/spec/far_mar/vendor_spec.rb b/spec/far_mar/vendor_spec.rb index 8f9f229c..6013bafe 100644 --- a/spec/far_mar/vendor_spec.rb +++ b/spec/far_mar/vendor_spec.rb @@ -26,6 +26,5 @@ expect(find_test.name).to eq "Hyatt-King" end end - end end From c13adb74172e6f5f52b84d2cdacebe9c30198046 Mon Sep 17 00:00:00 2001 From: Wesley Willis Date: Wed, 21 Oct 2015 15:47:02 -0700 Subject: [PATCH 05/13] add instance and class method for Sale class --- lib/far_mar/sale.rb | 20 +++++++++++++++++++- spec/far_mar/sale_spec.rb | 12 ++++++++++-- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/lib/far_mar/sale.rb b/lib/far_mar/sale.rb index 5604747f..b3def04b 100644 --- a/lib/far_mar/sale.rb +++ b/lib/far_mar/sale.rb @@ -1,7 +1,25 @@ module FarMar class Sale - def initialize + attr_accessor :id, :amount, :purchase_time, :vendor_id, :product_id + def initialize(sale_hash) + @id = sale_hash[:id].to_i + @amount = sale_hash[:amount].to_i + @purchase_time = DateTime.parse(sale_hash[:purchase_time]) + @vendor_id = sale_hash[:vendor_id] + @product_id = sale_hash[:product_id] + end + def self.all + @@sales_all ||= [] + if @@sales_all == [] + CSV.read("support/sales.csv").each do |id, amount, purchase_time, vendor_id, product_id| + sale_hash = {:id => id, :amount => amount, :purchase_time => purchase_time, :vendor_id => vendor_id, :product_id => product_id} + market = FarMar::Sale.new(sale_hash) + @@sales_all.push(market) + end + end + return @@sales_all end + end end diff --git a/spec/far_mar/sale_spec.rb b/spec/far_mar/sale_spec.rb index 206255d2..fa27d296 100644 --- a/spec/far_mar/sale_spec.rb +++ b/spec/far_mar/sale_spec.rb @@ -3,12 +3,20 @@ describe FarMar do describe FarMar::Sale do before :each do - @sale = FarMar::Sale.new + #@sale = FarMar::Sale.new end describe "#initialize" do it "creates a new instance of Sale" do - expect(@sale).to be_an_instance_of FarMar::Sale + fake_hash = {:id => 6, :amount => 150, :purchase_time => "2007-01-31 12:22:26", :vendor_id => 555, :product_id => 4} + test = FarMar::Sale.new(fake_hash) + expect(test).to be_an_instance_of FarMar::Sale + end + end + + describe '#self.all' do + it "returns a collection of Sale instances" do + expect(FarMar::Sale.all.length).to eq CSV.read("support/sales.csv").length end end end From 2808c8f4a8039bbfadc7a90f92b226048e589c30 Mon Sep 17 00:00:00 2001 From: Wesley Willis Date: Wed, 21 Oct 2015 15:55:16 -0700 Subject: [PATCH 06/13] add Sale.find method --- lib/far_mar/sale.rb | 10 ++++++++-- spec/far_mar/sale_spec.rb | 8 ++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/lib/far_mar/sale.rb b/lib/far_mar/sale.rb index b3def04b..1effdeca 100644 --- a/lib/far_mar/sale.rb +++ b/lib/far_mar/sale.rb @@ -5,8 +5,8 @@ def initialize(sale_hash) @id = sale_hash[:id].to_i @amount = sale_hash[:amount].to_i @purchase_time = DateTime.parse(sale_hash[:purchase_time]) - @vendor_id = sale_hash[:vendor_id] - @product_id = sale_hash[:product_id] + @vendor_id = sale_hash[:vendor_id].to_i + @product_id = sale_hash[:product_id].to_i end def self.all @@ -21,5 +21,11 @@ def self.all return @@sales_all end + def self.find(id) + Sale.all.find do |sale| + id == sale.id + end + end + end end diff --git a/spec/far_mar/sale_spec.rb b/spec/far_mar/sale_spec.rb index fa27d296..92dd60fc 100644 --- a/spec/far_mar/sale_spec.rb +++ b/spec/far_mar/sale_spec.rb @@ -19,5 +19,13 @@ expect(FarMar::Sale.all.length).to eq CSV.read("support/sales.csv").length end end + + describe "self.find(id)" do + it "returns specific instance of Sale" do + find_test = FarMar::Sale.find(15) + expect(find_test.product_id).to eq 4 + end + end + end end From 7d5284a0fbc2c5359fb8459f77a6a9985188ef81 Mon Sep 17 00:00:00 2001 From: Wesley Willis Date: Wed, 21 Oct 2015 16:16:42 -0700 Subject: [PATCH 07/13] add placeholders for upcoming methods in product, sale, and vendor classes --- lib/far_mar/product.rb | 12 ++++++++++++ lib/far_mar/sale.rb | 11 +++++++++++ lib/far_mar/vendor.rb | 15 +++++++++++++++ 3 files changed, 38 insertions(+) diff --git a/lib/far_mar/product.rb b/lib/far_mar/product.rb index a8239dbc..b87a0cce 100644 --- a/lib/far_mar/product.rb +++ b/lib/far_mar/product.rb @@ -26,5 +26,17 @@ def self.find(id) id == product.id end end + + def self.by_vendor(vendor_id) + end + + def list_vendors + end + + def list_sales + end + + def number_of_sales + end end end diff --git a/lib/far_mar/sale.rb b/lib/far_mar/sale.rb index 1effdeca..d9ca5823 100644 --- a/lib/far_mar/sale.rb +++ b/lib/far_mar/sale.rb @@ -27,5 +27,16 @@ def self.find(id) end end + def self.between(start_time,stop_time) + end + + def locate_vendors + end + + def locate_products + end + + + end end diff --git a/lib/far_mar/vendor.rb b/lib/far_mar/vendor.rb index 55663d51..606981fc 100644 --- a/lib/far_mar/vendor.rb +++ b/lib/far_mar/vendor.rb @@ -27,5 +27,20 @@ def self.find(id) id == vendor.id end end + + def self.by_market(market_id) + end + + def list_markets + end + + def list_products + end + + def list_sales + end + + def revenue + end end end From 7dd565a9d7e1f7eb8ff6069e1d40d2e3eea5fe36 Mon Sep 17 00:00:00 2001 From: Wesley Willis Date: Thu, 22 Oct 2015 11:31:45 -0700 Subject: [PATCH 08/13] fix bug in product class - product and vendor ID's should have been strings. add methods and specs to vendor class --- lib/far_mar/product.rb | 7 ++++--- lib/far_mar/vendor.rb | 12 ++++++++++++ spec/far_mar/vendor_spec.rb | 17 +++++++++++++++++ 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/lib/far_mar/product.rb b/lib/far_mar/product.rb index b87a0cce..a2c96d9c 100644 --- a/lib/far_mar/product.rb +++ b/lib/far_mar/product.rb @@ -3,9 +3,9 @@ module FarMar class Product attr_accessor :id, :name, :vendor_id def initialize(product_hash) - @id = product_hash[:id] + @id = product_hash[:id].to_i @name= product_hash[:name] - @vendor_id= product_hash[:vendor_id] + @vendor_id= product_hash[:vendor_id].to_i end def self.all @@ -16,6 +16,7 @@ def self.all product_hash = {:id => id.to_i, :name => name, :vendor_id => vendor_id} product = FarMar::Product.new(product_hash) @@products_all.push(product) + end end return @@products_all @@ -28,7 +29,7 @@ def self.find(id) end def self.by_vendor(vendor_id) - end + end def list_vendors end diff --git a/lib/far_mar/vendor.rb b/lib/far_mar/vendor.rb index 606981fc..ac3fd0b8 100644 --- a/lib/far_mar/vendor.rb +++ b/lib/far_mar/vendor.rb @@ -1,3 +1,4 @@ +require "pry" module FarMar class Vendor @@ -32,9 +33,20 @@ def self.by_market(market_id) end def list_markets + FarMar::Market.find(market_id) end def list_products + product_list = [] + FarMar::Product.all.each do |product| + if id == product.vendor_id + #binding.pry + product_list.push(product) + #binding.pry + end + end + product_list + #binding.pry end def list_sales diff --git a/spec/far_mar/vendor_spec.rb b/spec/far_mar/vendor_spec.rb index 6013bafe..467e8ea0 100644 --- a/spec/far_mar/vendor_spec.rb +++ b/spec/far_mar/vendor_spec.rb @@ -26,5 +26,22 @@ expect(find_test.name).to eq "Hyatt-King" end end + + describe "list_markets" do + it "returns specific market instance" do + market_test = FarMar::Vendor.find(20) + expect(market_test.list_markets.name).to eq "Jefferson City Farmer's Market" + + end + end + + describe "list_products" do + it "returns array of product instances" do + product_test = FarMar::Vendor.find(100) + product_test.list_products.each do |product| + expect(product.vendor_id).to eq 100 + end + end + end end end From 9a1b6d031651ea0310b9a8ed29aaa52190498246 Mon Sep 17 00:00:00 2001 From: Wesley Willis Date: Thu, 22 Oct 2015 12:22:42 -0700 Subject: [PATCH 09/13] add self.by_market and spec to vendor class --- lib/far_mar/vendor.rb | 14 ++++++++++---- spec/far_mar/vendor_spec.rb | 20 ++++++++++++++++++++ 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/lib/far_mar/vendor.rb b/lib/far_mar/vendor.rb index ac3fd0b8..ccdf8e0f 100644 --- a/lib/far_mar/vendor.rb +++ b/lib/far_mar/vendor.rb @@ -6,7 +6,7 @@ class Vendor def initialize(vendor_hash) @id = vendor_hash[:id].to_i @name = vendor_hash[:name] - @team_size = vendor_hash[:team_size] + @team_size = vendor_hash[:team_size].to_i @market_id = vendor_hash[:market_id].to_i end @@ -30,6 +30,8 @@ def self.find(id) end def self.by_market(market_id) + FarMar::Market.find(market_id).list_vendors + #list = FarMar::Market.list_vendors end def list_markets @@ -40,16 +42,20 @@ def list_products product_list = [] FarMar::Product.all.each do |product| if id == product.vendor_id - #binding.pry product_list.push(product) - #binding.pry end end product_list - #binding.pry end def list_sales + sales_list = [] + FarMar::Sale.all.each do |sale| + if id == sale.vendor_id + sales_list.push(sale) + end + end + sales_list end def revenue diff --git a/spec/far_mar/vendor_spec.rb b/spec/far_mar/vendor_spec.rb index 467e8ea0..661fdbb3 100644 --- a/spec/far_mar/vendor_spec.rb +++ b/spec/far_mar/vendor_spec.rb @@ -43,5 +43,25 @@ end end end + + describe "list_sales" do + it "returns array of sale instances" do + sale_test = FarMar::Vendor.find(50) + sale_test.list_sales.each do |sale| + expect(sale.vendor_id).to eq 50 + end + end + end + + describe "self.by_market" do + it "returns array of markets" do + testcase = FarMar::Vendor.by_market(27) + testcase.each do |vendor| + expect(vendor.list_markets.name).to eq "Pinckneyville Farmers Market" + end + end + end + + end end From 5c058920328ed60991daf3b55f5f22f07d0f88c5 Mon Sep 17 00:00:00 2001 From: Wesley Willis Date: Thu, 22 Oct 2015 14:51:06 -0700 Subject: [PATCH 10/13] add more primary requirement methods to sale and vendor classes --- lib/far_mar/product.rb | 10 ++++++++++ lib/far_mar/sale.rb | 6 ++++-- lib/far_mar/vendor.rb | 5 +++++ spec/far_mar/product_spec.rb | 32 ++++++++++++++++++++++++++++++++ spec/far_mar/sale_spec.rb | 16 ++++++++++++++++ spec/far_mar/vendor_spec.rb | 9 +++++++-- 6 files changed, 74 insertions(+), 4 deletions(-) diff --git a/lib/far_mar/product.rb b/lib/far_mar/product.rb index a2c96d9c..c8e68226 100644 --- a/lib/far_mar/product.rb +++ b/lib/far_mar/product.rb @@ -29,15 +29,25 @@ def self.find(id) end def self.by_vendor(vendor_id) + FarMar::Vendor.find(vendor_id).list_products end def list_vendors + FarMar::Vendor.find(vendor_id) end def list_sales + sales_list = [] + FarMar::Sale.all.each do |sale| + if id == sale.product_id + sales_list.push(sale) + end + end + sales_list end def number_of_sales + list_sales.length end end end diff --git a/lib/far_mar/sale.rb b/lib/far_mar/sale.rb index d9ca5823..8160d3ab 100644 --- a/lib/far_mar/sale.rb +++ b/lib/far_mar/sale.rb @@ -30,10 +30,12 @@ def self.find(id) def self.between(start_time,stop_time) end - def locate_vendors + def list_vendors + FarMar::Vendor.find(vendor_id) end - def locate_products + def list_products + FarMar::Product.find(product_id) end diff --git a/lib/far_mar/vendor.rb b/lib/far_mar/vendor.rb index ccdf8e0f..edbf036f 100644 --- a/lib/far_mar/vendor.rb +++ b/lib/far_mar/vendor.rb @@ -59,6 +59,11 @@ def list_sales end def revenue + money = 0 + list_sales.each do |sale| + money += sale.amount + end + return money end end end diff --git a/spec/far_mar/product_spec.rb b/spec/far_mar/product_spec.rb index 28f7efe0..7d52f8a6 100644 --- a/spec/far_mar/product_spec.rb +++ b/spec/far_mar/product_spec.rb @@ -26,5 +26,37 @@ expect(find_test.name).to eq "Comfortable Pretzel" end end + + describe "list_vendors" do + it "returns specific vendor instance" do + vendor_test = FarMar::Product.find(98) + expect(vendor_test.list_vendors.name).to eq "Jakubowski-Robel" + end + end + + describe "list_sales" do + it "returns array of sale instances" do + sale_test = FarMar::Product.find(109) + sale_test.list_sales.each do |sale| + expect(sale.product_id).to eq 109 + end + end + end + + describe "number_of_sales" do + it "sums up total sale instances for specific product" do + number_test = FarMar::Product.find(8) + expect(number_test.number_of_sales).to eq 5 + end + end + + describe "self.by_vendor" do + it "returns array of products" do + testcase = FarMar::Product.by_vendor(202) + testcase.each do |product| + expect(product.list_vendors.name).to eq "Kerluke LLC" + end + end + end end end diff --git a/spec/far_mar/sale_spec.rb b/spec/far_mar/sale_spec.rb index 92dd60fc..0555e4b8 100644 --- a/spec/far_mar/sale_spec.rb +++ b/spec/far_mar/sale_spec.rb @@ -27,5 +27,21 @@ end end + describe "list_vendors" do + it "returns specific vendor instance" do + vendor_test = FarMar::Sale.find(455) + expect(vendor_test.list_vendors.name).to eq "Moen, Thiel and Osinski" + end + end + + describe "list_products" do + it "returns specific product instance" do + product_test = FarMar::Sale.find(413) + expect(product_test.list_products.name).to eq "Moen, Thiel and Osinski" + end + end + + + end end diff --git a/spec/far_mar/vendor_spec.rb b/spec/far_mar/vendor_spec.rb index 661fdbb3..6ac12773 100644 --- a/spec/far_mar/vendor_spec.rb +++ b/spec/far_mar/vendor_spec.rb @@ -54,7 +54,7 @@ end describe "self.by_market" do - it "returns array of markets" do + it "returns array of vendors" do testcase = FarMar::Vendor.by_market(27) testcase.each do |vendor| expect(vendor.list_markets.name).to eq "Pinckneyville Farmers Market" @@ -62,6 +62,11 @@ end end - + describe "revenue" do + it "returns vendor's total $" do + vendor_example = FarMar::Vendor.find(68) + expect(vendor_example.revenue).to eq 31308 + end + end end end From 2e961dff85b8b0ea33bd3c3e548b93d2c2171bbc Mon Sep 17 00:00:00 2001 From: Wesley Willis Date: Thu, 22 Oct 2015 15:44:44 -0700 Subject: [PATCH 11/13] add some code towards the Sale.between method --- lib/far_mar/sale.rb | 8 ++++++++ spec/far_mar/sale_spec.rb | 9 ++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/far_mar/sale.rb b/lib/far_mar/sale.rb index 8160d3ab..d80df1bb 100644 --- a/lib/far_mar/sale.rb +++ b/lib/far_mar/sale.rb @@ -28,6 +28,14 @@ def self.find(id) end def self.between(start_time,stop_time) + start_time = DateTime.parse(start_time.to_s) + stop_time = DateTime.parse(stop_time.to_s) + time_span_sales = [] + FarMar::Sale.all.each do |sale| + if purchase_time.between?(start_time,stop_time) + time_span_sales.push(sale) + end + end end def list_vendors diff --git a/spec/far_mar/sale_spec.rb b/spec/far_mar/sale_spec.rb index 0555e4b8..d3f3f7f1 100644 --- a/spec/far_mar/sale_spec.rb +++ b/spec/far_mar/sale_spec.rb @@ -27,6 +27,13 @@ end end + describe "self.between(start,stop)" do + it "returns array of sale instances" do + time_test = FarMar::Sale.between(9:00,16:00) + expect(time_test.length).to eq 9 + end + end + describe "list_vendors" do it "returns specific vendor instance" do vendor_test = FarMar::Sale.find(455) @@ -37,7 +44,7 @@ describe "list_products" do it "returns specific product instance" do product_test = FarMar::Sale.find(413) - expect(product_test.list_products.name).to eq "Moen, Thiel and Osinski" + expect(product_test.list_products.name).to eq "– Mushrooms" end end From 28f22f1ef4a643ee154087cdae37a180c5716e62 Mon Sep 17 00:00:00 2001 From: Wesley Willis Date: Fri, 23 Oct 2015 14:39:18 -0700 Subject: [PATCH 12/13] add final primary requirements --- lib/far_mar/sale.rb | 15 +++++---------- spec/far_mar/sale_spec.rb | 11 ++++++++--- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/lib/far_mar/sale.rb b/lib/far_mar/sale.rb index d80df1bb..621be4e0 100644 --- a/lib/far_mar/sale.rb +++ b/lib/far_mar/sale.rb @@ -4,7 +4,7 @@ class Sale def initialize(sale_hash) @id = sale_hash[:id].to_i @amount = sale_hash[:amount].to_i - @purchase_time = DateTime.parse(sale_hash[:purchase_time]) + @purchase_time = DateTime.strptime(sale_hash[:purchase_time], "%Y-%m-%d %H:%M:%S %z") @vendor_id = sale_hash[:vendor_id].to_i @product_id = sale_hash[:product_id].to_i end @@ -27,15 +27,10 @@ def self.find(id) end end - def self.between(start_time,stop_time) - start_time = DateTime.parse(start_time.to_s) - stop_time = DateTime.parse(stop_time.to_s) - time_span_sales = [] - FarMar::Sale.all.each do |sale| - if purchase_time.between?(start_time,stop_time) - time_span_sales.push(sale) - end - end + def self.between?(start_time, stop_time) + start_time = DateTime.strptime(start_time,"%Y-%m-%d %H:%M:%S %z") + stop_time = DateTime.strptime(stop_time,"%Y-%m-%d %H:%M:%S %z") + FarMar::Sale.all.find_all { |sale| sale.purchase_time.between?(start_time, stop_time) } end def list_vendors diff --git a/spec/far_mar/sale_spec.rb b/spec/far_mar/sale_spec.rb index d3f3f7f1..5397d2e5 100644 --- a/spec/far_mar/sale_spec.rb +++ b/spec/far_mar/sale_spec.rb @@ -8,7 +8,7 @@ describe "#initialize" do it "creates a new instance of Sale" do - fake_hash = {:id => 6, :amount => 150, :purchase_time => "2007-01-31 12:22:26", :vendor_id => 555, :product_id => 4} + fake_hash = {:id => 6, :amount => 150, :purchase_time => "2013-11-10 18:56:53 -0800", :vendor_id => 555, :product_id => 4} test = FarMar::Sale.new(fake_hash) expect(test).to be_an_instance_of FarMar::Sale end @@ -29,8 +29,13 @@ describe "self.between(start,stop)" do it "returns array of sale instances" do - time_test = FarMar::Sale.between(9:00,16:00) - expect(time_test.length).to eq 9 + time_test = FarMar::Sale.between?("2013-11-10 08:56:53 -0800","2013-11-10 08:59:53 -0800") + expect(time_test.length).to eq 2 + end + + it "returns array of sale instances" do + time_test = FarMar::Sale.between?("2010-11-10 08:56:53 -0800","2014-11-10 08:59:53 -0800") + expect(time_test.length).to eq 12798 end end From 27769fa5f7cdc9714fc573b08703e71581c73d5d Mon Sep 17 00:00:00 2001 From: Wesley Willis Date: Fri, 23 Oct 2015 15:39:45 -0700 Subject: [PATCH 13/13] working on first optional method for market --- lib/far_mar/market.rb | 10 ++++++++++ spec/far_mar/market_spec.rb | 9 +++++++++ 2 files changed, 19 insertions(+) diff --git a/lib/far_mar/market.rb b/lib/far_mar/market.rb index 6c24e318..dd41cfd0 100644 --- a/lib/far_mar/market.rb +++ b/lib/far_mar/market.rb @@ -43,5 +43,15 @@ def list_vendors end vendor_list end + + def products + vendors = FarMar::Vendor.by_market(id) + products = [] + vendors.each do |vendor| + goods = vendor.list_products + products.push(goods) + end + products + end end end diff --git a/spec/far_mar/market_spec.rb b/spec/far_mar/market_spec.rb index ff2bdaf2..e621dc9a 100644 --- a/spec/far_mar/market_spec.rb +++ b/spec/far_mar/market_spec.rb @@ -35,5 +35,14 @@ end end end + + describe "products" do + it "shows it is returning product instances" do + market = FarMar::Market.find(100) + market.products.each do |goods| + expect(goods.name).to eq "candy" + end + end + end end end