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

Allison Hoke #47

Open
wants to merge 42 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
98b51d4
Completed baseline requirements. Need to do better at committing to g…
allisonhoke Sep 6, 2016
cc6b878
created self.find methods for all classes
allisonhoke Sep 6, 2016
9c83981
created tests for self.find methods for each class. all tests passed.
allisonhoke Sep 6, 2016
9d032a8
added new methods up to Vendor #products adn tests. all tests passing…
allisonhoke Sep 7, 2016
c851394
deleted a space
allisonhoke Sep 7, 2016
7a83814
added vendor_id to attr_reader
allisonhoke Sep 7, 2016
2255bf9
added vendor_id to attr_reader
allisonhoke Sep 7, 2016
199c4c7
added products and sales methods
allisonhoke Sep 7, 2016
1eb7aa1
created tests for products and sales methods. all tests pass
allisonhoke Sep 7, 2016
d6c2d2c
created a revenue method
allisonhoke Sep 7, 2016
aeba3e4
created test for revenue method. WIP, write a better test
allisonhoke Sep 7, 2016
ae74db9
changed #vendors method to call class method from Vendors class
allisonhoke Sep 7, 2016
2f9ed1a
adjusted test for vendors method due to addition of class method in V…
allisonhoke Sep 7, 2016
008feff
added self.by_market(market_id) method
allisonhoke Sep 7, 2016
c86c241
finished test for revenue - all tests pass. created tests for self.by…
allisonhoke Sep 7, 2016
cfc5de5
wrote additional/more specific test for self.by_market method
allisonhoke Sep 7, 2016
1937ef9
deleted unnecessary comments
allisonhoke Sep 7, 2016
a6add83
wrote comment about idea to improve code
allisonhoke Sep 8, 2016
8a941bd
created vendor method
allisonhoke Sep 8, 2016
b2f3931
created tests for vendor method. all tests pass.
allisonhoke Sep 8, 2016
704a6b9
added require 'time'
allisonhoke Sep 8, 2016
8c9cb24
bad git message - cleaned up method code, used .find method
allisonhoke Sep 8, 2016
6794b23
bad commit message - cleans up method code. used .find method where a…
allisonhoke Sep 8, 2016
380d0c9
adjusted tests for cleaned up methods
allisonhoke Sep 8, 2016
65feaf2
adjusted tests for cleaned up methods
allisonhoke Sep 8, 2016
ae0da7b
created vendor method
allisonhoke Sep 8, 2016
2631802
created tests for vendor method. all tests pass
allisonhoke Sep 8, 2016
2726649
added product method and self.between method
allisonhoke Sep 8, 2016
10ddc90
created tests for product method and self.between method. all tests pass
allisonhoke Sep 8, 2016
40445d6
used correct mthod calls on third test for self.between
allisonhoke Sep 8, 2016
7a61d56
changed loop to enumerable in sales method
allisonhoke Sep 8, 2016
e637c3f
changed loop to enumerable in self.by_market method
allisonhoke Sep 8, 2016
9f069d7
changed loop to enumerable in sales method.
allisonhoke Sep 8, 2016
8360f59
changed loop to enumerable in self.by_vendor method
allisonhoke Sep 8, 2016
da9fa1c
changed loop to enumerable in self.between method - is this a good id…
allisonhoke Sep 8, 2016
ddf18cb
adjusted test in vendors method to use before instead of let (this is…
allisonhoke Sep 8, 2016
86ff37a
clean up
allisonhoke Sep 8, 2016
69e45e8
confusion about what must be required
allisonhoke Sep 8, 2016
427575f
minor clean up
allisonhoke Sep 8, 2016
7606a7a
WIP trying to create ArgErr. code in comments needs to be figure out
allisonhoke Sep 8, 2016
3e1ed98
added argument errors on methods that take in arguments.
allisonhoke Sep 9, 2016
f7004d6
Commits got bad at the end. Changed .all methods in all classes to us…
allisonhoke 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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/coverage/
.DS_Store
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
14 changes: 14 additions & 0 deletions far_mar.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
require 'csv'
require_relative './lib/market'
require_relative './lib/product'
require_relative './lib/sale'
require_relative './lib/vendor'

module FarMar
end

a = FarMar::Market.all.sample
puts a.vendors.length
puts a.products
puts a.products[0].vendor_id
puts a.products[2].vendor_id
79 changes: 79 additions & 0 deletions lib/market.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
module FarMar
class Market
attr_reader :id, :name

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.read
markets = [] #array to store all of the hashes with market info
CSV.read("../FarMar/support/markets.csv").each do |line|
market = {
id: line[0].to_i,
name: line[1],
address: line[2],
city: line[3],
county: line[4],
state: line[5],
zip: line[6]
} #create a new hash for each market to store specific info

markets << self.new(market) #creates a new instance with the hash info and puts it in the array to be returned
end
markets #returns this array
end

def self.all #what happens if the csv file is updated - this variable will not include new data
@@all_markets ||= self.read
return @@all_markets
end

def self.find(id)
if FarMar::Market.ids.include?(id)
self.all.find { |m| m.id == id }
else
raise ArgumentError.new("There are no vendors with that id")
end
end

def vendors #returns a collection of FarMar::Vendor instances that are associated with the market
FarMar::Vendor.by_market(@id) #has clause for ArgError in method
end

def self.ids
market_ids = []

self.all.each do |m|
market_ids << m.id
end
return market_ids
end

def products #returns an array of Products that are associated to the market via the Vendor class
all_products = []

this_markets_vendors = FarMar::Vendor.by_market(@id) #returns array of Vendors

this_markets_vendors.each do |v|
vendor_products = FarMar::Product.by_vendor(v.id) #returns array of Products
vendor_products.each do |pro|
all_products << pro
end
end
return all_products
end

#find all vendors associated with this market
#find all products associated with those vendors
#end
end
end
63 changes: 63 additions & 0 deletions lib/product.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
module FarMar
class Product
attr_reader :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.read
products = [] #array to store all of the hashes with product info
CSV.read("../FarMar/support/products.csv").each do |line|
product = {id: line[0].to_i, name: line[1], vendor_id: line[2].to_i} #create a new hash for each product to store specific info

products << self.new(product) #creates a new instance with the hash info and puts it in the array to be returned
end
products #returns this array
end

def self.all #what happens if the csv file is updated - this variable will not include new data
@@all_products ||= self.read
return @@all_products
end

def self.find(id)
if FarMar::Product.ids.include?(id)
self.all.find { |pro| pro.id == id }
else
raise ArgumentError.new("There are no products with that id")
end
end

def vendor #returns the object:Vendor instance that is associated with Product
FarMar::Vendor.find(@vendor_id)
end

def sales #returns an array of Sale objects that are accociated with this product
FarMar::Sale.all.select { |s| s.product_id == @id }
end

def number_of_sales #returns the number of times this product has been sold
sales.length
end

def self.by_vendor(vendor_id) #returns all the products associated with the given vendor_id
if FarMar::Vendor.ids.include?(vendor_id)
self.all.select { |pro| pro.vendor_id == vendor_id }
else
raise ArgumentError.new("There are no vendors with that id")
end
end

def self.ids
product_ids = []

self.all.each do |pro|
product_ids << pro.id
end
return product_ids
end
end
end
58 changes: 58 additions & 0 deletions lib/sale.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
module FarMar
class Sale
attr_reader :id, :amount, :vendor_id, :product_id, :purchase_time

def initialize(sale_hash)
@id = sale_hash[:id]
@amount = sale_hash[:amount]
@purchase_time = sale_hash[:purchase_time]
@vendor_id = sale_hash[:vendor_id]
@product_id = sale_hash[:product_id]
end

def self.read
# IDEA make this an instance variable so you only have to make it run once
sales = [] #array to store all of the hashes with sale info
CSV.read("../FarMar/support/sales.csv").each do |line|
sale = {id: line[0].to_i, amount: line[1].to_i, purchase_time: DateTime.parse(line[2]), vendor_id: line[3].to_i, product_id: line[4].to_i } #create a new hash for each sale to store specific info

sales << self.new(sale) #creates a new instance with the hash info and puts it in the array to be returned
end
sales #returns this array
end

def self.all #what happens if the csv file is updated - this variable will not include new data
@@all_sales ||= self.read
return @@all_sales
end

def self.find(id)
if FarMar::Sale.ids.include?(id)
self.all.find { |s| s.id == id }
else
raise ArgumentError.new("There are no sales with that id")
end
end

def vendor #returns the Vendor instance that is associated with this sale
FarMar::Vendor.find(@vendor_id)
end

def product
FarMar::Product.find(@product_id)
end

def self.between(beginning_time, end_time)
self.all.select { |s| beginning_time <= s.purchase_time && s.purchase_time <= end_time }
end

def self.ids
sale_ids = []

self.all.each do |s|
sale_ids << s.id
end
return sale_ids
end
end
end
82 changes: 82 additions & 0 deletions lib/vendor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
module FarMar
class Vendor
attr_reader :id, :name, :num_employees, :market_id

def initialize(vendor_hash)
@id = vendor_hash[:id]
@name = vendor_hash[:name]
@num_employees = vendor_hash[:num_employees]
@market_id = vendor_hash[:market_id]
end

def self.read #returns an array of objects:Vendors
vendors = [] #array to store all of the hashes with vendor info
CSV.read("../FarMar/support/vendors.csv").each do |line|
vendor = {id: line[0].to_i, name: line[1], num_employees: line[2].to_i, market_id: line[3].to_i} #create a new hash for each vendor to store specific info

vendors << self.new(vendor) #creates a new instance with the hash info and puts it in the array to be returned
end
vendors #returns this array
end

def self.all #what happens if the csv file is updated - this variable will not include new data
@@all_vendors ||= self.read
return @@all_vendors
end

def self.find(id) #returns the object:Vendor with arg. id
if FarMar::Vendor.ids.include?(id)
self.all.find { |v| v.id == id }
else
raise ArgumentError.new("There are no vendors with that id")
end

#OLD CODE WITH .each LOOP
# self.all.each do |v|
# if v.id == id
# return v #returns the object whose id matches the argument
# end
# end
end

def market #returns the object:Market that this vendor belongs to
FarMar::Market.find(@market_id)
end

def products #returns an array of object:Products that belong to this Vendor
FarMar::Product.by_vendor(@id)
end

def sales #returns a collection of object:Sales that are associated with this vendor
FarMar::Sale.all.select { |s| s.vendor_id == @id }
end

def revenue #returns the sum of all the vendor's sales
total = 0
self.sales.each do |s|
total += s.amount
end
return total
end

def self.by_market(market_id) #returns an array of the vendors with the given market_id
# self.all.select { |v| v.market_id == market_id }

if FarMar::Market.ids.include?(market_id)
self.all.select { |v| v.market_id == market_id }
else
raise ArgumentError.new("There are no vendors associated with this market")
end
end

def self.ids
vendor_ids = []

self.all.each do |v|
vendor_ids << v.id
end
return vendor_ids
end

end
end
Loading