Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
e00cf71
please don't watch this stuff
Lisa-Sano Mar 14, 2016
d4b8322
new class files for Market, Product, Sale, and Vendor
Lisa-Sano Mar 14, 2016
2618ef9
new spec files for the classes (Market, Product, Sale, Vendor). New …
Lisa-Sano Mar 14, 2016
45d5e49
new Rakefile
Lisa-Sano Mar 14, 2016
6f389a3
new module FarMar
Lisa-Sano Mar 14, 2016
736acc7
ruby gemset and version
Lisa-Sano Mar 14, 2016
aba3ca5
class files in lib/ contain empty classes
Lisa-Sano Mar 14, 2016
2a850d4
added a single test to each spec file
Lisa-Sano Mar 14, 2016
76b02c9
moved required far_mar.rb file from each individual spec file to the …
Lisa-Sano Mar 14, 2016
06dc32b
added a new module mixin called FarMarMethods. It contains a self.mak…
Lisa-Sano Mar 14, 2016
40ce874
added self.find method to far_mar_methods module and added tests.
Lisa-Sano Mar 14, 2016
c35d7c2
added initialize method to Vendor class
Lisa-Sano Mar 14, 2016
0cffd6d
added initialize method to the Product class
Lisa-Sano Mar 14, 2016
d1ddfd8
added initialize method to the Sale class
Lisa-Sano Mar 14, 2016
b67bfde
Fixed mixin module and 'extend FarMar::FarMarMethods' usage.
Lisa-Sano Mar 15, 2016
87469e2
use self in mixin module FarMarMethods
Lisa-Sano Mar 15, 2016
85db122
added file as an argument with a default path to the markets.csv file…
Lisa-Sano Mar 15, 2016
2f562df
self.all and self.find methods working on FarMar::Vendor. added tests…
Lisa-Sano Mar 15, 2016
d0df356
added let() to market spec file.
Lisa-Sano Mar 15, 2016
51ff264
able to call self.all and self.find on FarMar::Product class. added t…
Lisa-Sano Mar 15, 2016
bcc236f
added comments to the FarMarMethods module
Lisa-Sano Mar 15, 2016
1cee1ad
added self.all and self.find (module mixin) methods to the FarMar::Sa…
Lisa-Sano Mar 15, 2016
6dfcb48
added #vendor to FarMar::Market which returns an array of vendors tha…
Lisa-Sano Mar 15, 2016
2379fc2
added #market and #products methods to the vendor class. added attr_r…
Lisa-Sano Mar 15, 2016
f608cd9
updated #vendor in Market class and added self.by_market method in Ve…
Lisa-Sano Mar 15, 2016
eac6659
updated #products in Vendor and self.by_vendor method in Product
Lisa-Sano Mar 15, 2016
f69d33b
Added #sales to Vendor class. Added self.by_vendor and self.by_produc…
Lisa-Sano Mar 15, 2016
d4c53fb
added specs to test FarMar::Vendor#sales
Lisa-Sano Mar 15, 2016
a7408de
added #revenue to FarMar::Vendor class. returns a sum of all sales in…
Lisa-Sano Mar 15, 2016
4667c0d
added #vendor to FarMar::Product class. added spec to test.
Lisa-Sano Mar 15, 2016
1d1cacf
added #sales to FarMar::Product class. Added spec to test it
Lisa-Sano Mar 15, 2016
1fb05f3
added number_of_sales method to FarMar::Product. added specs to test
Lisa-Sano Mar 15, 2016
0ef8c3e
added #vendor and #product to FarMar::Sale class. each returns a spec…
Lisa-Sano Mar 15, 2016
1415aa4
added more general class method self.get_by(type, id) in FarMar::Sale…
Lisa-Sano Mar 15, 2016
7882ac9
Added self.between method to FarMar::Sale class. returns a collection…
Lisa-Sano Mar 15, 2016
0d34a7b
Added a #products method to FarMar::Market that returns a collection …
Lisa-Sano Mar 15, 2016
c635f5d
tiny refactor (from .collect to .each on #products
Lisa-Sano Mar 15, 2016
b882af9
added a self.search(search_term) method to FarMar::Market. returns li…
Lisa-Sano Mar 15, 2016
4531ee4
added #preferred_vendor which returns the vendor object who sold the …
Lisa-Sano Mar 16, 2016
e359fb6
added method comments to clarify whether each returns a single instan…
Lisa-Sano Mar 16, 2016
f6254dc
added 'time' requirement. tests pass without, but it doesn't work in …
Lisa-Sano Mar 16, 2016
29db74d
added #worst_vendor method which returns the vendor with the lowest s…
Lisa-Sano Mar 16, 2016
29acb9b
changed Time to DateTime
Lisa-Sano Mar 17, 2016
b1c603e
added a method self.sales_by_date to FarMar::Vendor that returns an a…
Lisa-Sano Mar 17, 2016
275df7c
git added self.revenue(date) to FarMar::Vendor which returns the reve…
Lisa-Sano Mar 17, 2016
dbfbb6c
added argument date to #revenue method in FarMar::Vendor. date is opt…
Lisa-Sano Mar 18, 2016
dee5232
added date option to #worst_vendor. changed it so that only vendors t…
Lisa-Sano Mar 18, 2016
4c1715d
refactored FarMar::FarMarMethods#make_all method to use enumerable
Lisa-Sano Mar 18, 2016
4ecce5c
refactor of #products in FarMar::Market to use #flatten
Lisa-Sano Mar 18, 2016
81c6257
added comments to methods
Lisa-Sano Mar 18, 2016
c9a6cdc
added a self.find_by_name method to FarMar::Market class. added specs…
Lisa-Sano 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['specs/*_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'
require 'date'

module FarMar; end

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

def make_all(file, key_array)
# create an array of instances by using map enumerable
CSV.open(file, 'r').map do |line|
instance_hash = {}

# assigns a value to each key in the hash
key_array.each_with_index do |key, index|
instance_hash[key] = line[index]
end

self.new(instance_hash)
end
end

def find(id)
# iterates through each instance in the collection
self.all.each do |instance|
return instance if id == instance.id
end

# if an instance matching the given id isn't found, return nil
nil
end
end
82 changes: 82 additions & 0 deletions lib/market.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
class FarMar::Market
attr_reader :id, :name

# module mixin
extend FarMar::FarMarMethods

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

def self.all(file_name = './support/markets.csv')
market_keys = [:id, :name, :address, :city, :county, :state, :zip]

# use mixin module FarMar::FarMarMethods#make_all
make_all(file_name, market_keys)
end

def self.search(search_term)
search_term.downcase!

# get all markets to pass to name matching method
all_markets = self.all
market_list = self.match_name(search_term, all_markets)

# get all vendors to pass to name matching method
all_vendors = FarMar::Vendor.all

# returned from match_name is an array of vendor instances
vendors = self.match_name(search_term, all_vendors)

# append the associated market instances (linked through vendor's market_id)
market_list.push *vendors.collect { |vendor| self.find(vendor.market_id) }
end

def self.match_name(search_term, array)
array.find_all do |item|
item.name.downcase.include? search_term
end
end

def self.find_by_name(match)
match = match.downcase
self.all.find do |market|
market.name.downcase == match
end
end

# returns a collection of vendor instances related to market through market_id
def vendors
FarMar::Vendor.by_market(id)
end

# returns a collection of product instances associated to market through the vendor.
def products
products = vendors.map do |vendor|
FarMar::Product.by_vendor(vendor.id)
end

# make nested arrays into a single array using array method #flatten
products.flatten
end

# returns the vendor with the highest revenue (either total or on a specific date)
def preferred_vendor(date = nil)
vendors.max_by { |vendor| vendor.revenue(date) }
end

# returns the vendor with the lowest revenue IF THEY SOLD SOMETHING THAT DAY
# does not count any vendors that didn't sell on a particular date in case
# they weren't present at that particular market on that day
def worst_vendor(date = nil)
all_vendors = vendors
all_vendors_on_day = all_vendors.reject{|vendor| vendor.revenue(date).zero?}
all_vendors_on_day.min_by { |vendor| vendor.revenue(date) }
end
end
39 changes: 39 additions & 0 deletions lib/product.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
class FarMar::Product
attr_reader :id, :name, :vendor_id

# module mixin
extend FarMar::FarMarMethods

def initialize(info_hash)
@id = info_hash[:id].to_i
@name = info_hash[:name]
@vendor_id = info_hash[:vendor_id].to_i
end

def self.all(file = './support/products.csv')
product_keys = [:id, :name, :vendor_id]

make_all(file, product_keys)
end

# returns a lit of all products with the associated vendor_id
def self.by_vendor(id_of_vendor)
self.all.find_all do |product|
product.vendor_id == id_of_vendor
end
end

# returns a vendor instance associated with a particular product
def vendor
FarMar::Vendor.find(vendor_id)
end

# returns a list of all sales associated to product by product_id
def sales
FarMar::Sale.get_by("product", id)
end

def number_of_sales
sales.length
end
end
51 changes: 51 additions & 0 deletions lib/sale.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
class FarMar::Sale
attr_reader :id, :amount, :purchase_time, :vendor_id, :product_id

# module mixin
extend FarMar::FarMarMethods

def initialize(info_hash)
@id = info_hash[:id].to_i
@amount = info_hash[:amount].to_i
@purchase_time = info_hash[:purchase_time]
@vendor_id = info_hash[:vendor_id].to_i
@product_id = info_hash[:product_id].to_i
end

def self.all(file = './support/sales.csv')
sale_keys = [:id, :amount, :purchase_time, :vendor_id, :product_id]

make_all(file, sale_keys)
end

# returns a list of sales associated to a vendor or product
def self.get_by(type, id)
self.all.find_all do |sale|
if type == "vendor"
sale.vendor_id == id
elsif type == "product"
sale.product_id == id
end
end
end

# return an array of all instances within a certain time range
def self.between(beginning_time, end_time)
# convert strings to DateTime objects by parsing
beginning_time = DateTime.parse(beginning_time) if beginning_time.is_a? String
end_time = DateTime.parse(end_time) if end_time.is_a? String

# find all instances within the begin..end range and return array
self.all.find_all do |sale|
(beginning_time..end_time).cover?(DateTime.parse(sale.purchase_time))
end
end

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

def product
FarMar::Product.find(product_id)
end
end
69 changes: 69 additions & 0 deletions lib/vendor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
class FarMar::Vendor
attr_reader :id, :name, :market_id

# module mixin
extend FarMar::FarMarMethods

def initialize(info_hash)
@id = info_hash[:id].to_i
@name = info_hash[:name]
@number_employees = info_hash[:number_employees].to_i
@market_id = info_hash[:market_id].to_i
end

def self.all(file_name = './support/vendors.csv')
vendor_keys = [:id, :name, :number_employees, :market_id]

make_all(file_name, vendor_keys)
end

# returns all vendors with the given market id
def self.by_market(id_of_market)
self.all.find_all do |vendor|
vendor.market_id == id_of_market
end
end

def self.sales_by_date(date)
# must create DateTime object if date is given as a string (like "2013-11-06")
# start at 00:00:00 in the same time zone as the sales.csv file (-0800)
date = DateTime.parse(date + " 00:00:00-08:00")

next_day = date.next
FarMar::Sale.between(date, next_day)
end

# gives all revenue on a particular date from all vendors
def self.revenue(date)
self.sales_by_date(date).reduce(0) { |sum, sale| sum + sale.amount}
end

# returns the associated market instance
def market
FarMar::Market.find(market_id)
end

# returns an array of associated products
def products
FarMar::Product.by_vendor(id)
end

# returns an array of associated sales
def sales
FarMar::Sale.get_by("vendor", id)
end

# returns the sum of all sales in cents
def revenue(date = nil)
# if no date is provided, give sum of all sales by a vendor
return sales.reduce(0) { |sum, sale| sum + sale.amount } if date.nil?

# if a date is given, get all sales with a matching date from a particular vendor
sales_on_day = sales.find_all do |sale|
sale.purchase_time.include? date
end

# get the sum of all sales from that date
sales_on_day.reduce(0) { |sum, sale| sum + sale.amount }
end
end
Loading