Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FarMar Primary + Optionals #46

Open
wants to merge 44 commits into
base: dfcp/master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
b3931e0
Add tests and method for Market #all method, add empty #all and #find…
dezshino Oct 20, 2015
6ab9dc8
Add test and code for Market #find(id) method
dezshino Oct 20, 2015
41734d7
Refactor #all, initialize, and #find methods
dezshino Oct 21, 2015
233d1d2
Refactor code in Market class, add test and code for #all method in V…
dezshino Oct 21, 2015
3598bc5
Add Vendor#find method to pass tests
dezshino Oct 21, 2015
470c290
Update Product#initialize to accept a hash
dezshino Oct 21, 2015
c6911a1
Add Rspec test for Product#all method
dezshino Oct 21, 2015
a7fcb03
Add code for Product#find method to pass rspec
dezshino Oct 21, 2015
4cb1e59
Add code and test for Product #find
dezshino Oct 21, 2015
a535010
Update Sale #initialize to accept a hash
dezshino Oct 21, 2015
39a6cb6
Add test and method for Sale#all
dezshino Oct 21, 2015
840a9e7
Add test and method for Sale#find
dezshino Oct 21, 2015
a0b89de
Add test for Market .vendor method
dezshino Oct 21, 2015
b696ec4
Add Market .vendors method to pass rspec, add test for Vendor .market
dezshino Oct 21, 2015
6837edd
Add Vendor .market method to pass rspec
dezshino Oct 21, 2015
23e7c00
Add Vendor .products method and test
dezshino Oct 21, 2015
569fd6c
fix comment
dezshino Oct 21, 2015
586aa30
Add Vendor .sales method and test, improve prior vendor tests
dezshino Oct 21, 2015
debdbaf
Add Vendor .revenue method and tests
dezshino Oct 21, 2015
5757b70
Add Vendor #by_market(market_id) method and tests
dezshino Oct 21, 2015
a5834fe
Add another test for Vender #by_market method
dezshino Oct 21, 2015
bd19540
Add test for Product .vendor method
dezshino Oct 21, 2015
e08e16e
Add Product .vendor method
dezshino Oct 21, 2015
aafeb26
Add Product .sales method
dezshino Oct 21, 2015
d9a7b27
Add code and tests for Product .number_of_sales method
dezshino Oct 21, 2015
29629f1
Add test and method for Product #by_vendor
dezshino Oct 22, 2015
5d9320b
Add Sale .vendor method and test
dezshino Oct 22, 2015
973027e
Add Sale .product method and test
dezshino Oct 22, 2015
0ce0989
Require pry in the spec_helper file
dezshino Oct 22, 2015
74a71d0
Add Sale #between method and test
dezshino Oct 22, 2015
14bcf28
Add another test for Sale #between method
dezshino Oct 22, 2015
a443759
Refactor code in product.rb
dezshino Oct 22, 2015
4a76243
Refactor Vendor .revenue method and Sale #between method
dezshino Oct 22, 2015
e1a3e8c
Delete comment
dezshino Oct 22, 2015
eb07d55
Add tests
dezshino Oct 22, 2015
48aa8b5
Add Market .products method and test
dezshino Oct 22, 2015
a8cc17d
Add Market #search(search_term) method and test
dezshino Oct 22, 2015
9a71bad
Add test and method for Market .preferred_vendor
dezshino Oct 23, 2015
7d56973
Refactor Market .preferred_vendor method
dezshino Oct 23, 2015
0fe77a0
Add method to randomize order of running specs
dezshino Oct 23, 2015
a36f0b2
Add method and test for Market .worst_wendor method, refactor Market.…
dezshino Oct 23, 2015
d04be92
Refactor Market .preferred_vendor to accept a date, refactor Vendor .…
dezshino Oct 23, 2015
24da0ac
Refactor tests, refactor worst_vendor method to accept dates
dezshino Oct 23, 2015
757b099
Take out white space
dezshino Oct 23, 2015
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions lib/far_mar/market.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,86 @@
module FarMar
class Market
attr_reader :id, :name, :address, :city, :county, :state, :zip

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]
end

