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

Completed primary requirements #40

Open
wants to merge 30 commits into
base: ald/master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
bd1e866
Added attr_readers and methods
Dreedle Oct 19, 2015
978e457
Fixed vendor bug that had been in baseline. Rspecs run now
Dreedle Oct 20, 2015
714d2ae
self.all method for Market.rb passes specs
Dreedle Oct 20, 2015
306bfda
Market.all method was not working, is working now
Dreedle Oct 21, 2015
e74caf9
Market.find(id) method passes specs
Dreedle Oct 21, 2015
7a63723
Market methods take default csv
Dreedle Oct 21, 2015
41d3dc7
Vendor.all and Vendor.find pass specs
Dreedle Oct 21, 2015
fe063d4
Product.all and Product.find passing specs
Dreedle Oct 21, 2015
4ea854d
Sale.all and Sale.find passing specs
Dreedle Oct 21, 2015
1837997
Trying to implement @market.vendors, not finished
Dreedle Oct 21, 2015
e36bf00
#market.vendors method passes specs
Dreedle Oct 22, 2015
2bfc5d8
Improved test for market.vendors
Dreedle Oct 22, 2015
a4130fb
vendor.market method passing specs
Dreedle Oct 22, 2015
8a3fc2e
Corrected market.vendors to take any csv
Dreedle Oct 22, 2015
3f974a2
Improved market_spec for market.vendors
Dreedle Oct 22, 2015
fd9ba6e
improved vendor.market
Dreedle Oct 22, 2015
2fe3124
vendor.products is passing specs
Dreedle Oct 22, 2015
a1c83ea
vendor.sales method passing specs
Dreedle Oct 22, 2015
5b37a87
Used .send in Vendor
Dreedle Oct 22, 2015
9e24036
Added vendor spec for by_market
Dreedle Oct 22, 2015
8d162a3
Product.by_vendor passing specs
Dreedle Oct 22, 2015
16d5621
Product.sales passing specs
Dreedle Oct 22, 2015
8d1e7f9
Repaired sale_spec for non-default find
Dreedle Oct 22, 2015
5c94f5c
sale.product and sale.vendor pass specs
Dreedle Oct 22, 2015
5090ca0
vendor.revenue is passing specs
Dreedle Oct 22, 2015
ad7b278
product.number_of_sales passing specs
Dreedle Oct 23, 2015
5e366ec
Sale.between is passing specs
Dreedle Oct 23, 2015
b797dfb
removed superfluous comments
Dreedle Oct 23, 2015
50177af
Completed primary requirements
Dreedle Oct 23, 2015
44b566a
Removing binding.pry from tests
Dreedle Dec 1, 2015
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 lib/far_mar.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require "csv"
require "time"
require "pry"
require "./lib/far_mar/market"
require "./lib/far_mar/vendor"
require "./lib/far_mar/product"
Expand Down
52 changes: 45 additions & 7 deletions lib/far_mar/market.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,55 @@
module FarMar

class Market
class Market
attr_reader :id, :name, :address, :city, :county, :state, :zip

def self.all
end
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.find(id)
#method for inputting csv file to reutrn an array of market instances
#market_csv is the path to a csv file, and the default is the markets.csv
def self.all(csv = "./support/markets.csv")
market_info = CSV.read(csv)
markets = []
market_info.each do |a|
markets.push(Market.new(a[0], a[1], a[2], a[3], a[4], a[5], a[6]))
end
return markets
end

def vendors
end

#method to return an instance for which the id matches the parameter
#input a csv and an id. By default, the market.csv will be used.

def self.find(search_id, csv = "./support/markets.csv")
all = self.all(csv)
match = (all.find {|n| n.id == search_id.to_s})
return match
end

#~~~~~search foreign keyholder~~~~~~~~~~~#
#returns an array of vendors with the given market_id
#can use default or other csv
def vendors(vendors_csv = "./support/vendors.csv")
all_vendors = Vendor.all(vendors_csv)
vendor_matches = (all_vendors.find_all {|n| n.market_id == self.id})
return vendor_matches
end

# returns a collection of product instances that are associated to the market through the FarMar::Vendor class.
def products(vendors_csv = "./support/vendors.csv", products_csv = "./support/products.csv")
vendors = self.vendors(vendors_csv)
product_matches = []
vendors.each do |n|
product_matches.push(n.products(products_csv))
end
return product_matches
end
end
end
53 changes: 46 additions & 7 deletions lib/far_mar/product.rb
Original file line number Diff line number Diff line change
@@ -1,24 +1,63 @@
module FarMar

