Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
9 changes: 9 additions & 0 deletions Rakefile.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require 'rake/testtask'

Rake::TestTask.new do |t|
t.libs = ["lib"]
t.warning = true
t.test_files = FileList['specs/*spec.rb']
end

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

module FarMar

require './lib/farmar_market'
require './lib/farmar_product'
require './lib/farmar_sale'
require './lib/farmar_vendor'

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

class FarMar::Market

attr_reader :market_id, :name, :address, :city, :county, :state, :zip

def initialize(market_hash)
@market_id = market_hash[:market_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

# self.all: returns a collection of instances, representing all of the objects described in the CSV

def self.all
csv_market_array = []
CSV.open("./support/markets.csv", 'r') do |csv|
csv.read.each do |row|
csv_market_array << FarMar::Market.new(market_id: row[0].to_i, name: row[1].to_s, address: row[2].to_s, city: row[3].to_s, county: row[4].to_s, state: row[5].to_s, zip: row[6].to_i)
end
return csv_market_array
end
end

# self.find(id): returns an instance of the object where the value of the id field in the CSV matches the passed parameter.

def self.find(id)
found = self.all.find_all { |market| market.market_id == id}
return found[0]
end

#vendors: returns a collection of FarMar::Vendor instances that are associated with the market by the market_id field.

def vendors
FarMar::Vendor.all.find_all { |vendor| vendor.market_id == @market_id }
end
end
54 changes: 54 additions & 0 deletions lib/farmar_product.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@

class FarMar::Product
attr_reader :product_id, :product_name, :vendor_id

def initialize(product_hash)
@product_id = product_hash[:product_id]
@product_name = product_hash[:product_name]
@vendor_id = product_hash[:vendor_id]
end

# self.all: returns a collection of instances, representing all of the objects described in the CSV

def self.all
csv_products_array = []
CSV.open("./support/products.csv", 'r') do |csv|
csv.read.each do |row|
csv_products_array << FarMar::Product.new(product_id: row[0].to_i, product_name: row[1].to_s, vendor_id: row[2].to_i)
end
return csv_products_array
end
end

# self.find(id): returns an instance of the object where the value of the id field in the CSV matches the passed parameter.

def self.find(id)
found = self.all.find_all { |product| product.product_id == id}
return found[0]
end

# self.by_vendor(vendor_id): returns all of the products with the given vendor_id

def self.by_vendor(vendor_id = @vendor_id)
self.all.find_all { |product| product.vendor_id == vendor_id }
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
FarMar::Sale.all.find_all { |sale| sale.product_id == @product_id}
end

#number_of_sales: returns the number of times this product has been sold.

def number_of_sales
number = FarMar::Sale.all.find_all { |sale| sale.product_id == @product_id}
return number.length
end
end
52 changes: 52 additions & 0 deletions lib/farmar_sale.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@

class FarMar::Sale

attr_reader :sale_id, :amount, :purchase_time, :vendor_id, :product_id

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

# self.all: returns a collection of instances, representing all of the objects described in the CSV

def self.all
csv_sale_array = []
CSV.open("./support/sales.csv", 'r') do |csv|
csv.read.each do |row|
csv_sale_array << FarMar::Sale.new(sale_id: row[0].to_i, amount: row[1].to_i, purchase_time: DateTime.parse(row[2]), vendor_id: row[3].to_i, product_id: row[4].to_i)
end
return csv_sale_array
end
end

# self.find(id): returns an instance of the object where the value of the id field in the CSV matches the passed parameter.

def self.find(id)
found = self.all.find_all { |sale_item| sale_item.sale_id == id}
return found[0]
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::Sale product_id field

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

# self.between(beginning_time, end_time): returns a collection of FarMar::Sale objects where the purchase time is between the two times given as arguments

def self.between(beginning_time, end_time)
starting = DateTime.parse(beginning_time)
ending = DateTime.parse(end_time)
self.all.find_all { |sale| sale.purchase_time.between? starting, ending }
end
end
67 changes: 67 additions & 0 deletions lib/farmar_vendor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@

class FarMar::Vendor

attr_reader :vendor_id, :vendor_name, :num_of_employees, :market_id

def initialize(vendor_hash)
@vendor_id = vendor_hash[:vendor_id]
@vendor_name = vendor_hash[:vendor_name]
@num_of_employees = vendor_hash[:num_of_employees]
@market_id = vendor_hash[:market_id]
end

# self.all: returns a collection of instances, representing all of the objects described in the CSV

def self.all
csv_vendors_array = []
CSV.open("./support/vendors.csv", 'r') do |csv|
csv.read.each do |row|
csv_vendors_array << FarMar::Vendor.new(vendor_id: row[0].to_i, vendor_name: row[1].to_s, num_of_employees: row[2].to_i, market_id: row[3].to_i)
end
return csv_vendors_array
end
end

# self.find(id): returns an instance of the object where the value of the id field in the CSV matches the passed parameter.

def self.find(id)
found = self.all.find_all { |vendor| vendor.vendor_id == id}
return found[0]
end

# self.by_market(market_id): returns all of the vendors with the given market_id

def self.by_market(this_market_id)
self.all.find_all { |vendor| vendor.market_id == this_market_id}
end

#market: returns the FarMar::Market instance that is associated with this vendor using the FarMar::Vendor market_id field

def market(mar_id)
FarMar::Market.find(mar_id)
end

# products: returns a collection of FarMar::Product instances that are associated by the FarMar::Product vendor_id field.

def products(ven_id)
FarMar::Product.all.find_all { |product| product.vendor_id == ven_id }
end

# sales: returns a collection of FarMar::Sale instances that are associated by the vendor_id field.

def sales(ven_id)
FarMar::Sale.all.find_all { |sale_item| sale_item.vendor_id == ven_id }
end

# revenue: returns the the sum of all of the vendor's sales (in cents)

def revenue
vend_id = @vendor_id
found_sale_items = self.sales(vend_id)
total_revenue = 0
found_sale_items.each do |sale|
total_revenue += sale.amount
end
return total_revenue
end
end
46 changes: 46 additions & 0 deletions specs/market-spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
require_relative './spec-helper'

describe FarMar::Market do
it "Does it exist?" do
FarMar::Market.wont_be_nil
end
end

describe "Market#all" do
it "return instances of Market class?" do
FarMar::Market.all[12].must_be_instance_of FarMar::Market
end
end

describe "Market#all" do
it "return all instances of Market class?" do
FarMar::Market.all.length.must_equal(500)
# The csv contains 500 markets.
end
end

describe "Market#find(id)" do
it "Does it exist?" do
FarMar::Market.find(1).wont_be_nil
end
end

describe "Market#find(id)" do
it "Does it exist?" do
FarMar::Market.find(20).must_be_instance_of FarMar::Market
end
end

describe "#vendors" do
it "Does it return a collection of vendors?" do
new_market = FarMar::Market.new(market_id: 5)
new_market.vendors.length.must_equal(2)
end
end

describe "#vendors" do
it "Are the items in the collection instances of FarMar::Vendor?" do
new_market = FarMar::Market.new(market_id: 5)
new_market.vendors[0].must_be_instance_of FarMar::Vendor
end
end
60 changes: 60 additions & 0 deletions specs/product-spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
require_relative './spec-helper'

describe FarMar::Product do
it "Does it exist?" do
FarMar::Product.wont_be_nil
end
end

describe "Product#all" do
it "return instances of Product class?" do
FarMar::Product.all.wont_be_nil
end
end

describe "Product#all" do
it "return all instances of Product class?" do
FarMar::Product.all.length.must_equal(8193)
# The csv contains 8193 products.
end
end

describe "Product#find(id)" do
it "Does it exist?" do
FarMar::Product.find(1).wont_be_nil
end
end

describe "Product#find(id)" do
it "Does it return the found product?" do
FarMar::Product.find(20).must_be_instance_of FarMar::Product
end
end

describe "Product#by_vendor" do
it "Does it return the found product?" do
vendor_array = FarMar::Product.by_vendor(20)
vendor_array[0].must_be_instance_of FarMar::Product
end
end

describe "#vendor" do
it "Does it return the product's vendor?" do
new_product = FarMar::Product.new(vendor_id: 1)
new_product.vendor.must_be_instance_of FarMar::Vendor
end
end

describe "#sales" do
it "Does it return the sales of the product?" do
new_product = FarMar::Product.new(product_id: 1)
new_product.sales.must_be_instance_of Array
end
end

describe "#number_of_sales" do
it "Does it return the sales of the product?" do
new_product = FarMar::Product.new(product_id: 1)
new_product.number_of_sales.must_equal(7)
end
end
Loading