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

Hrw/master #31

Open
wants to merge 13 commits into
base: hrw/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
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 "./lib/far_mar/market"
require "./lib/far_mar/product"
require "./lib/far_mar/sale"
Expand Down
52 changes: 51 additions & 1 deletion lib/far_mar/market.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,57 @@
require "pry"
module FarMar

class Market
def initialize
attr_accessor :id, :name, :address, :city, :county, :state, :zip
def initialize(market_hash)
@id = market_hash[:id].to_i
@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

#MARKET_DATA = CSV.read("support/markets.csv")

def self.all
@@markets_all ||= []
if @@markets_all == []
CSV.read("support/markets.csv").each do |id, name, address, city, county, state, zip|
#binding.pry
market_hash = {:id => id.to_i, :name => name, :address => address, :city => city, :county => county, :state => state, :zip => zip}
market = FarMar::Market.new(market_hash)
@@markets_all.push(market)
end
end
return @@markets_all
end

def self.find(id)
Market.all.find do |market|
id == market.id
end
end

def list_vendors
vendor_list = []
FarMar::Vendor.all.each do |vendor|
if id == vendor.market_id
vendor_list.push(vendor)
end
end
vendor_list
end

def products
vendors = FarMar::Vendor.by_market(id)
products = []
vendors.each do |vendor|
goods = vendor.list_products
products.push(goods)
end
products
end
end
end
48 changes: 47 additions & 1 deletion lib/far_mar/product.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,53 @@
module FarMar

class Product
def initialize
attr_accessor :id, :name, :vendor_id
def initialize(product_hash)
@id = product_hash[:id].to_i
@name= product_hash[:name]
@vendor_id= product_hash[:vendor_id].to_i
end

def self.all
@@products_all ||= []
if @@products_all == []
CSV.read("support/products.csv").each do |id, name, vendor_id|
#binding.pry
product_hash = {:id => id.to_i, :name => name, :vendor_id => vendor_id}
product = FarMar::Product.new(product_hash)
@@products_all.push(product)

end
end
return @@products_all
end

def self.find(id)
Product.all.find do |product|
id == product.id
end
end

def self.by_vendor(vendor_id)
FarMar::Vendor.find(vendor_id).list_products
end

def list_vendors
FarMar::Vendor.find(vendor_id)
end

def list_sales
sales_list = []
FarMar::Sale.all.each do |sale|
if id == sale.product_id
sales_list.push(sale)
end
end
sales_list
end

def number_of_sales
list_sales.length
end
end
end
42 changes: 41 additions & 1 deletion lib/far_mar/sale.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,47 @@
module FarMar
class Sale
def initialize
attr_accessor :id, :amount, :purchase_time, :vendor_id, :product_id
def initialize(sale_hash)
@id = sale_hash[:id].to_i
@amount = sale_hash[:amount].to_i
@purchase_time = DateTime.strptime(sale_hash[:purchase_time], "%Y-%m-%d %H:%M:%S %z")
@vendor_id = sale_hash[:vendor_id].to_i
@product_id = sale_hash[:product_id].to_i
end

def self.all
@@sales_all ||= []
if @@sales_all == []
CSV.read("support/sales.csv").each do |id, amount, purchase_time, vendor_id, product_id|
sale_hash = {:id => id, :amount => amount, :purchase_time => purchase_time, :vendor_id => vendor_id, :product_id => product_id}
market = FarMar::Sale.new(sale_hash)
@@sales_all.push(market)
end
end
return @@sales_all
end

def self.find(id)
Sale.all.find do |sale|
id == sale.id
end
end

def self.between?(start_time, stop_time)
start_time = DateTime.strptime(start_time,"%Y-%m-%d %H:%M:%S %z")
stop_time = DateTime.strptime(stop_time,"%Y-%m-%d %H:%M:%S %z")
FarMar::Sale.all.find_all { |sale| sale.purchase_time.between?(start_time, stop_time) }
end

def list_vendors
FarMar::Vendor.find(vendor_id)
end

def list_products
FarMar::Product.find(product_id)
end



end
end
64 changes: 63 additions & 1 deletion lib/far_mar/vendor.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,69 @@
require "pry"
module FarMar

class Vendor
def initialize
attr_accessor :id, :name, :team_size, :market_id
def initialize(vendor_hash)
@id = vendor_hash[:id].to_i
@name = vendor_hash[:name]
@team_size = vendor_hash[:team_size].to_i
@market_id = vendor_hash[:market_id].to_i
end

