Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
ee4868f
Added all files
dri19tcc Mar 14, 2016
e71c28b
Changed file name from vendor to vendors. Added require 'CSV' to file
dri19tcc Mar 14, 2016
791746b
Shaved some more yaks
dri19tcc Mar 14, 2016
f871c51
Added self methods in all files. Added tests for all the self method…
dri19tcc Mar 15, 2016
c85d59d
Started next method in vendor.rb
dri19tcc Mar 15, 2016
b19552b
updated product.rb to have a self method to find products by vendor_i…
dri19tcc Mar 15, 2016
e9b71fa
Added a self.by_vendor method in sale.rb to find sales by a specific …
dri19tcc Mar 15, 2016
700ec0d
Market.rb-Added comments for understanding. Product.rb-Added sales an…
dri19tcc Mar 16, 2016
03d630a
Added number of sales tests and methods. Both worked
dri19tcc Mar 16, 2016
b054fa4
Added a product method and test to check that an instance was returned
dri19tcc Mar 16, 2016
3a70711
Started time between sales method and started test for it
dri19tcc Mar 16, 2016
adb64a2
Added a sales between time method and test
dri19tcc Mar 17, 2016
826465b
Formatted, got rid of whitespace
dri19tcc Mar 17, 2016
596ebe7
updated all vendor_specs where it made sense, with let. Did same thin…
dri19tcc Mar 17, 2016
9f95d6e
Added let in all my tests, moved all of them to the top so that was t…
dri19tcc Mar 18, 2016
d7aaac8
In all specs, got rid of unnecessary white space. In the lib files, …
dri19tcc Mar 18, 2016
2b53421
Added the detect enumerable in the self.find method. This works bett…
dri19tcc Mar 18, 2016
032d6a0
Added detect into each of the self.find methods
dri19tcc Mar 18, 2016
69b0970
Added the find_all enumerable in the self.by_* in all of the files. …
dri19tcc Mar 18, 2016
08c04d0
Tried extras, tidied up
dri19tcc Mar 18, 2016
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
1 change: 1 addition & 0 deletions .ruby-gemset
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
farmar
1 change: 1 addition & 0 deletions .ruby-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2.3.0
9 changes: 9 additions & 0 deletions Rakefile
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['spec/*_spec.rb']
end

task default: :test
10 changes: 10 additions & 0 deletions far_mar.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require 'CSV'

module FarMar

end

require_relative './lib/market'
require_relative './lib/sale'
require_relative './lib/vendor'
require_relative './lib/product'
46 changes: 46 additions & 0 deletions lib/market.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
class FarMar::Market

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

def initialize(info={})
@id = info[:id].to_i
@name = info[:name]
@address = info[:address]
@city = info[:city]
@county = info[:county]
@state = info[:state]
@zip = info[:zip].to_i
end

def self.all(file = "./support/markets.csv")
# market_array = []
market = CSV.read(file)
market.map do |line|
hash = {
:id => line[0],
:name => line[1],
:address => line[2],
:city => line[3],
:county => line[4],
:state => line[5],
:zip => line[6]
}
# market_array <<
FarMar::Market.new(hash)
end
# return market_array
end

def self.find(id)
markets = self.all
markets.detect do |found_market|
return found_market if id == found_market.id
end
return nil
end

def vendors
market_id = @id#self.id
FarMar::Vendor.by_market(market_id)
end
end
58 changes: 58 additions & 0 deletions lib/product.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
class FarMar::Product

attr_reader :id, :name, :vendor_id

def initialize(info={})
@id = info[:id].to_i
@name = info[:name]
@vendor_id = info[:vendor_id].to_i
end

def self.all(file = "./support/products.csv")
# product_array = []
product = CSV.read(file)
product.map do |line|
hash = {
:id => line[0],
:name => line[1],
:vendor_id => line[2]
}
# product_array <<
FarMar::Product.new(hash)
end
# return product_array
end

def self.find(id)
products = self.all
products.detect do |found_product|
return found_product if id == found_product.id
end
return nil
end

def self.by_vendor(vendor_id)
# prod = []
products = self.all
products.find_all do |found_product|
# prod <<
found_product if vendor_id == found_product.vendor_id
end
# return prod
end

def vendor
vendor = self.vendor_id
return FarMar::Vendor.find(vendor)
end

def sales
product_id = @id
return FarMar::Sale.by_product(product_id)
end

def number_of_sales
count = self.sales
return count.length
end
end
85 changes: 85 additions & 0 deletions lib/sale.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
class FarMar::Sale

attr_reader :id, :amount, :purchase_time, :vendor_id, :product_id