def self.all
# if @@all_markets is nil, reads the csv and creates the array
# otherwise uses array already in memory
@@all_markets ||= CSV.read('./support/markets.csv').map do |col|
FarMar::Market.new({
id: col[0],
name: col[1],
address: col[2],
city: col[3],
county: col[4],
state: col[5],
zip: col[6]
})
end
end

def self.find(id)
# can use all without self (all.find instead of self.all.find) because already in the class scope
all.find {|market| market.id == id}
end

def vendors
# returns a collection of FarMar::Vendor instances
# that are associated with the market by the market_id field.
FarMar::Vendor.all.find_all do |vendor|
vendor.market_id == @id
end
end

def products
# if just .map, this would give back an array of an array of instances
# .flat_map is like calling .flatten on the .map array
vendors.flat_map{|vendor| vendor.products }
end

def self.search(search_term)
all.find_all do |market|
market.name.downcase.include?(search_term.downcase) ||
market.vendors.any?{|vendor| vendor.name.downcase.include?(search_term.downcase)}
end
end

# This was my def preferred_vendor code before refactoring:
# highest_rev = 0
# pref_vendor = nil
# vendors.each do |vendor|
# if vendor.revenue > highest_rev
# highest_rev = vendor.revenue
# pref_vendor = vendor
# end
# end
# return pref_vendor

def preferred_vendor(date = nil)
if date == nil
vendors.max_by{|vendor|vendor.revenue}
else
vendors.max_by {|vendor| vendor.revenue(date)}
end
end

def worst_vendor(date = nil)
if date == nil
vendors.min_by{|vendor|vendor.revenue}
else
vendors.min_by{|vendor|vendor.revenue(date)}
end
end


end
end
37 changes: 37 additions & 0 deletions lib/far_mar/product.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,41 @@
module FarMar
class Product
attr_reader :id, :name, :vendor_id

def initialize(product_hash)
@id = product_hash[:id].to_i
@name = product_hash[:name]
@vendor_id = product_hash[:vendor_id].to_i
end

def self.all
@@all_products ||= CSV.read('./support/products.csv').map do |col|
FarMar::Product.new({
id: col[0],
name: col[1],
vendor_id: col[2]
})
end
end

def self.find(id)
all.find{|product| product.id == id}
end

def vendor
FarMar::Vendor.all.find {|vendor| vendor.id == @vendor_id}
end

def sales
FarMar::Sale.all.find_all {|sale| sale.product_id == @id}
end

def number_of_sales
sales.length
end

def self.by_vendor(vendor_id)
all.find_all{|product| product.vendor_id == vendor_id}
end
end
end
44 changes: 44 additions & 0 deletions lib/far_mar/sale.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,48 @@
module FarMar
class Sale
attr_reader :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].to_i
@product_id = sale_hash[:product_id].to_i
end

def self.all
@@all_sales ||= CSV.read('./support/sales.csv').map do |col|
FarMar::Sale.new({
id: col[0],
amount: col[1],
purchase_time: col[2],
vendor_id: col[3],
product_id: col[4]
})
end
end

def self.find(id)
all.find{|sale| sale.id == id}
end

def vendor
FarMar::Vendor.all.find do |vendor|
vendor.id == @vendor_id
end
end

def product
FarMar::Product.all.find do |product|
product.id == @product_id
end
end

def self.between(beginning_time, end_time)
all.find_all do |sale|
sale.purchase_time.between?(beginning_time, end_time)
end
end

end
end
71 changes: 71 additions & 0 deletions lib/far_mar/vendor.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,75 @@
module FarMar
class Vendor
attr_reader :id, :name, :employees, :market_id

def initialize(vendor_hash)
@id = vendor_hash[:id].to_i
@name = vendor_hash[:name]
@employees = vendor_hash[:employees].to_i
@market_id = vendor_hash[:market_id].to_i
end

