diff --git a/SPEC/far_mar/market_spec.rb b/SPEC/far_mar/market_spec.rb index a2996b60..45e4194e 100644 --- a/SPEC/far_mar/market_spec.rb +++ b/SPEC/far_mar/market_spec.rb @@ -1,13 +1,69 @@ -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: "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 - 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.city).to eq "Portland" + 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 + + describe "vendors" 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 + + # 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 92b3d9b1..d52901c5 100644 --- a/SPEC/far_mar/product_spec.rb +++ b/SPEC/far_mar/product_spec.rb @@ -2,12 +2,46 @@ describe FarMar::Product do before :each do - @product = FarMar::Product.new("cherries") + product_hash={ + id: 1, + name: "Dry Beets", + vendor_id: 1 + } + @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 "Dry Beets" + end + + it "knows about its associated data file" do + 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(1).id).to eq 1 + end + end end -end diff --git a/SPEC/far_mar/sale_spec.rb b/SPEC/far_mar/sale_spec.rb index 2978dc6e..61427408 100644 --- a/SPEC/far_mar/sale_spec.rb +++ b/SPEC/far_mar/sale_spec.rb @@ -2,12 +2,49 @@ describe FarMar::Sale do before :each do - @sale = FarMar::Sale.new("pounds") + sale_hash = { + id: 1, + amount: 9290, + purchase_time: "2013-11-07 04:34:56 -0800", + vendor_id: 1, + product_id: 1 + } + + @sale = FarMar::Sale.new(sale_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 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(@sale.id).to eq 1 + end + + it "knows bout its associated data file" do + 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(1).amount).to eq 9290 + end + end end -end diff --git a/SPEC/far_mar/vendor_spec.rb b/SPEC/far_mar/vendor_spec.rb index 2f7aebf1..47ff2883 100644 --- a/SPEC/far_mar/vendor_spec.rb +++ b/SPEC/far_mar/vendor_spec.rb @@ -2,12 +2,97 @@ describe FarMar::Vendor do before :each do - @vendor = FarMar::Vendor.new("Earth Farms") + vendor_hash= { + id: 1, + name: "Feil-Farrell", + num_employees: 8, + market_id: 1 + } + + @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 "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 + + 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 + + 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/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 2a91db44..8f35f905 100644 --- a/lib/far_mar/market.rb +++ b/lib/far_mar/market.rb @@ -1,7 +1,73 @@ +require 'csv' + 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 + + def vendors + #create an empty array + #match FarMar::Market id with FarMar:: Vendor id + FarMar::Vendor.all.find_all do |vendor| + vendor.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..c4c5ea45 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, :products_csv + def initialize(product_hash) + @id = product_hash[:id].to_i + @name = product_hash[:name] + @vendor_id = product_hash[:vendor_id].to_i + @products_csv = ("./support/products.csv") + end + + def self.all + @@product_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 do |product| + product.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..2c6e794a 100644 --- a/lib/far_mar/sale.rb +++ b/lib/far_mar/sale.rb @@ -1,7 +1,37 @@ +require 'csv' + module FarMar class Sale - def initialize(name) - @name = name + 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[: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") + end + + def self.all + @@sale_all ||= + + CSV.read("./support/sales.csv").map do |row| + #binding.pry + FarMar::Sale.new({ + id: row[0].to_i, + amount: row[1].to_i, + purchase_time: row[2], + vendor_id: row[3].to_i, + product_id: row[4].to_i, + }) + end + end + + def self.find(id) + # all_markets = FarMar::Market.all + all.find do |sale| + sale.id == id + end end end end diff --git a/lib/far_mar/vendor.rb b/lib/far_mar/vendor.rb index e6bb3e54..33c4ecba 100644 --- a/lib/far_mar/vendor.rb +++ b/lib/far_mar/vendor.rb @@ -1,7 +1,67 @@ +require 'csv' +require 'pry' 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 + @@vendor_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 do |vendor| + 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