def self.all
@@vendors_all ||= []
if @@vendors_all == []
CSV.read("support/vendors.csv").each do |id, name, team_size, market_id|
#binding.pry
vendor_hash = {:id => id.to_i, :name => name, :team_size => team_size, :market_id => market_id}
market = FarMar::Vendor.new(vendor_hash)
@@vendors_all.push(market)
end
end
return @@vendors_all
end

def self.find(id)
Vendor.all.find do |vendor|
id == vendor.id
end
end

def self.by_market(market_id)
FarMar::Market.find(market_id).list_vendors
#list = FarMar::Market.list_vendors
end

def list_markets
FarMar::Market.find(market_id)
end

def list_products
product_list = []
FarMar::Product.all.each do |product|
if id == product.vendor_id
product_list.push(product)
end
end
product_list
end

def list_sales
sales_list = []
FarMar::Sale.all.each do |sale|
if id == sale.vendor_id
sales_list.push(sale)
end
end
sales_list
end

def revenue
money = 0
list_sales.each do |sale|
money += sale.amount
end
return money
end
end
end
39 changes: 36 additions & 3 deletions spec/far_mar/market_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,46 @@
describe FarMar do
describe FarMar::Market do
before :each do
@market = FarMar::Market.new
#@market = FarMar::Market.all
end

describe "#initialize" do
it "creates a new instance of Market" do
expect(@market).to be_an_instance_of FarMar::Market
fake_hash = {:id => 666, :name => "Boo Boo", :address => "123 Haunted House Way", :city => "Spooky Estates", :county => "Cook", :state => "IL", :zip => 60616}
test = FarMar::Market.new(fake_hash)
expect(test).to be_an_instance_of FarMar::Market
end
end

describe '#self.all' do
it "returns a collection of Market instances" do
expect(FarMar::Market.all.length).to eq CSV.read("support/markets.csv").length
end
end

describe "self.find(id)" do
it "returns specific instance of market" do
find_test = FarMar::Market.find(15)
expect(find_test.name).to eq "Farmers Market in Denison"
end
end

describe "list_vendors(id)" do
it "returns array of vendors at specific market" do
vendor_list_test = FarMar::Market.find(20)
vendor_list_test.list_vendors.each do |vendor|
expect(vendor.market_id).to eq 20
end
end
end

describe "products" do
it "shows it is returning product instances" do
market = FarMar::Market.find(100)
market.products.each do |goods|
expect(goods.name).to eq "candy"
end
end
end
end
end
end
51 changes: 49 additions & 2 deletions spec/far_mar/product_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,59 @@
describe FarMar do
describe FarMar::Product do
before :each do
@product = FarMar::Product.new
#@product = FarMar::Product.all
end

describe "#initialize" do
it "creates a new instance of Product" do
expect(@product).to be_an_instance_of FarMar::Product
fake_hash = {:id => 111, :name => "Snickers", :vendor_id => 909}
test = FarMar::Product.new(fake_hash)
expect(test).to be_an_instance_of FarMar::Product
end
end

describe '#self.all' do
it "returns a collection of Product instances" do
expect(FarMar::Product.all.length).to eq CSV.read("support/products.csv").length
end
end

describe "self.find(id)" do
it "returns specific instance of product" do
find_test = FarMar::Product.find(15)
expect(find_test.name).to eq "Comfortable Pretzel"
end
end

describe "list_vendors" do
it "returns specific vendor instance" do
vendor_test = FarMar::Product.find(98)
expect(vendor_test.list_vendors.name).to eq "Jakubowski-Robel"
end
end

describe "list_sales" do
it "returns array of sale instances" do
sale_test = FarMar::Product.find(109)
sale_test.list_sales.each do |sale|
expect(sale.product_id).to eq 109
end
end
end

describe "number_of_sales" do
it "sums up total sale instances for specific product" do
number_test = FarMar::Product.find(8)
expect(number_test.number_of_sales).to eq 5
end
end

describe "self.by_vendor" do
it "returns array of products" do
testcase = FarMar::Product.by_vendor(202)
testcase.each do |product|
expect(product.list_vendors.name).to eq "Kerluke LLC"
end
end
end
end
Expand Down
Loading