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

updated pull request #69

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/.config
coverage/
.DS_Store
13 changes: 13 additions & 0 deletions FarMar.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# gems your project needs
require 'csv'

# our namespace module
module FarMar;
end

# all of our data classes that live in the module

require_relative './lib/mkt'
require_relative './lib/vendor'
require_relative './lib/products'
require_relative './lib/sales'
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'] #this is the set of files that will be tested.
end

task default: :test
37 changes: 37 additions & 0 deletions lib/mkt.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
module FarMar

class Market
@@all_markets = nil
attr_accessor :id, :name, :street, :city, :county, :state, :zip
def initialize(id, name, street, city, county, state, zip)
@id = id
@name= name
@street = street
@city = city
@county = county
@state = state
@zip = zip
@vendors = Vendor.by_market(id)
end

def vendors
return @vendors
end

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

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

end
end
61 changes: 61 additions & 0 deletions lib/products.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
module FarMar

class Product
@@all_products_by_vendor = nil
@@all_products = nil
attr_accessor :product_id, :name, :vendor_id
def initialize(product_id, name, vendor_id)
@product_id = product_id
@name = name
@vendor_id = vendor_id
end

#return Vendor instance that is associated with this vendor using the FarMar::Product vendor_id field
##this is really confusing.
def vendor
return 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
return Sales.by_product(@product_id)
end
#number_of_sales: returns the number of times this product has been sold.
#first of product, last of sales
def number_of_sales
return Sales.by_product(@product_id).size
end

##create all product instances from csv
def self.all
if @@all_products == nil
products = { }
CSV.read('./support/products.csv').each do |line|
products[line[0]] = Product.new(line[0], line[1], line [2])
end
@@all_products = products
end
return @@all_products
end

##find product by its own id
def self.find(product_id)
Product.all[product_id]
end

#self.by_vendor(vendor_id): returns all of the products with the given vendor_id
def self.by_vendor(vendor_id)
if @@all_products_by_vendor == nil
products = { }
Product.all.each do |product_id, current_product|
if products[current_product.product_id] == nil
products[current_product.product_id] = { }
end
products[current_product.vendor_id][current_product.product_id] = current_product
end
@@all_products_by_vendor = products
end
return @@all_products_by_vendor[vendor_id]
end
end
end
81 changes: 81 additions & 0 deletions lib/sales.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
module FarMar

class Sales
@@all_sales_by_vendor = nil
@@all_sales_for_each_product = nil
@@all_sales = nil
attr_accessor :sales_id, :transaction_total, :datetime, :vendor_id, :product_id
def initialize(sales_id, transaction_total, datetime, vendor_id, product_id)
@sales_id = sales_id
@transaction_total = transaction_total.to_i
@datetime = DateTime.parse(datetime)
@vendor_id = vendor_id
@product_id = product_id
end

def vendor
return Vendor.find(@vendor_id)
end

def product
return Product.find(@product_id)
end

##create instances of sales for each element in csv
def self.all
if @@all_sales == nil
sales = { }
CSV.read('./support/sales.csv').each do |line|
sales[line[0]] = Sales.new(line[0],line[1],line[2],line[3],line[4])
end
@@all_sales = sales
end
return @@all_sales
end

def self.find(sales_id)
return Sales.all[sales_id]
end

def self.between(beginning_time, end_time)
b = DateTime.parse(beginning_time)
e = DateTime.parse(end_time)
sales = { }
Sales.all.each do | sales_id, sale |
if b < sale.datetime && e > sale.date_time
sales[line[0]] = Sales.new(line[0],line[1],line[2],line[3],line[4])
end
return sales
end
end


def self.by_product(product_id)
if @@all_sales_for_each_product == nil
sale = { }
Sales.all.each do |sales_id, sales|
if sale[sales.product_id] == nil
sale[sales.product_id] = { }
end
sale[sales.product_id][sales_id] = vendor_id
end
@@all_sales_for_each_product = sale
end
return @@all_sales_for_each_product[product_id]
end

