From 3496a882437fce151510b62be0a38bf942de5abf Mon Sep 17 00:00:00 2001 From: Annalee Date: Thu, 22 Oct 2015 11:54:26 -0700 Subject: [PATCH 1/4] modified files with new code and specs for self.all and self.find --- SPEC/far_mar/market_spec.rb | 50 ++++++++++++++++++++++++++--- SPEC/far_mar/product_spec.rb | 19 +++++++++-- SPEC/far_mar/sale_spec.rb | 23 ++++++++++--- SPEC/far_mar/vendor_spec.rb | 19 +++++++++-- lib/far_mar/market.rb | 62 ++++++++++++++++++++++++++++++++++-- lib/far_mar/product.rb | 41 ++++++++++++++++++++++-- lib/far_mar/sale.rb | 42 ++++++++++++++++++++++-- lib/far_mar/vendor.rb | 39 +++++++++++++++++++++-- 8 files changed, 272 insertions(+), 23 deletions(-) diff --git a/SPEC/far_mar/market_spec.rb b/SPEC/far_mar/market_spec.rb index a2996b60..a25d2937 100644 --- a/SPEC/far_mar/market_spec.rb +++ b/SPEC/far_mar/market_spec.rb @@ -1,13 +1,53 @@ -require "spec_helper" +require 'spec_helper' describe FarMar::Market do -before :each do - @market = FarMar::Market.new("Rainier") -end + before :each do + market_hash = { + id: 1738, + name: "Newtown", + address: "123 Fake St. SW", + city: "Seattle", + county: "King", + state: "WA", + zip: "98146" + } + + @market = FarMar::Market.new(market_hash) + end - describe "self.new(name)" do + describe "self.new" do it "creates a new instance of Market" do expect(@market).to be_an_instance_of FarMar::Market end + + + it "has a name property that is equal to what is passed in" do + expect(@market.name).to eq "Newtown" + end + + it "knows about its associated data file" do + expect(@market.market_csv).to eq ("./support/markets.csv") + end + end + + describe "self.all" do + all_markets = FarMar::Market.all + it "returns an array" do + expect(all_markets.class).to eq Array + #test that it is an array + expect(all_markets[0]).to be_an_instance_of FarMar::Market + expect(all_markets[-1]).to be_an_instance_of FarMar::Market + market_csv= CSV.read("./support/markets.csv") + expect(all_markets.length).to eq (market_csv.length) + #all markets length to equal csv.length + end + end + +#returns the instance of Market with matching the input # ID + describe "self.find(id)" do + it "returns an instance of FarMar::Market with its passed in id" do + expect(FarMar::Market.find(1)).to be_an_instance_of FarMar::Market + expect(FarMar::Market.find(1).name).to eq "People's Co-op Farmers Market" + end end end diff --git a/SPEC/far_mar/product_spec.rb b/SPEC/far_mar/product_spec.rb index 92b3d9b1..76686b1f 100644 --- a/SPEC/far_mar/product_spec.rb +++ b/SPEC/far_mar/product_spec.rb @@ -2,12 +2,25 @@ describe FarMar::Product do before :each do - @product = FarMar::Product.new("cherries") + product_hash={ + id: 121, + name: "marigolds", + vendor_id: 45 + } + @product = FarMar::Product.new(product_hash) end - describe "self.new(name)" do - it "creates a new instance of Market" do + describe "self.new" do + it "creates a new instance of Product" do expect(@product).to be_an_instance_of FarMar::Product end + + it "has a name property that is equal to what is passed in" do + expect(@product.name).to eq "marigolds" + end + + it "knows about its associated data file" do + expect(@product.product_csv).to eq ("./support/products.csv") + end end end diff --git a/SPEC/far_mar/sale_spec.rb b/SPEC/far_mar/sale_spec.rb index 2978dc6e..39d758dd 100644 --- a/SPEC/far_mar/sale_spec.rb +++ b/SPEC/far_mar/sale_spec.rb @@ -1,13 +1,28 @@ require "spec_helper" -describe FarMar::Sale do +describe FarMar::Sales do before :each do - @sale = FarMar::Sale.new("pounds") + sales_hash = { + id: 311, + amount: 1200, + # purchase_time: 2016-11-09 09:34:56 -0800, + vendor_id: 34, + product_id: 9118 + } + @sales = FarMar::Sales.new(sales_hash) end - describe "self.new(name)" do + describe "self.new" do it "creates a new instance of Market" do - expect(@sale).to be_an_instance_of FarMar::Sale + expect(@sales).to be_an_instance_of FarMar::Sales + end + + it "has an id property that is equal to what it is passed in" do + expect(@sales.id).to eq 311 + end + + it "knows bout its associated data file" do + expect(@sales.sales_csv).to eq ("./support/sales.csv") end end end diff --git a/SPEC/far_mar/vendor_spec.rb b/SPEC/far_mar/vendor_spec.rb index 2f7aebf1..e4d1f898 100644 --- a/SPEC/far_mar/vendor_spec.rb +++ b/SPEC/far_mar/vendor_spec.rb @@ -2,12 +2,27 @@ describe FarMar::Vendor do before :each do - @vendor = FarMar::Vendor.new("Earth Farms") + vendor_hash= { + id: 1500, + name: "Remlinger Farms", + num_employees: 8, + market_id: 321 + } + @vendor = FarMar::Vendor.new(vendor_hash) end - describe "self.new(name)" do + describe "self.new" do it "creates a new instance of Vendor" do expect(@vendor).to be_an_instance_of FarMar::Vendor end + + + it "has a name property that is equal to what is passed in" do + expect(@vendor.name).to eq "Remlinger Farms" + end + + it "knows about its associated data file" do + expect(@vendor.vendor_csv).to eq ("./support/vendors.csv") + end end end diff --git a/lib/far_mar/market.rb b/lib/far_mar/market.rb index 2a91db44..a9817c28 100644 --- a/lib/far_mar/market.rb +++ b/lib/far_mar/market.rb @@ -1,7 +1,65 @@ +require 'csv' +require 'pry' module FarMar class Market - def initialize(name) - @name = name + attr_reader :id, :name, :address, :city, :county, :state, :zip, :market_csv + def initialize(market_hash) + @id = market_hash[:id].to_i + @name = market_hash[:name] + @address = market_hash[:address] + @city = market_hash[:city] + @county = market_hash[:county] + @state = market_hash[:state] + @zip = market_hash[:zip] + @market_csv = ("./support/markets.csv") + + end + + + def self.all + @@markets_all ||= + + CSV.read("support/markets.csv").map do |row| + #binding.pry + FarMar::Market.new({ + id: row[0].to_i, + name: row[1], + address: row[2], + city: row[3], + county: row[4], + state: row[5], + zip: row[6] + }) + end + end + + def self.find(id) + # all_markets = FarMar::Market.all + all.find do |market| + market.id == id + end end end end + +# +# +# +# +# +# +# +# +# +# +# +# +# market_hash = +# { id: 1738, +# name: "Newtown", +# address: "123 Fake St. SW", +# city: "Seattle", +# county: "King", +# state: "WA", +# zip: "98146" +# } diff --git a/lib/far_mar/product.rb b/lib/far_mar/product.rb index 4623571f..d7dc24e0 100644 --- a/lib/far_mar/product.rb +++ b/lib/far_mar/product.rb @@ -1,7 +1,44 @@ +require 'csv' module FarMar class Product - def initialize(name) - @name = name + attr_reader :id, :name, :vendor_id, :product_csv + def initialize(product_hash) + @id = product_hash[:id].to_i + @name = product_hash[:name] + @vendor_id = product_hash[:vendor_id].to_i + @product_csv = ("./support/products.csv") + end + + def self.all + @@products_all ||= + + CSV.read("support/products.csv").map do |row| + #binding.pry + FarMar::Product.new({ + id: row[0].to_i, + name: row[1], + vendor_id: row[2], + }) + end + end + + def self.find(id) + # all_markets = FarMar::Market.all + all.find(id) do |product| + market.id == id + end end end end + + + + + + + +# product_hash={ +# id: 121 +# name: "marigolds" +# vendor_id: 45 +# } diff --git a/lib/far_mar/sale.rb b/lib/far_mar/sale.rb index 707c146e..4824412d 100644 --- a/lib/far_mar/sale.rb +++ b/lib/far_mar/sale.rb @@ -1,7 +1,43 @@ +require 'csv' module FarMar - class Sale - def initialize(name) - @name = name + class Sales + attr_reader :id, :amount, :purchase_time, :vendor_id, :product_id, :sales_csv + def initialize(sales_hash) + @id = sales_hash[:id].to_i + @amount = sales_hash[:amont].to_i + @purchase_time = sales_hash[:purchase_time].time + @vendor_id = sales_hash[:vendor_id].to_i + @product_id = sales_hash[:product_id].to_i + @sales_csv = ("./support/sales.csv") + end + def self.all + @@products_all ||= + + CSV.read("support/sales.csv").map do |row| + #binding.pry + FarMar::Product.new({ + id: row[0].to_i, + name: row[1], + vendor_id: row[2], + }) + end + end + + def self.find(id) + # all_markets = FarMar::Market.all + all.find(id) do |sales| + market.id == id + end end end end + + + +# sale_hash = { +# id: 311, +# amount: 1200, +# purchase_time: 2016-11-09 09:34:56 -0800, +# vendor_id: 34, +# product_id: 9118 +# } diff --git a/lib/far_mar/vendor.rb b/lib/far_mar/vendor.rb index e6bb3e54..3df12216 100644 --- a/lib/far_mar/vendor.rb +++ b/lib/far_mar/vendor.rb @@ -1,7 +1,42 @@ +require 'csv' module FarMar class Vendor - def initialize(name) - @name = name + attr_reader :id, :name, :num_employees, :market_id , :vendor_csv + def initialize(vendor_hash) + @id = vendor_hash[:id].to_i + @name = vendor_hash[:name] + @num_employees= vendor_hash[:num_employees].to_i + @market_id = vendor_hash[:market_id].to_i + @vendor_csv = ("./support/vendors.csv") + end + + def self.all + @@vendors_all ||= + + CSV.read("support/vendors.csv").map do |row| + #binding.pry + FarMar::Vendor.new({ + id: row[0].to_i, + name: row[1], + num_employees: row[2].to_i, + market_id: row[3].to_i + }) + end + end + + def self.find(id) + # all_markets = FarMar::Market.all + all.find(id) do |vendors| + vendors.id == id + end end end end + + +# vendor_hash= +#{ id: 1500, +#name: "Remlinger Farms", +#num_employees: 8 +#market_id = +# } From bcada975ca4200576d4616745d05d08873a1d284 Mon Sep 17 00:00:00 2001 From: Annalee Date: Thu, 22 Oct 2015 15:38:59 -0700 Subject: [PATCH 2/4] modified specs and files --- SPEC/far_mar/market_spec.rb | 27 ++++++++++++++------ SPEC/far_mar/product_spec.rb | 33 ++++++++++++++++++++----- SPEC/far_mar/sale_spec.rb | 48 ++++++++++++++++++++++++++---------- SPEC/far_mar/vendor_spec.rb | 32 ++++++++++++++++++++---- lib/far_mar/market.rb | 10 +++++++- lib/far_mar/product.rb | 10 ++++---- lib/far_mar/sale.rb | 42 ++++++++++++++----------------- lib/far_mar/vendor.rb | 6 ++--- 8 files changed, 143 insertions(+), 65 deletions(-) diff --git a/SPEC/far_mar/market_spec.rb b/SPEC/far_mar/market_spec.rb index a25d2937..15d57a77 100644 --- a/SPEC/far_mar/market_spec.rb +++ b/SPEC/far_mar/market_spec.rb @@ -3,15 +3,16 @@ describe FarMar::Market do before :each do market_hash = { - id: 1738, - name: "Newtown", - address: "123 Fake St. SW", - city: "Seattle", - county: "King", - state: "WA", - zip: "98146" + id: 1, + name: "People's Co-op Farmers Market", + address: "30th and Burnside", + city: "Portland", + county: "Multnomah", + state: "Oregon", + zip: "97202" } + @market = FarMar::Market.new(market_hash) end @@ -22,7 +23,7 @@ it "has a name property that is equal to what is passed in" do - expect(@market.name).to eq "Newtown" + expect(@market.city).to eq "Portland" end it "knows about its associated data file" do @@ -50,4 +51,14 @@ expect(FarMar::Market.find(1).name).to eq "People's Co-op Farmers Market" end end + + describe "vendors" do + it "returns an array of FarMar::Vendors instances that are associated with the market by the market_id field." do + expect(@market.vendors.class).to eq Array + end + end end + + # it do + # expect(@market.vendors.length).to eq + # end diff --git a/SPEC/far_mar/product_spec.rb b/SPEC/far_mar/product_spec.rb index 76686b1f..4b37fa9b 100644 --- a/SPEC/far_mar/product_spec.rb +++ b/SPEC/far_mar/product_spec.rb @@ -3,9 +3,9 @@ describe FarMar::Product do before :each do product_hash={ - id: 121, - name: "marigolds", - vendor_id: 45 + id: 1, + name: "Dry Beets", + vendor_id: 1 } @product = FarMar::Product.new(product_hash) end @@ -16,11 +16,32 @@ end it "has a name property that is equal to what is passed in" do - expect(@product.name).to eq "marigolds" + expect(@product.name).to eq "Dry Beets" end it "knows about its associated data file" do - expect(@product.product_csv).to eq ("./support/products.csv") + expect(@product.products_csv).to eq ("./support/products.csv") + end + end + + describe "self.all" do + all_products = FarMar::Product.all + it "returns an array" do + expect(all_products.class).to eq Array + #test that it is an array + expect(all_products[0]).to be_an_instance_of FarMar::Product + expect(all_products[-1]).to be_an_instance_of FarMar::Product + products_csv= CSV.read("./support/products.csv") + expect(all_products.length).to eq (products_csv.length) + #all markets length to equal csv.length + end + end + + #returns the instance of Market with matching the input # ID + describe "self.find(id)" do + it "returns an instance of FarMar::Product with its passed in id" do + expect(FarMar::Product.find(1)).to be_an_instance_of FarMar::Product + expect(FarMar::Product.find("Dry Beets").name).to eq "Dry Beets" + end end end -end diff --git a/SPEC/far_mar/sale_spec.rb b/SPEC/far_mar/sale_spec.rb index 39d758dd..38271ca1 100644 --- a/SPEC/far_mar/sale_spec.rb +++ b/SPEC/far_mar/sale_spec.rb @@ -1,28 +1,50 @@ require "spec_helper" -describe FarMar::Sales do +describe FarMar::Sale do before :each do - sales_hash = { - id: 311, - amount: 1200, - # purchase_time: 2016-11-09 09:34:56 -0800, - vendor_id: 34, - product_id: 9118 + sale_hash = { + id: 1, + amount: 9290, + purchase_time: DateTime.parse(sale_hash[:purchase_time]), + vendor_id: 1, + product_id: 1 } - @sales = FarMar::Sales.new(sales_hash) + + @sale = FarMar::Sale.new(sale_hash) end describe "self.new" do - it "creates a new instance of Market" do - expect(@sales).to be_an_instance_of FarMar::Sales + it "creates a new instance of Sale" do + expect(@sale).to be_an_instance_of FarMar::Sale end it "has an id property that is equal to what it is passed in" do - expect(@sales.id).to eq 311 + expect(@sale.id).to eq 1 end it "knows bout its associated data file" do - expect(@sales.sales_csv).to eq ("./support/sales.csv") + expect(@sale.sales_csv).to eq ("./support/sales.csv") + end + end + + describe "self.all" do + all_sales = FarMar::Sale.all + it "returns an array" do + expect(all_sales.class).to eq Array + #test that it is an array + expect(all_sales[0]).to be_an_instance_of FarMar::Sale + expect(all_sales[-1]).to be_an_instance_of FarMar::Sale + sales_csv= CSV.read("./support/sales.csv") + expect(all_sales.length).to eq (sales_csv.length) + #all markets length to equal csv.length + end + end + + #returns the instance of Market with matching the input # ID + describe "self.find(id)" do + it "returns an instance of FarMar::Sale with its passed in id" do + expect(FarMar::Sale.find(1)).to be_an_instance_of FarMar::Sale + expect(FarMar::Sale.find(9390).amount).to eq 9390 + end end end -end diff --git a/SPEC/far_mar/vendor_spec.rb b/SPEC/far_mar/vendor_spec.rb index e4d1f898..a50e9d62 100644 --- a/SPEC/far_mar/vendor_spec.rb +++ b/SPEC/far_mar/vendor_spec.rb @@ -3,11 +3,12 @@ describe FarMar::Vendor do before :each do vendor_hash= { - id: 1500, - name: "Remlinger Farms", + id: 1, + name: "Feil-Farrell", num_employees: 8, - market_id: 321 + market_id: 1 } + @vendor = FarMar::Vendor.new(vendor_hash) end @@ -15,14 +16,35 @@ it "creates a new instance of Vendor" do expect(@vendor).to be_an_instance_of FarMar::Vendor end - + it "has a name property that is equal to what is passed in" do - expect(@vendor.name).to eq "Remlinger Farms" + expect(@vendor.name).to eq "Feil-Farrell" end it "knows about its associated data file" do expect(@vendor.vendor_csv).to eq ("./support/vendors.csv") end end + + describe "self.all" do + all_vendors = FarMar::Vendor.all + it "returns an array" do + expect(all_vendors.class).to eq Array + #test that it is an array + expect(all_vendors[0]).to be_an_instance_of FarMar::Vendor + expect(all_vendors[-1]).to be_an_instance_of FarMar::Vendor + vendor_csv= CSV.read("./support/vendors.csv") + expect(all_vendors.length).to eq (vendor_csv.length) + #all markets length to equal csv.length + end + end + + #returns the instance of Market with matching the input # ID + describe "self.find(id)" do + it "returns an instance of FarMar::Vendor with its passed in id" do + expect(FarMar::Vendor.find(1)).to be_an_instance_of FarMar::Vendor + expect(FarMar::Vendor.find(1).id).to eq 1 + end + end end diff --git a/lib/far_mar/market.rb b/lib/far_mar/market.rb index a9817c28..26907805 100644 --- a/lib/far_mar/market.rb +++ b/lib/far_mar/market.rb @@ -1,5 +1,5 @@ require 'csv' -require 'pry' + module FarMar class Market attr_reader :id, :name, :address, :city, :county, :state, :zip, :market_csv @@ -39,6 +39,14 @@ def self.find(id) market.id == id end end + + def vendors + #create an empty array + #match FarMar::Market id with FarMar:: Vendor id + FarMar::Vendors.all.find_all do |vendor| + vendor.market.id == @id + end + end end end diff --git a/lib/far_mar/product.rb b/lib/far_mar/product.rb index d7dc24e0..c4c5ea45 100644 --- a/lib/far_mar/product.rb +++ b/lib/far_mar/product.rb @@ -1,16 +1,16 @@ require 'csv' module FarMar class Product - attr_reader :id, :name, :vendor_id, :product_csv + attr_reader :id, :name, :vendor_id, :products_csv def initialize(product_hash) @id = product_hash[:id].to_i @name = product_hash[:name] @vendor_id = product_hash[:vendor_id].to_i - @product_csv = ("./support/products.csv") + @products_csv = ("./support/products.csv") end def self.all - @@products_all ||= + @@product_all ||= CSV.read("support/products.csv").map do |row| #binding.pry @@ -24,8 +24,8 @@ def self.all def self.find(id) # all_markets = FarMar::Market.all - all.find(id) do |product| - market.id == id + all.find do |product| + product.id == id end end end diff --git a/lib/far_mar/sale.rb b/lib/far_mar/sale.rb index 4824412d..34a03a47 100644 --- a/lib/far_mar/sale.rb +++ b/lib/far_mar/sale.rb @@ -1,43 +1,37 @@ require 'csv' + module FarMar - class Sales + class Sale attr_reader :id, :amount, :purchase_time, :vendor_id, :product_id, :sales_csv - def initialize(sales_hash) - @id = sales_hash[:id].to_i - @amount = sales_hash[:amont].to_i - @purchase_time = sales_hash[:purchase_time].time - @vendor_id = sales_hash[:vendor_id].to_i - @product_id = sales_hash[:product_id].to_i + def initialize(sale_hash) + @id = sale_hash[:id].to_i + @amount = sale_hash[:amont].to_i + @purchase_time = sale_hash[:purchase_time] + @vendor_id = sale_hash[:vendor_id].to_i + @product_id = sale_hash[:product_id].to_i @sales_csv = ("./support/sales.csv") end + def self.all - @@products_all ||= + @@sale_all ||= - CSV.read("support/sales.csv").map do |row| + CSV.read("./support/sales.csv").map do |row| #binding.pry - FarMar::Product.new({ + FarMar::Sale.new({ id: row[0].to_i, - name: row[1], - vendor_id: row[2], + amount: row[1], + purchase_time: row[2], + vendor_id: row[3], + product_id: row[4], }) end end def self.find(id) # all_markets = FarMar::Market.all - all.find(id) do |sales| - market.id == id + all.find do |sale| + sale.id == id end end end end - - - -# sale_hash = { -# id: 311, -# amount: 1200, -# purchase_time: 2016-11-09 09:34:56 -0800, -# vendor_id: 34, -# product_id: 9118 -# } diff --git a/lib/far_mar/vendor.rb b/lib/far_mar/vendor.rb index 3df12216..82dced80 100644 --- a/lib/far_mar/vendor.rb +++ b/lib/far_mar/vendor.rb @@ -11,7 +11,7 @@ def initialize(vendor_hash) end def self.all - @@vendors_all ||= + @@vendor_all ||= CSV.read("support/vendors.csv").map do |row| #binding.pry @@ -26,8 +26,8 @@ def self.all def self.find(id) # all_markets = FarMar::Market.all - all.find(id) do |vendors| - vendors.id == id + all.find do |vendor| + vendor.id == id end end end From f6b9dea2f3f0de961b36c3a622d81790b188d61f Mon Sep 17 00:00:00 2001 From: Annalee Date: Fri, 23 Oct 2015 10:11:24 -0700 Subject: [PATCH 3/4] Added additional market method and 2 of the additional .vendor --- SPEC/far_mar/market_spec.rb | 9 +++++++-- SPEC/far_mar/product_spec.rb | 2 +- SPEC/far_mar/sale_spec.rb | 4 ++-- SPEC/far_mar/vendor_spec.rb | 18 ++++++++++++++++++ SPEC/spec_helper.rb | 2 +- lib/far_mar/market.rb | 4 ++-- lib/far_mar/sale.rb | 10 +++++----- lib/far_mar/vendor.rb | 21 ++++++++++++--------- 8 files changed, 48 insertions(+), 22 deletions(-) diff --git a/SPEC/far_mar/market_spec.rb b/SPEC/far_mar/market_spec.rb index 15d57a77..45e4194e 100644 --- a/SPEC/far_mar/market_spec.rb +++ b/SPEC/far_mar/market_spec.rb @@ -3,7 +3,7 @@ describe FarMar::Market do before :each do market_hash = { - id: 1, + id: "1", name: "People's Co-op Farmers Market", address: "30th and Burnside", city: "Portland", @@ -53,9 +53,14 @@ end describe "vendors" do - it "returns an array of FarMar::Vendors instances that are associated with the market by the market_id field." do + it "returns an array of FarMar::Vendor instances that are associated with the market_id field." do expect(@market.vendors.class).to eq Array end + + it "returns 6" do + expect(@market.vendors.length).to eq 6 + end + end end diff --git a/SPEC/far_mar/product_spec.rb b/SPEC/far_mar/product_spec.rb index 4b37fa9b..d52901c5 100644 --- a/SPEC/far_mar/product_spec.rb +++ b/SPEC/far_mar/product_spec.rb @@ -41,7 +41,7 @@ describe "self.find(id)" do it "returns an instance of FarMar::Product with its passed in id" do expect(FarMar::Product.find(1)).to be_an_instance_of FarMar::Product - expect(FarMar::Product.find("Dry Beets").name).to eq "Dry Beets" + expect(FarMar::Product.find(1).id).to eq 1 end end end diff --git a/SPEC/far_mar/sale_spec.rb b/SPEC/far_mar/sale_spec.rb index 38271ca1..61427408 100644 --- a/SPEC/far_mar/sale_spec.rb +++ b/SPEC/far_mar/sale_spec.rb @@ -5,7 +5,7 @@ sale_hash = { id: 1, amount: 9290, - purchase_time: DateTime.parse(sale_hash[:purchase_time]), + purchase_time: "2013-11-07 04:34:56 -0800", vendor_id: 1, product_id: 1 } @@ -44,7 +44,7 @@ describe "self.find(id)" do it "returns an instance of FarMar::Sale with its passed in id" do expect(FarMar::Sale.find(1)).to be_an_instance_of FarMar::Sale - expect(FarMar::Sale.find(9390).amount).to eq 9390 + expect(FarMar::Sale.find(1).amount).to eq 9290 end end end diff --git a/SPEC/far_mar/vendor_spec.rb b/SPEC/far_mar/vendor_spec.rb index a50e9d62..2d4caba4 100644 --- a/SPEC/far_mar/vendor_spec.rb +++ b/SPEC/far_mar/vendor_spec.rb @@ -47,4 +47,22 @@ expect(FarMar::Vendor.find(1).id).to eq 1 end end + + describe ".markets" do + it "returns an instance of FarMar::Market" do + expect(@vendor.market).to be_an_instance_of FarMar::Market + end + + it "returns 6" do + expect(@vendor.market.name).to eq "People's Co-op Farmers Market" + end + end + + describe ".product" do + it "returns a collection of FarMar::Product instances that are associated by the FarMar::Product vendor_id field." do + expect(FarMar::Product.find(1)).to be_an_instance_of FarMar::Product + expect(FarMar::Product.find(1).vendor_id).to eq 1 + expect(@vendor.products.class).to eq Array + end + end end diff --git a/SPEC/spec_helper.rb b/SPEC/spec_helper.rb index 03753ffe..7b0de6d9 100644 --- a/SPEC/spec_helper.rb +++ b/SPEC/spec_helper.rb @@ -1,4 +1,4 @@ require "simplecov" SimpleCov.start - +require "pry" require "./lib/far_mar" diff --git a/lib/far_mar/market.rb b/lib/far_mar/market.rb index 26907805..8f35f905 100644 --- a/lib/far_mar/market.rb +++ b/lib/far_mar/market.rb @@ -43,8 +43,8 @@ def self.find(id) def vendors #create an empty array #match FarMar::Market id with FarMar:: Vendor id - FarMar::Vendors.all.find_all do |vendor| - vendor.market.id == @id + FarMar::Vendor.all.find_all do |vendor| + vendor.market_id == @id end end end diff --git a/lib/far_mar/sale.rb b/lib/far_mar/sale.rb index 34a03a47..2c6e794a 100644 --- a/lib/far_mar/sale.rb +++ b/lib/far_mar/sale.rb @@ -5,8 +5,8 @@ class Sale attr_reader :id, :amount, :purchase_time, :vendor_id, :product_id, :sales_csv def initialize(sale_hash) @id = sale_hash[:id].to_i - @amount = sale_hash[:amont].to_i - @purchase_time = sale_hash[:purchase_time] + @amount = sale_hash[:amount].to_i + @purchase_time = DateTime.parse(sale_hash[:purchase_time]) @vendor_id = sale_hash[:vendor_id].to_i @product_id = sale_hash[:product_id].to_i @sales_csv = ("./support/sales.csv") @@ -19,10 +19,10 @@ def self.all #binding.pry FarMar::Sale.new({ id: row[0].to_i, - amount: row[1], + amount: row[1].to_i, purchase_time: row[2], - vendor_id: row[3], - product_id: row[4], + vendor_id: row[3].to_i, + product_id: row[4].to_i, }) end end diff --git a/lib/far_mar/vendor.rb b/lib/far_mar/vendor.rb index 82dced80..b876cb25 100644 --- a/lib/far_mar/vendor.rb +++ b/lib/far_mar/vendor.rb @@ -13,7 +13,7 @@ def initialize(vendor_hash) def self.all @@vendor_all ||= - CSV.read("support/vendors.csv").map do |row| + CSV.read("./support/vendors.csv").map do |row| #binding.pry FarMar::Vendor.new({ id: row[0].to_i, @@ -30,13 +30,16 @@ def self.find(id) vendor.id == id end end + + def market + FarMar::Market.all.find do |market| + market.id == @market_id #vendor's, the other one is from FarMar::Market + end + end + def products + FarMar::Product.all.find_all do |product| + product.vendor_id == @id + end + end end end - - -# vendor_hash= -#{ id: 1500, -#name: "Remlinger Farms", -#num_employees: 8 -#market_id = -# } From c3316a05eca00747d62608303565371e6c40b992 Mon Sep 17 00:00:00 2001 From: Annalee Date: Fri, 23 Oct 2015 15:44:09 -0700 Subject: [PATCH 4/4] added methods --- SPEC/far_mar/vendor_spec.rb | 32 +++++++++++++++++++++++++++++++- lib/far_mar/vendor.rb | 24 +++++++++++++++++++++++- 2 files changed, 54 insertions(+), 2 deletions(-) diff --git a/SPEC/far_mar/vendor_spec.rb b/SPEC/far_mar/vendor_spec.rb index 2d4caba4..47ff2883 100644 --- a/SPEC/far_mar/vendor_spec.rb +++ b/SPEC/far_mar/vendor_spec.rb @@ -64,5 +64,35 @@ expect(FarMar::Product.find(1).vendor_id).to eq 1 expect(@vendor.products.class).to eq Array end + + it "returns 1" do + @vendor.products.each do |product| + expect(product.vendor_id).to eq @vendor.id + end + end + end + + describe ".sales" do + it "returns a collection of FarMar::Sale instances that are associated by the @vendor.id field" do + @vendor.sales.each do |sales| + expect(sales.vendor_id).to eq @vendor.id + end + end + + it "returns an array" do + expect(@vendor.sales.class).to eq Array + end + end + + describe ".revenue" do + it "returns the sum of Sale.amount values" do + expect(@vendor.revenue.class).to eq Fixnum + end + end + + it "returns 9290 for the sume of Sale.amount" do + FarMar::Sales.each do |sale| + expect(sale.amount).to eq 9290 + end + end end -end diff --git a/lib/far_mar/vendor.rb b/lib/far_mar/vendor.rb index b876cb25..33c4ecba 100644 --- a/lib/far_mar/vendor.rb +++ b/lib/far_mar/vendor.rb @@ -1,4 +1,5 @@ require 'csv' +require 'pry' module FarMar class Vendor attr_reader :id, :name, :num_employees, :market_id , :vendor_csv @@ -30,16 +31,37 @@ def self.find(id) vendor.id == id end end - def market FarMar::Market.all.find do |market| market.id == @market_id #vendor's, the other one is from FarMar::Market end end + def products FarMar::Product.all.find_all do |product| product.vendor_id == @id end end + + def sales + FarMar::Sale.all.find_all do |sales| + sales.vendor_id == @id + end + end + + def revenue + sale_cents = 0 + FarMar::Sale.all.each do |sale| + if sale.vendor_id == @id + sale_cents += sale.amount + end + end + return sale_cents + end + + + def self.by_market(market_id) + end + end end