Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
81cfa04
File structure set up
dschrimm Sep 6, 2016
62eb830
Add all/find methods to market, vendor, and product files. Sale tests…
dschrimm Sep 7, 2016
a82084f
Datetime value retrieved accurately
dschrimm Sep 7, 2016
5b2204b
Working on vendors method, currently broken.
dschrimm Sep 7, 2016
33fa8ec
Working market vendors method
dschrimm Sep 7, 2016
6949b41
Working Vendor market method.
dschrimm Sep 7, 2016
c7b1341
Added working Vendor products method and associated test.
dschrimm Sep 7, 2016
7eb6a99
Edited Market vendors method to return an array of objects instead of…
dschrimm Sep 7, 2016
e97be33
Edited files to allow for raking. Removed superfluous requiring FarMa…
dschrimm Sep 8, 2016
90a576b
Working Vendor sales method and test. Note to self: double check .to_…
dschrimm Sep 8, 2016
3bd7a59
Working vendor Revenue method.
dschrimm Sep 8, 2016
922f36e
Print total revenue
dschrimm Sep 8, 2016
afd69d4
Working Vendor by_market method and test
dschrimm Sep 8, 2016
17a5fe4
Removed test printing
dschrimm Sep 8, 2016
d4a23f2
Working Product vendor method and test
dschrimm Sep 8, 2016
44b7117
Working Product number_of_sales method and test
dschrimm Sep 8, 2016
edd4559
Refactor product specs to reuse Product instances.
dschrimm Sep 8, 2016
f6c3383
Added working Product by_vendor method
dschrimm Sep 8, 2016
179e783
Working Sale vendor method
dschrimm Sep 8, 2016
84009e6
Working Sale product method
dschrimm Sep 8, 2016
f2f8988
Working Sale between method and test
dschrimm Sep 8, 2016
df18ebc
Clean up unused commented tests
dschrimm Sep 8, 2016
a09b6b0
Refactor to reuse created methods in other methods (Vendor market met…
dschrimm Sep 8, 2016
7092ab6
Update to reuse previously created methods (in Product vendor method)
dschrimm Sep 8, 2016
be11cbc
Working Market products method
dschrimm Sep 9, 2016
5089e6b
Working Market search method.
dschrimm Sep 9, 2016
8928d5b
In progress: prefered_vendor. Need to implement as instance method, c…
dschrimm Sep 9, 2016
f955204
Working Market prefered_vendor method
dschrimm Sep 9, 2016
891474e
Refactor to simplify search, products, and vendors methods
dschrimm Sep 9, 2016
f921449
Refactor find, market, products, sales, revenue, by_market methods to…
dschrimm Sep 9, 2016
281a29f
Refactor most product method to simplify.
dschrimm Sep 9, 2016
4d965df
Market prefered_vendor(date) in progress
dschrimm Sep 9, 2016
2758c1d
Edit Market to return all markets in search method, not market/vendor…
dschrimm Sep 9, 2016
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
coverage/
7 changes: 7 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'rake/testtask'

Rake::TestTask.new do |t|
t.test_files = FileList['specs/*_spec.rb']
end

task default: :test
10 changes: 10 additions & 0 deletions far_mar.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require 'csv'

module FarMar
require_relative 'lib/farmar_market'
require_relative 'lib/farmar_vendor'
require_relative 'lib/farmar_product'
require_relative 'lib/farmar_sale'


end
98 changes: 98 additions & 0 deletions lib/farmar_market.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
require 'csv'

class Market

attr_reader :id, :name, :city, :county, :state, :zip

def initialize(id, name, address, city, county, state, zip)
@id = id
@name = name
@city = city
@county = county
@state = state
@zip = zip
end

def self.all
markets = {}
CSV.read('support/markets.csv').each do |line|
id = line[0].to_i
name = line[1]
address = line[2]
city = line[3]
county = line[4]
state = line[5]
zip = line[6]

markets[id] = self.new(id, name, address, city, county, state, zip)
end
return markets
end

def self.find(id)
market = Market.all
return market[id]
end

def vendors
vendor_list = Vendor.all.values
vendor_list.find_all { |n| n.market_id == @id }
end

def products
vendor_list = vendors # Vendors associated with Market instance
all_products = Product.all.values

product_list = []

vendor_list.each do |i|
product_list << all_products.find_all { |n| n.vendor_id == i.id }
end

return product_list.flatten
end

def self.search(search_term)
matches = []
v = Vendor.all.values
m = Market.all.values

# find matching vendor names
vendors = []
vendors << v.find_all { |n| n.name.downcase.include?(search_term) }
# find and add associated market
vendors.flatten.each do |i|
matches << i.market
end

# find and add matching market names
matches << m.find_all { |n| n.name.downcase.include?(search_term)}

return matches.flatten
end

def prefered_vendor
vendors.max_by { |n| n.revenue }
end

# IN PROGRESS:
# def prefered_vendor(date)
# date = DateTime.parse(date)
# vendor_list = vendors
#
# beginning_time = date.to_date
# end_time = beginning_time + 1
#
# sales = Sale.between(beginning_time, end_time)
#
# market_sales = []
#
# sales.each do |i|
# if vendor_list.include?(i.vendor_id)
# market_sales << i
# end
# end
# return market_sales.max_by { |n| n.revenue }
# end

end
52 changes: 52 additions & 0 deletions lib/farmar_product.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
require 'csv'

class Product
attr_reader :id, :name, :vendor_id

def initialize(id, name, vendor_id)
@id = id
@name = name
@vendor_id = vendor_id
end

def self.all
products = {}
CSV.read('support/products.csv').each do |line|
id = line[0].to_i
name = line[1]
vendor_id = line[2].to_i

products[id] = self.new(id, name, vendor_id)

# products[line[0].to_i] = (self.new(id: line[0].to_i, name: line[1], vendor_id: line[2].to_i))
# ^^ I want this to work but I can't seem to find what's going wrong with the process.
end
return products
end

def self.find(id)
return all[id]
end

def vendor
Vendor.find(@vendor_id)
end

def sales
return Sale.all.values.find_all { |n| n.product_id == @id }
end

def number_of_sales
sale_instances = Sale.all.values.find_all { |n| n.product_id == @id }

return sale_instances.length
end

def self.by_vendor(vendor_id)
product_list = []
product_list << all.values.find_all { |n| n.vendor_id == vendor_id }

return product_list.flatten
end

end
50 changes: 50 additions & 0 deletions lib/farmar_sale.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
require 'csv'
require 'date'

class Sale
attr_reader :id, :amount, :purchase_time, :vendor_id, :product_id

def initialize(id, amount, purchase_time, vendor_id, product_id)
@id = id
@amount = amount
@purchase_time = DateTime.parse(purchase_time)
@vendor_id = vendor_id
@product_id = product_id
end

def self.all
sale = {}
CSV.read('support/sales.csv').each do |line|
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

sale[id] = self.new(id, amount, purchase_time, vendor_id, product_id)
end
return sale
end

def self.find(id)
sale = Sale.all
return sale[id]
end

def vendor
Vendor.find(@vendor_id)
end

def product
Product.find(@product_id)
end

def self.between(beginning_time, end_time)
if beginning_time.class != Date
beginning_time = DateTime.parse(beginning_time)
end_time = DateTime.parse(end_time)
end

Sale.all.values.find_all { |n| n.purchase_time.between?(beginning_time, end_time)}
end
end
65 changes: 65 additions & 0 deletions lib/farmar_vendor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
require 'csv'

class Vendor
attr_reader :id, :name, :employees, :market_id

def initialize(id, name, employees, market_id)
@id = id
@name = name
@employees = employees
@market_id = market_id
end

def self.all
vendors = {}
CSV.read('support/vendors.csv').each do |line|
id = line[0].to_i
name = line[1]
employees = line[2].to_i
market_id = line[3].to_i

vendors[id] = self.new(id, name, employees, market_id)
end
return vendors
end

def self.find(id)
return all[id]
end

def market
Market.find(@market_id)
end

def products
product_list = Product.all.values
product_instances = []
product_instances << product_list.find_all { |n| n.vendor_id == @id }

return product_instances.flatten
end

def sales
sales_list = Sale.all.values
sales_instances = []
sales_instances << sales_list.find_all { |n| n.vendor_id == @id }

return sales_instances.flatten
end

def revenue
total_revenue = 0
sales.each do |i|
total_revenue += i.amount
end

return total_revenue
end

def self.by_market(market_id)
vendors_by_market = []
vendors_by_market << all.values.find_all { |n| n.market_id == market_id }

return vendors_by_market.flatten
end
end
65 changes: 65 additions & 0 deletions specs/farmar_market_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
require_relative 'spec_helper'

describe Market do
let (:m) { Market.new(500, "name", "address", "city", "county", "state", "zip") }

describe "#initialize" do
it "should create an instance of Market" do
m = Market.new(123, "name", "address", "city", "county", "state", "zip")
m.must_be_instance_of(Market)
end

it "should know about associated data file" do
m = Market.all
m[500].city.must_equal("Bronx")
end
end

describe "all" do
it "should return a hash" do
m = Market.all
m.class.must_equal(Hash)
end
end

describe "find" do
it "should return an instance of the object" do
m = Market.find(500)
m.must_be_instance_of(Market)
m.id.must_equal(500)
end
end

describe "#vendors" do
it "should return a collection of FarMar::Vendor instances that are associated with the market" do
# m = Market.new(500, "name", "address", "city", "county", "state", "zip")
m.vendors.length.must_equal(10)
end
end

describe "#products" do
it "should return a collection of Product instances that are associated to the market through the Vendor class" do
m.products.length.must_equal(34)
end
end

describe "search" do
it "should returna collection of Market instances where the market or vendor name contain the search item" do
# Market.search('school').length.must_equal(3)
Market.search('collins').length.must_equal(12 + 2) # 12 vendors and 2 markets
end
end

describe "#prefered_vendor" do
it "should return the vendor with the highest revenue" do
m.prefered_vendor.class.must_equal(Vendor)
end
end

# describe "#prefered_vendor(date)" do
# it "should return the vendor with the highest revenue for the given date" do
# m.prefered_vendor(2013-11-11).class.must_equal(Vendor)
# end
# end

end
Loading