def self.all
@@all_vendors ||= CSV.read('./support/vendors.csv').map do |col|
FarMar::Vendor.new({
id: col[0],
name: col[1],
employees: col[2],
market_id: col[3]
})
end
end

def self.find(id)
all.find{|vendor| vendor.id == id}
end

def market
#returns the FarMar::Market instance that is associated
#with this vendor using the FarMar::Vendor market_id field
FarMar::Market.all.find do |market|
@market_id == market.id
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 |sale|
sale.vendor_id == @id
end
end

# def revenue
# total_revenue = 0
# sales.each do |sale|
# total_revenue += sale.amount
# end
# return total_revenue
# end

def revenue(date=nil)
if date == nil
sales.reduce(0) {|sum, sale| sum + sale.amount}
else
date = DateTime.parse(date)
total_revenue = 0
sales.each do |sale|
if sale.purchase_time.to_date == date
total_revenue += sale.amount
end
end
return total_revenue
end
end

def self.by_market(market_id)
#returns all of the vendors with the given market id
all.find_all{|vendor| vendor.market_id == market_id}
end
end
end
85 changes: 84 additions & 1 deletion spec/far_mar/market_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,24 @@

describe FarMar::Market do
before :each do
@market = FarMar::Market.new
@market = FarMar::Market.new({
id: "1",
name: "People's Co-op Farmers Market",
address: "30th and Burnside",
city: "Portland",
county: "Multnomah",
state: "Oregon",
zip: "97202"
})
@market2 = FarMar::Market.new({
id: "2",
name: "Silverdale Farmers Market",
address: "98383",
city: "Silverdale",
county: "Kitsap",
state: "Washington",
zip: "98383"
})
end

describe ".new" do
Expand All @@ -11,4 +28,70 @@
end
end

describe "#all" do
all_markets = FarMar::Market.all
it "returns a collection of all market instances in the csv" do
#test that its an array
expect(all_markets.class).to eq Array
#test that some are instances of Market
expect(all_markets[0]).to be_an_instance_of FarMar::Market
expect(all_markets[-1]).to be_an_instance_of FarMar::Market
#test length of array
csv = CSV.read("support/markets.csv")
expect(all_markets.length).to eq csv.length
end
end

describe "#find" do
it "returns the instance of Market matching the input 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 a collection of vendor instances associated with the specific market" do
expect(@market.vendors).to be_an Array
expect(@market.vendors.length).to eq 6
end
end

describe ".products" do
it "returns a collection of product instances associated with the market" do
expect(@market.products).to be_an Array
expect(@market.products.length).to eq 13
end
end

describe "#search(search_term)" do
it "returns a collection of Market instances where the market or vendor name contain the search term" do
expect(FarMar::Market.search("school")).to be_an Array
expect(FarMar::Market.search("school").length).to eq 3
expect(FarMar::Market.search("schmitt").length).to eq 15
end
end

describe ".preferred_vendor" do
it "returns the vendor with the highest revenue, if no date given" do
expect(@market2.preferred_vendor).to be_an_instance_of FarMar::Vendor
expect(@market2.preferred_vendor.id).to eq 8
end
it "returns the vendor with the highest revenue for the given date, when given a date" do
expect(@market2.preferred_vendor("2013-11-07")).to be_an_instance_of FarMar::Vendor
expect(@market2.preferred_vendor("2013-11-07").id).to eq 7
end
end

describe ".worst_vendor" do
it "returns the vendor with the lowest revenue, if no date given" do
expect(@market2.worst_vendor).to be_an_instance_of FarMar::Vendor
expect(@market2.worst_vendor.id).to eq 9
end
it "returns the vendor revenue for the given date, when given date" do
expect(@market2.worst_vendor("2013-11-07")).to be_an_instance_of FarMar::Vendor
expect(@market2.worst_vendor("2013-11-07").id).to eq 9
end
end


end
Loading