Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
0672f83
baseline
Mar 14, 2016
41d5c39
created instance variables and a self.all method in each .rb in lib. …
Mar 14, 2016
6cc714b
added the self.find(id) method to all lib files. added a test to each…
Mar 14, 2016
1edcd5e
added #vendors to farmar_market. attempting to add #market to farmar_…
Mar 14, 2016
3268fd1
fixed #market in farmar_vendor with a one liner. old method with mult…
Mar 14, 2016
5aed32b
added #sales to farmar_vendor. everyhing passesssss.
Mar 14, 2016
cc7bab9
added #sales method. all good good.
Mar 14, 2016
2306305
there was a bug in sales... I forgot to change the vendor_id in the s…
Mar 15, 2016
be03c30
added #revenue to farmar_vendor.rb. test passed.
Mar 15, 2016
a1f82d4
added self.by_market method into the faramar_vendor.rb. tested out th…
Mar 15, 2016
3faaf88
decided to rename files in my spec folder cause it was bugging me.. n…
Mar 15, 2016
f1df17d
fixed the #vendor method in the farmar_product.rb
Mar 15, 2016
117e45e
added sales and number_of_sales methods to farmar_product.rb. Check t…
Mar 15, 2016
1f20b7d
cleaned up vendor_spec majorly.. working on others meow.
Mar 15, 2016
f12c6ac
cleaned the duck out of all spec.rb files. added more verification te…
Mar 15, 2016
7aa58b5
added the self.by_vendor method to farmar_products.rb. all tests run …
Mar 15, 2016
30b3b36
added #vendor to farmar_sales. all tests good.
Mar 15, 2016
e08fa3c
added #product to sale.rb. added test. gooooood.
Mar 15, 2016
fda7321
tried to round amounts up to whole integer with .ceil and .round2 BUT…
Mar 15, 2016
4a3b99f
added the self.between method in farmar_sale.rb. fudged with the whol…
Mar 16, 2016
a7b1a91
added #products to farmar_market.rb. tests run.
Mar 16, 2016
78461a0
added self.search method to farmar_market.rb. got frustrated trying t…
Mar 16, 2016
0009421
changed the #products in farmar_market to NOT have subarrays
Mar 16, 2016
6e094f1
trying to figure out a better way to find the preferred_vendor.. ughh…
Mar 17, 2016
90e8866
got preferred_vendor to work but it looks messy and not pretty.. meh.…
Mar 17, 2016
00d5db5
added my own sales method to farmar_market because that makes sense t…
Mar 17, 2016
32d0531
finished all of the optionals. they all run. the code is kinda messy …
Mar 18, 2016
716600d
cleared up some comments
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
8 changes: 8 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
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
13 changes: 13 additions & 0 deletions far_mar.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 all needed classes
require_relative 'lib/farmar_market'
require_relative 'lib/farmar_product'
require_relative 'lib/farmar_sale'
require_relative 'lib/farmar_vendor'
123 changes: 123 additions & 0 deletions lib/farmar_market.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
# lib/farmar_market.rb
class FarMar::Market
attr_reader :id, :name, :address, :city, :county, :state, :zip
def initialize(hash)
@id = hash[:id].to_i
@name = hash[:name]
@address = hash[:address]
@city = hash[:city]
@county = hash[:county]
@state = hash[:state]
@zip = hash[:zip]
end

# creates instances (500) of each row of data in the csv
# and pushes them into an array -- *blank cells are nil
def self.all
markets_info = []
CSV.foreach("support/markets.csv") do |row|
info = self.new(id: row[0], name: row[1], address: row[2],
city: row[3], county: row[4], state: row[5], zip: row[6])
markets_info << info
end
return markets_info
end

# finds all instances of market that match the given id
def self.find(id)
self.all.each do |instance|
return instance if instance.id == id.to_i
end
end

# returns a collection of product instances by referring to the vendor id's
# from #vendors. shovels the products into subarrays within prod array
def products
ven = vendors_id
FarMar::Product.all.collect {|instance| instance if ([instance.vendor_id] & ven).any? }.compact
end