def self.by_vendor(vendor_id)
if @@all_sales_by_vendor == nil
transactions = { }
Sales.all.each do |sales_id, sales|
if transactions[sales.vendor_id] == nil
transactions[sales.vendor_id] = { }
end
transactions[sales.vendor_id][sales_id] = vendor_id
end
@@all_sales_by_vendor = transactions
end
return @@all_sales_by_vendor[vendor_id]
end
end
end
67 changes: 67 additions & 0 deletions lib/vendor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
module FarMar

class Vendor
@@all_vendors_by_market = nil
@@all_vendors = nil
attr_accessor :market_id, :vendor_id, :name, :employees
def initialize(market_id, vendor_id, name, employees)
@market_id = market_id
@vendor_id = vendor_id
@name = name
@employees = employees
end

##returns a collection of FarMar::Product instances that are associated by the FarMar::Product vendor_id field.
def products
return Product.by_vendor(@vendor_id)
end

#sales: returns a collection of FarMar::Sale instances that are associated by the vendor_id field.
def sales
return Sales.by_vendor(@vendor_id)
end

#revenue: returns the the sum of all of the vendor's sales (in cents)
##add for.each method to total all transaction_total(s) in hash
def revenue
total = 0
Sales.by_vendor(@vendor_id).each do | sale_id, sale |
total += sale.transaction_total
end
return total
end

def market
return Market.find(@market_id)
end

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

def self.find(vendor_id)
Vendor.all[vendor_id]
end

def self.by_market(market_id)
if @@all_vendors_by_market == nil
vendors = { }
Vendor.all.each do |vendor_id, vendor|
if vendors[vendor.market_id] == nil
vendors[vendor.market_id] = { }
end
vendors[vendor.market_id][vendor.vendor_id] = vendor
end
@@all_vendors_by_market = vendors
end
return @@all_vendors_by_market[market_id]
end
end
end
66 changes: 66 additions & 0 deletions specs/mkt_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
require_relative 'spec_helper' #get all the stuff we need for testing.

module FarMar
describe Market do
#This allows us to just use a single instance ("magnolia") as a new Market throughout the tests.
describe "#initialize" do

it "1 should create return instances of self" do
ballard = Market.new("85636", "Ballard", "737 Olive Way", "Seattle", "King", "WA", "98101")
magnolia = Market.new("1352", "Magnolia", "3510 W Commodore Way", "Seattle", "King", "WA", "98199")
ballard.must_be_instance_of(Market)
magnolia.must_be_instance_of(Market)
end

it "2 should reflect instance variables" do
magnolia = Market.new("11", "Magnolia", "3510 W Commodore Way", "Seattle", "King", "WA", "98199")
magnolia.id.must_equal "11"
end

it "3 should return instance variables" do
ballard = Market.new("85636", "Ballard", "737 Olive Way", "Seattle", "King", "WA", "98101")
ballard.name.must_equal("Ballard")
end

it "4 should raise an error when entry invalid" do
magnolia = Market.new("1352", "Magnolia", "3510 W Commodore Way", "Seattle", "King", "WA", "98199")
#I want to check that if we pass something that isn't a string in, that it doesn't try to score it.
proc {magnolia.name(21)}.must_raise(ArgumentError)
end

it "5 should raise Argument Errors when needed" do
proc{ Market.new("Magnolia", "3510 W Commodore Way", "Seattle", "King", "WA", "98199") }.must_raise(ArgumentError)
end
end


it "6 should return a hash of objects" do
Market.all.must_be_kind_of(Hash)
end

it "7 should return all objects of self" do
Market.all.length.must_equal 500
end
end

describe "8 Market.find" do

it "9 should return object with self id" do
id = "1"
Market.find(id).name.must_equal("People's Co-op Farmers Market")
end
it "10 should return a hash of Market(id)" do
Market.find("333").must_be_kind_of(FarMar::Market)
end
it "11 should raise error if the id is invalid" do
Market.find(891).must_be_nil
end

describe "12 Vendor" do

it "13 should return a hash of instances of Vendors associated with each market" do
Market.find("333").vendors.must_be_kind_of(Hash)
end
end
end
end
Loading