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

Lauren (Sky) Herrera #60

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/coverage
8 changes: 8 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

require 'rake/testtask'

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

task default: :test
12 changes: 12 additions & 0 deletions far_mar.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# gems your project needs
require 'csv'

# our namespace module
module FarMar; end

# all of our data classes that live in the module
# ...require all needed classes
require_relative './lib/market'
require_relative './lib/product'
require_relative './lib/sale'
require_relative './lib/vendor'
46 changes: 46 additions & 0 deletions lib/market.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
module FarMar
class Market
attr_reader :id

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

def self.all
all_markets = {}
CSV.read('support/markets.csv').each do |line|
all_markets[line[0].to_i] = self.new(line[0].to_i, line[1], line[2], line[3],line[4], line[5], line[6])
end
return all_markets
end

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

def vendors
FarMar::Vendor.by_market(@id)
end

#products returns a collection of FarMar::Product instances that are associated to the market through theFarMar::Vendor class.
def products
products_by_market = []
#gives me an array of vendors
venders_by_market = FarMar::Vendor.by_market(@id)

venders_by_market.each do | value |
products = FarMar::Product.by_vendor(value.id)
products_by_market.concat(products)
end

return products_by_market
end
end
end
61 changes: 61 additions & 0 deletions lib/product.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
module FarMar
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
all_products = {}
CSV.read('support/products.csv').each do |line|
all_products[line[0].to_i] = self.new(line[0].to_i, line[1], line[2].to_i)
end
return all_products
end

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

#self.by_vendor(vendor_id): returns all of the products with the given vendor_id
def self.by_vendor(ven_id)
products = self.all
products_by_vendor = []

products.each do | product_key, value |
if value.vendor_id == ven_id
products_by_vendor << value
end
end
return products_by_vendor
end

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

#sales: returns a collection of FarMar::Sale instances that are associated using the FarMar::Sale.product_id field.
def sales
sales_by_product_id = []
all_sales = FarMar::Sale.all

all_sales.each do |sale_key, sale_value|
if sale_value.product_id == @id
sales_by_product_id << sale_value
end

end
return sales_by_product_id
end

# number_of_sales: returns the number of times this product has been sold.
def number_of_sales
return sales.length
end
end
end
51 changes: 51 additions & 0 deletions lib/sale.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
module FarMar
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 = purchase_time
@vendor_id = vendor_id
@product_id = product_id
end

def self.all
all_sales = {}
CSV.read('support/sales.csv').each do | line |
all_sales[ line[0].to_i ] = self.new(line[0].to_i, line[1].to_i, line[2], line[3].to_i, line[4].to_i)
end
return all_sales
end

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

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

#product: returns the FarMar::Product instance that is associated with this sale using the FarMar::Saleproduct_id field
def product
FarMar::Product.find(@product_id)
end

# self.between(beginning_time, end_time): returns a collection ofFarMar::Sale objects where the purchase time is between the two timesgiven as arguments
def self.between (start_time, end_time)
all_sales = self.all
sales_between = []
s_time = DateTime.parse(start_time)
e_time = DateTime.parse(end_time)

all_sales.each do | sale_key, sale_values |
if (DateTime.parse(sale_values.purchase_time) >= s_time) && (DateTime.parse(sale_values.purchase_time) < e_time)
sales_between << sale_values
end
end
return sales_between
end
end
end
109 changes: 109 additions & 0 deletions lib/vendor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
module FarMar
class Vendor
attr_reader :id, :name, :num_of_employees, :market_id, :revenue_from_sales

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

def self.all
all_vendors = {}
CSV.read('support/vendors.csv').each do | line |
all_vendors[line[0].to_i] = self.new(line[0].to_i, line[1], line[2].to_i, line[3].to_i)
end
return all_vendors
end

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

def self.by_market(id)
all_vendors = self.all
vendors_by_market = []

#key is vendor_id and the value is the vendor object
all_vendors.each do | vendor_key, value|
if value.market_id == id
vendors_by_market << value
end
end
#returns all of the vendors with the given market_id
return vendors_by_market
end