# returns all instances of vendors based on given market_id
def vendors
FarMar::Vendor.all.find_all {|instance| instance.market_id == id}
end

def vendors_id
vendors.collect {|ven| ven.id}
end

# returns a collection of market instance where the market name of vendor name
# contain the search_term
def self.search(search_term)
market = self.all.find_all {|instance| instance.name.downcase.include?(search_term)}
vendor = FarMar::Vendor.all.find_all {|instance| instance.name.downcase.include?(search_term)}
match = market + vendor
return match
end

# returns sales in an array
def sales
ven = vendors_id
FarMar::Sale.all.collect {|instance| instance if ([instance.vendor_id] & ven).any? }.compact
end

def sales_sum_array
sale_sums = []
ven = vendors_id
ven.each do |vendor_instance|
sale_sums << sales.collect {|instance| instance.amount if ([instance.vendor_id] & [vendor_instance]).any? }.compact
end
sale_sums.map {|meo| meo.inject(:+)}
end

def preferred_vendor
vendor_array = vendors
sum = sales_sum_array
max_vendor = sum.max
index_vendor = sum.index(max_vendor)
vendor_array[index_vendor]
end

def worst_vendor
vendor_array = vendors
sum = sales_sum_array
min_vendor = sum.min
index_vendor = sum.index(min_vendor)
vendor_array[index_vendor]
end

# returns vendor with the highest revenue for the given date
def prefered_vendor(date)
chosen_date = Date.parse(date).mjd
total_sales = sales
date_sale = total_sales.find_all do |instance|
DateTime.strptime(instance.purchase_time, %q[%Y-%m-%d %H:%M:%S %z]).mjd == chosen_date
end
sale_sums = []
ven_id = date_sale.collect {|ven| ven.vendor_id}
ven_id.each do |vendor_instance|
sale_sums << sales.collect {|instance| instance.amount if ([instance.vendor_id] & [vendor_instance]).any? }.compact
end
total_amounts = sale_sums.map {|meo| meo.inject(:+)}
max_vendor = total_amounts.max
index_vendor = total_amounts.index(max_vendor)
ven_id[index_vendor] # [40126, 40126, 26866, 61749]
end

def worst_vend(date)
chosen_date = Date.parse(date).mjd
total_sales = sales
date_sale = total_sales.find_all do |instance|
DateTime.strptime(instance.purchase_time, %q[%Y-%m-%d %H:%M:%S %z]).mjd == chosen_date
end
sale_sums = []
ven_id = date_sale.collect {|ven| ven.vendor_id}
ven_id.each do |vendor_instance|
sale_sums << sales.collect {|instance| instance.amount if ([instance.vendor_id] & [vendor_instance]).any? }.compact
end
total_amounts = sale_sums.map {|meo| meo.inject(:+)}
max_vendor = total_amounts.min
index_vendor = total_amounts.index(max_vendor)
ven_id[index_vendor] # [40126, 40126, 26866, 61749]
end
end
49 changes: 49 additions & 0 deletions lib/farmar_product.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# lib/farmar_product.rb
class FarMar::Product
attr_reader :id, :name, :vendor_id
def initialize(hash)
@id = hash[:id].to_i
@name = hash[:name]
@vendor_id = hash[:vendor_id].to_i
end

# creates instances (8193) of each row of data in the csv
# and pushes them into an array -- *blank cells are nil
def self.all
products_info = []
CSV.foreach("support/products.csv") do |row|
info = self.new(id: row[0], name: row[1], vendor_id: row[2])
products_info << info
end
return products_info
end

def self.find(id)
self.all.each do |instance|
if instance.id == id
return instance
end
end
end

# returns the instances that match vendor_id (based on given prod id)
def vendor
FarMar::Vendor.all.find {|instance| instance.id == vendor_id}
end

# returns all of the sales that match the given prod id
def sales
FarMar::Sale.all.find_all {|instance| instance.product_id == id}
end

# returns the num of times this product has been sold
def number_of_sales
sales.length
end

# returns all products associated with the given ven_id
def self.by_vendor(ven_id)
self.all.find_all {|instance| instance.vendor_id == ven_id}
end