def initialize(info={})
@id = info[:id].to_i
@amount = info[:amount].to_f
@purchase_time = info[:purchase_time]
@vendor_id = info[:vendor_id].to_i
@product_id = info[:product_id].to_i
end

def self.all(file = "./support/sales.csv")
# sale_array = []
sale = CSV.read(file)
sale.map do |line|
hash = {
:id => line[0],
:amount => line[1],
:purchase_time => line[2],
:vendor_id => line[3],
:product_id => line[4]
}
# sale_array <<
FarMar::Sale.new(hash)
end
# return sale_array
end

def self.find(id)
sales = self.all
sales.detect do |found_sale|
return found_sale if id == found_sale.id
end
return nil
end

def self.by_vendor(vendor_id)
# sale = []
sales = self.all
sales.find_all do |found_sale|
# sale <<
found_sale if vendor_id == found_sale.vendor_id
end
# return sale
end

def self.by_product(product_id)
# sales = []
sale = self.all
sale.find_all do |found_sales|
# sales <<
found_sales if product_id == found_sales.product_id
end
# return sales
end

def vendor
vendor = self.vendor_id
return FarMar::Vendor.find(vendor)
end

def product
product = self.product_id
return FarMar::Product.find(product)
end

def self.between(beginning_time, end_time)
# sold = []
# self.all.each do |sale|
# time = DateTime.parse(sale.purchase_time)
# if time > beginning_time && time < end_time
# sold << sale
# end
# end
# return sold
self.all.find_all do |sale|
DateTime.parse(sale.purchase_time) > beginning_time && DateTime.parse(sale.purchase_time) < end_time
end
end




end
68 changes: 68 additions & 0 deletions lib/vendor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
class FarMar::Vendor

attr_reader :id, :name, :no_of_employees, :market_id

def initialize(info={})
@id = info[:id].to_i
@name = info[:name]
@no_of_employees = info[:no_of_employees].to_i
@market_id = info[:market_id].to_i
end

def self.all(file = "./support/vendors.csv")
# vendor_array = []
vendor = CSV.read(file)
vendor.map do |line|
hash = {
:id => line[0],
:name => line[1],
:no_of_employees => line[2],
:market_id => line[3]
}
# vendor_array <<
FarMar::Vendor.new(hash)
end
# return vendor_array
end

def self.find(id)
vendors = self.all
vendors.detect do |found_vendor|
return found_vendor if id == found_vendor.id
end
return nil
end

def self.by_market(market_id)
# vend = []
vendors = self.all
vendors.find_all do |found_vendor| found_vendor if market_id == found_vendor.market_id
end
end

def market
market_id = @market_id#self.market_id
return FarMar::Market.find(market_id)
#--find market by vendor_id--
end

def products
vendor_id = @id#self.id
FarMar::Product.by_vendor(vendor_id)
end

def sales
vendor_id = @id#self.id
return FarMar::Sale.by_vendor(vendor_id)
end

def revenue
sales = self.sales #self is the instance this was called with
total_amount = sales.inject(0) { |sum, sale|
sum + sale.amount
}
return total_amount
#array.inject(0){|sum,x| sum + x }
end

end
49 changes: 49 additions & 0 deletions spec/market_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
require_relative './spec_helper'
require_relative '../lib/market'

describe FarMar::Market do
let(:all_markets) { FarMar::Market.all}
let(:market) { FarMar::Market.find(1) }

it "Market Class exist?" do
FarMar::Market.wont_be_nil
end

describe "#self.all(file)" do
# let(:all_markets) { FarMar::Market.all}
it "Return an array?" do
all_markets.must_be_instance_of Array
end

it "Return an array full of FarMar::Market Instances?" do
classes = all_markets.map { |m| m.class}
classes.uniq.must_equal [FarMar::Market]
end
end

describe "#self.find(id)" do
# let(:market) { FarMar::Market.find(1) }
it "Know basic data about market?" do
market.must_be_instance_of FarMar::Market
market.id.must_equal(1)
market.name.must_equal("People's Co-op Farmers Market")
market.address.must_equal("30th and Burnside")
market.city.must_equal("Portland")
market.county.must_equal("Multnomah")
market.state.must_equal("Oregon")
market.zip.must_equal(97202)
end

it "Returns nil if market is not found?" do
FarMar::Market.find(10_000).must_equal(nil)
end
end

describe "#vendors" do
# let(:market) { FarMar::Market.find(1) }
it "Returns an array of vendor objects that match a specific market_id" do
vendors = market.vendors
vendors.length.must_equal(6)
end
end
end
Loading