#market: returns the FarMar::Market instance that is associated with this vendor using the FarMar::Vendor.market_id field
def market
FarMar::Market.find(@market_id)
end

def products
FarMar::Product.by_vendor(@id)
end

#sales: returns a collection of FarMar::Sale instances that are associated by the vendor_id field.
# def sales
# @sales_by_vendor = {}
#
# products.each do | product |
# if product.sales && product.sales.any?
# @sales_by_vendor[product.id] = product.sales
# end
# end
# return @sales_by_vendor
# end
#
# def revenue
# sales
# revenue_by_product = @sales_by_vendor.map do |k, v|
# all_sales = []
# v.each do | sale |
# all_sales << sale.amount
# end
# all_sales.reduce(:+)
# end
# @revenue_from_sales = revenue_by_product.reduce(:+)
# return @revenue_from_sales
# end


def sales
sales_by_vendor = []
@all_sales ||= FarMar::Sale.all
@all_sales.each do | sales_key, sale_value |
if sale_value.vendor_id == @id
sales_by_vendor << sale_value
end
end
return sales_by_vendor
end

def revenue
@revenue_from_sales = 0
@sales_by_vendor ||= sales
@sales_by_vendor.each do | sales_value |
@revenue_from_sales += sales_value.amount
end
return @revenue_from_sales
end

# # should return the top n vendor instances ranked by total revenue
# def self.most_revenue(n)
# all_vendors = self.all
# all_vendors.each do | key, value |
# value.revenue
# end
#
# puts "---------"
# highest = all_vendors.sort_by { | value | value.revenue_from_sales }
# puts highest
#
# # highest.reverse
# # return highest.take(n)
# end
end
end
61 changes: 61 additions & 0 deletions specs/market_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
require_relative 'spec_helper'

describe FarMar::Market do
describe "#initialize" do
greenlake = FarMar::Market.new(123, "greenlake", "123 green", "Green Lake", "Seattle", "WA", "98105")

it "should create a new instance" do
greenlake.must_be_instance_of(FarMar::Market)
end
end

describe "self.all" do
it "should return a hash of instances, representing all of the objects described in the CSV" do
FarMar::Market.all.must_be_kind_of(Hash)
end
end

describe "self.find(id)" do
it "should return an instance of the object where the value of the id field in the CSV matches the passed parameter" do
FarMar::Market.find(1).id.must_equal(1)
end
end

describe "#vendors" do
let(:market_test) {FarMar::Market.find(2)}
it "should return the correct number of FarMar::Vendor instances that are associated with the market instance" do
# market_test = FarMar::Market.find(2)
market_test.vendors.length.must_equal(3)
end

it "should return the correct id of the stated FarMar::Vendor instance that is associated with the market instance" do
# market_test = FarMar::Market.find(2)
market_test.vendors[0].id.must_equal(7)
end

it "should return true if the correct number of FarMar::Vendor instances that are associated with the market instance are returned by the indstance method" do
market_test2 = FarMar::Market.find(1)
market_test2.vendors.length.must_equal(6)
end
end

# products returns a collection of FarMar::Product instances that are associated to the market through theFarMar::Vendor class.
describe "#products" do
let(:product_test) {FarMar::Market.find(2)}
it "should returns a collection of FarMar::Product instances that are associated to the market through theFarMar::Vendor class" do
product_test.products.length.must_equal(9)
end

it "should returns a the correct id of one item in the collection of FarMar::Product instances that are associated to the market through theFarMar::Vendor class" do
puts product_test.products[0].id
product_test.products[0].id.must_equal(14)
end
end

# self.search(search_term) returns a collection of FarMar::Market instances where the market name or vendor name contain the search_term. For example FarMar::Market.search('school') would return 3 results, one being the market with id 75 (Fox School Farmers FarMar::Market).
# describe "self.search(search_term)" do
# it "should returna collection of FarMar::Market instances where the market name or vendor name contain the search_term." do
#
# end
# end
end
Loading