end
83 changes: 83 additions & 0 deletions lib/farmar_sale.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# lib/farmar_sale.rb
class FarMar::Sale
attr_reader :id, :amount, :purchase_time, :vendor_id, :product_id
def initialize(hash)
@id = hash[:id].to_i
@amount = hash[:amount].to_i
@purchase_time = hash[:purchase_time]
@vendor_id = hash[:vendor_id].to_i
@product_id = hash[:product_id].to_i
end

# creates instances (12798) of each row of data in the csv
# and pushes them into an array -- *blank cells are nil
def self.all
sales_info = []
CSV.foreach("support/sales.csv") do |row|
info = self.new(id: row[0], amount: row[1], purchase_time: row[2],
vendor_id: row[3], product_id: row[4])
sales_info << info
end
return sales_info
end

# finds all instances of sales that match the given id
def self.find(id)
self.all.each do |instance|
if instance.id == id
return instance
end
end
end

# finds instance of vendor that matches the vendor_id (based on given sale id)
def vendor #there should only be one right?
FarMar::Vendor.all.find {|instance| instance.id == vendor_id}
end

# finds all instances of product that match the given sale id
def product
FarMar::Product.all.find_all {|instance| instance.vendor_id == vendor_id}
end

# returns a collection of sale OBJECTS where the purchase time is between the
# two given times
def self.between(beginning_time, end_time)
beginning_t = DateTime.strptime(beginning_time, %q[%Y-%m-%d %H:%M:%S %z])
end_t = DateTime.strptime(end_time, %q[%Y-%m-%d %H:%M:%S %z])
all_times = []
CSV.foreach("support/sales.csv") do |row|
market_time = DateTime.strptime(row[2], %q[%Y-%m-%d %H:%M:%S %z])
if (end_t-market_time) < (end_t-beginning_t) && (end_t-market_time) >= 0
all_times << row
end
end
return all_times
end

# create each loop


# sale.purchase_time <= end_time && sale.purchase_time >= beginning_t






# messy date method
# start = DateTime.strptime('2000-01-01 00:00:00 -0800', %q[%Y-%m-%d %H:%M:%S %z])
# beginning_t = DateTime.strptime(beginning_time, %q[%Y-%m-%d %H:%M:%S %z]).mjd
# end_t = DateTime.strptime(end_time, %q[%Y-%m-%d %H:%M:%S %z]).mjd
# b = (beginning_t - start).to_f
# e = (end_t - start).to_f
# all_times = []
# CSV.foreach("support/sales.csv") do |row|
# market_time = DateTime.strptime(row[2], %q[%Y-%m-%d %H:%M:%S %z])
# m = (market_time - start).to_f
# if m > b && m < e
# all_times << row
# end
# end
# return all_times
end
52 changes: 52 additions & 0 deletions lib/farmar_vendor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# lib/farmar_vendor.rb
class FarMar::Vendor
attr_reader :id, :name, :employees, :market_id
def initialize(hash)
@id = hash[:id].to_i
@name = hash[:name]
@employees = hash[:employees].to_i
@market_id = hash[:market_id].to_i
end

# creates instances (2690) of each row of data in the csv
# and pushes them into an array -- *blank cells are nil
def self.all
vendors_info = []
CSV.foreach("support/vendors.csv") do |row|
info = self.new(id: row[0], name: row[1], employees: row[2],
market_id: row[3])
vendors_info << info
end
return vendors_info
end

def self.find(id)
self.all.each do |instance|
if instance.id == id
return instance
end
end
end

def market
FarMar::Market.all.find {|instance| instance.id == market_id}
end

def products
FarMar::Product.all.find_all {|instance| instance.vendor_id == id}
end

def sales
FarMar::Sale.all.find_all {|instance| instance.vendor_id == id}
end

def revenue
amounts = sales.map {|instance| instance.amount}
amounts.inject(:+)
# sprintf("%0.02f", amounts.inject(:+))
end

def self.by_market(mar_id)
self.all.find_all {|instance| instance.market_id == mar_id}
end
end
Loading