class Product
attr_reader :id, :name, :vendor_id

def self.all
def initialize(id = "", name = "", vendor_id = "")
@id = id
@name = name
@vendor_id = vendor_id
end

def self.find(id)
######CLASS METHODS####
def self.all(csv = "./support/products.csv")
products_info = CSV.read(csv)
products = []
products_info.each do |a|
products.push(Product.new(a[0], a[1], a[2]))
end
return products
end

def self.by_vendor(vendor_id)
#helper method for class methods
def self.find_by(var, search_param, csv = "./support/products.csv")
all = self.all(csv)
match = (all.find_all {|n| n.send(var) == search_param.to_s})
return match
end

def vendor
#returns the product with the given id
def self.find(search_id, csv = "./support/products.csv")
self.find_by(:id, search_id, csv)[0]
end

def sales
#returns an array of products with the given vendor_id
def self.by_vendor(search_vendor_id, csv = "./support/vendors.csv")
self.find_by(:vendor_id, search_vendor_id, csv)
end

def number_of_sales
######INSTANCE METHODS####

#~~~~~search primary keyholder~~~~~~~~~~~#
#returns the vendor with an id matching that of a given product instance
def vendor(vendor_csv = "./support/vendors.csv")
all_vendors = Vendor.all(vendor_csv)
vendor_match = (all_vendors.find {|n| n.id == self.vendor_id})
return vendor_match
end

#~~~~~search foreign keyholder~~~~~~~~~~~#
#returns an array of sales with the given product id
def sales(sales_csv = "./support/sales.csv")
all_sales = Sale.all(sales_csv)
matches = (all_sales.find_all {|n| n.product_id == self.id})
return matches
end

end
#~~~~~special fun method~~~~~~~~~~~#
#returns the number of times a product has been sold
def number_of_sales(sales_csv = "./support/sales.csv" )
self.sales(sales_csv).length
end
end
end
63 changes: 50 additions & 13 deletions lib/far_mar/sale.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,60 @@
module FarMar

class Sale

def self.all
end

def self.find(id)
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.between(beginning_time, end_time)
end

def vendor
end

def product
end
######CLASS METHODS####
def self.all(csv = "./support/sales.csv")
sales_info = CSV.read(csv)
sales = []
sales_info.each do |a|
sales.push(Sale.new(a[0], a[1], a[2], a[3], a[4]))
end
return sales
end

def self.find(search_id, csv = "./support/sales.csv")
all = self.all(csv)
match = all.find {|n| n.id == search_id.to_s}
return match
end

#~~~~~special fun method~~~~~~~~~~~#
def self.between(beginning_time, end_time, csv = "./support/sales.csv")

#returns a collection of FarMar::Sale objects where the purchase time is between the two times given as arguments
range = beginning_time..end_time
all = self.all(csv) #array of all sales
in_range = all.find_all {|n| range.cover?(n.purchase_time)}
return in_range
end

######INSTANCE METHODS####
#~~~~~search primary keyholders~~~~~~~~~~~#
#returns the vendor with an id matching that of a given product instance
#helper method to search multiple primary keyholders
def search_primary_keyholders(var, klass, csv)
search_in = klass.all(csv)
match = (search_in.find {|n| n.id == self.send(var)})
return match
end

#returns the vendor with an id matching that of a given sale instance
def vendor(vendor_csv = "./support/vendors.csv")
search_primary_keyholders(:vendor_id, Vendor, vendor_csv)
end

#returns the product with an id matching that of a given sale instance
def product(product_csv = "./support/products.csv")
search_primary_keyholders(:product_id, Product, product_csv)
end
end
end
66 changes: 58 additions & 8 deletions lib/far_mar/vendor.rb
Original file line number Diff line number Diff line change
@@ -1,27 +1,77 @@
module FarMar

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

def self.all
def initialize(id = "", name = "", employees = "", market_id = "")
@id = id
@name = name
@employees = employees
@market_id = market_id
end

def self.find(id)
######CLASS METHODS####
def self.all(csv = "./support/vendors.csv")
vendors_info = CSV.read(csv)
vendors = []
vendors_info.each do |a|
vendors.push(Vendor.new(a[0], a[1], a[2], a[3]))
end
return vendors
end

def self.by_market(market_id)
#helper method for class methods to search vendors
def self.find_by(var, search_param, csv = "./support/vendors.csv")
all = self.all(csv)
match = (all.find_all {|n| n.send(var) == search_param.to_s})
return match
end

def market
#returns the vendor with the given id.
def self.find(search_id, csv = "./support/vendors.csv")
self.find_by(:id, search_id, csv)[0]
end

def products
#returns an array of vendors with the given market_id
def self.by_market(search_market_id, csv = "./support/vendors.csv")
self.find_by(:market_id, search_market_id, csv)
end

def sales

#####INSTANCE METHODS######
#~~~~~search primary keyholder~~~~~~~~~~~#
#returns the market with an id matching that of a given vendor instance
def market(market_csv = "./support/markets.csv" )
all_markets = Market.all(market_csv)
market_match = (all_markets.find {|n| n.id == self.market_id})
return market_match
end

def revenue
#~~~~~search foreign keyholders~~~~~~~~~~~#
# to assist with the .products and .sales methods
def search_foreign_keyholders(csv, klass)
search_in = klass.all(csv)
matches = (search_in.find_all {|n| n.vendor_id == self.id})
return matches
end

end
#returns an Array of product instances that are associated by the FarMar::Product vendor_id field.
def products(products_csv = "./support/products.csv")
search_foreign_keyholders(products_csv, Product)
end

#returns an array of FarMar::Sale instances that are associated by the vendor_id field.
def sales(sales_csv = "./support/sales.csv")
search_foreign_keyholders(sales_csv, Sale)
end

#~~~~~special fun method~~~~~~~~~~~#
#returns the the sum of all of a vendor instance's sales (in cents)
def revenue(sales_csv = "./support/sales.csv")
total = 0
sales = self.sales(sales_csv)
sales.each {|n| total += n.amount.to_i}
return total
end
end
end
86 changes: 83 additions & 3 deletions spec/far_mar/market_spec.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,93 @@
require "spec_helper"

describe FarMar::Market do
before :each do
@market = FarMar::Market.new
end

describe ".new" do
before :each do
@market = FarMar::Market.new
end

it "creates a new instance of a market" do
expect(@market).to be_an_instance_of FarMar::Market
end

it "by default assigns empty strings to instance variables" do
expect(@market.name).to eq ""
expect(@market.address).to eq ""
end

it "accepts parameters passed in" do
@market2 = FarMar::Market.new("3", "Best Market","","","","","45555")
expect(@market2.name).to eq "Best Market"
expect(@market2.zip).to eq "45555"
end
end

describe "self.all" do
context "default csv is passed in" do
it "creates an array of market objects from default csv" do
all = FarMar::Market.all
my_csv = CSV.read("./support/markets.csv")
expect(all.length).to eq my_csv.length
expect(all).to be_an(Array)
expect(all[0].class).to eq FarMar::Market
expect(all[11].zip).to eq "12051"
end
end

context "non-default csv is passed in" do
it "creates an array of market objects from default csv" do
all = FarMar::Market.all("./support/markets2.csv")
my_csv = CSV.read("./support/markets2.csv")
expect(all.length).to eq my_csv.length
expect(all).to be_an_instance_of Array
expect(all[0].class).to eq FarMar::Market
expect(all[9].state).to eq "New Dork"
end
end
end

describe "self.find" do
context "default csv is passed in" do
it "returns an instance of a market matching the passed id" do
id4 = FarMar::Market.find(4)
id370 = FarMar::Market.find(370)
expect(id4.county).to eq "New London"
expect(id4.class).to eq FarMar::Market
expect(id370.state).to eq "Illinois"
end
end

context "non-default csv is passed in" do
it "returns an instance of a market matching the passed id" do
id4 = FarMar::Market.find(4, "./support/markets2.csv")
expect(id4.county).to eq "New Fundon"
expect(id4.class).to eq FarMar::Market
end
end
end

#as smaller csv named vendors2.csv proved acceptable in previous tests, use it for this test as well, for the sake of decreasing test time
describe "#vendors" do
it "returns an array of vendor instances with the associated market id" do
@market3 = FarMar::Market.new("3", "Best Market","","","","","45555")
vendor_matches = @market3.vendors("./support/vendors2.csv")
expect(vendor_matches).to be_an(Array)
expect(vendor_matches[0].market_id).to eq "3"
expect(vendor_matches[0].name).to eq "Fakey Fake Vendor"
end
end

describe "#products" do
it "returns an array of products associated with the market through vendor" do
@market3 = FarMar::Market.new("3", "Best Market","","","","","45555")
product_matches = @market3.products("./support/vendors2.csv", "./support/products2.csv")
expect(product_matches).to be_an(Array)
expect(product_matches.length).to eq 3
expect(product_matches[1].name).to eq "Sticky Mango"
#fakey fake, new guy- 9
#and 10. length 3
#last 3 items in products
end
end
end
Loading