Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
e1ce9a0
File setup
guineveresaenger Sep 6, 2016
e27a57f
Added all and find class methods to all classes, including tests
guineveresaenger Sep 7, 2016
52dca47
Updated .gitignore to include extraneous coverage folder. Added vendo…
guineveresaenger Sep 7, 2016
94539d6
Fixed the test for vendors method
guineveresaenger Sep 7, 2016
a59deda
Fixed the vendors method to actually return Vendor objects in an array
guineveresaenger Sep 7, 2016
7c57a62
Updated vendor and vendor specs to have a market method
guineveresaenger Sep 7, 2016
a90240a
Added self.by_market method to vendor; updated tests and Market class
guineveresaenger Sep 7, 2016
ac38f98
Added self.by_vendor method to Product
guineveresaenger Sep 7, 2016
6697f0f
Added products method to Vendor, and self.by_vendor to Product.
guineveresaenger Sep 7, 2016
80ee00c
Fixed initialize test in product specs; added vendor method to Product
guineveresaenger Sep 7, 2016
8d62d74
Added test for vendor method for Sale class
guineveresaenger Sep 8, 2016
adaadf4
Added vendor method to Sales class; added Sales method to Vendor class.
guineveresaenger Sep 8, 2016
a82016a
Cleaned up some of the tests
guineveresaenger Sep 8, 2016
f8e8782
Updated sales method in vendor to use Sales.all method
guineveresaenger Sep 8, 2016
e01dc62
contains actual updated sales method
guineveresaenger Sep 8, 2016
594d073
Added sales method to Product class
guineveresaenger Sep 8, 2016
f9520da
Added number_of_sales method ot Product
guineveresaenger Sep 8, 2016
88f65ae
Updated self.by_vendor to use self.all method
guineveresaenger Sep 8, 2016
22c19c8
Updated self.by_market to use self.all method
guineveresaenger Sep 8, 2016
6297a12
Added revenue method in Vendor
guineveresaenger Sep 8, 2016
61db1e9
Updated revenue method with &: in call to map
guineveresaenger Sep 8, 2016
0194492
Updated the self.find method blocks to have curly braces
guineveresaenger Sep 8, 2016
c20651c
Added self.between method for Sale class, with specs. Project is now …
guineveresaenger Sep 8, 2016
4ed3e52
Formatted specs files
guineveresaenger Sep 8, 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
36 changes: 36 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
*.gem
*.rbc
/.config
/coverage/
coverage
/InstalledFiles
/pkg/
/spec/reports/
/test/tmp/
/test/version_tmp/
/tmp/

## Specific to RubyMotion:
.dat*
.repl_history
build/

## Documentation cache and generated files:
/.yardoc/
/_yardoc/
/doc/
/rdoc/

## Environment normalisation:
/.bundle/
/vendor/bundle
/lib/bundler/man/

# for a library or gem, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# Gemfile.lock
# .ruby-version
# .ruby-gemset

# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
.rvmrc
7 changes: 7 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'rake/testtask'

Rake::TestTask.new do |t|
t.test_files = FileList['specs/*_spec.rb']
end

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

module FarMar; end

require_relative 'lib/farmar_market'
require_relative 'lib/farmar_vendor'
require_relative 'lib/farmar_product'
require_relative 'lib/farmar_sale'
40 changes: 40 additions & 0 deletions lib/farmar_market.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# require 'csv'
# require_relative 'farmar_vendor'

module FarMar
class Market

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

def initialize(market_hash)
@id = market_hash[:id]
@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

def self.all
CSV.open('./support/markets.csv', 'r').map do |line|
self.new({id: line[0].to_i,
name: line[1],
address:line[2],
city: line[3],
county: line[4],
state: line[5],
zip: line[6]})
end
end

def self.find(num)
self.all.find {|market| market.id == num}
end

def vendors
return FarMar::Vendor.by_market(@id)
end

end
end
44 changes: 44 additions & 0 deletions lib/farmar_product.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
require 'csv'


module FarMar
class Product
attr_reader :id, :name, :vendor_id

def initialize(product_hash)
@id = product_hash[:id]
@name = product_hash[:name]
@vendor_id = product_hash[:vendor_id]
end

def self.all
CSV.open('./support/products.csv', 'r').map do |line|
self.new({id: line[0].to_i,
name: line[1],
vendor_id:line[2].to_i
})
end
end

def self.find(num)
self.all.find {|product| product.id == num}
end

def self.by_vendor(vendor_id)
self.all.select {|product| product.vendor_id == vendor_id}
end

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

def sales
FarMar::Sale.all.select {|sale| sale.product_id == @id}
end

def number_of_sales
sales.length
end

end
end
52 changes: 52 additions & 0 deletions lib/farmar_sale.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
require 'csv'
require 'date'

module FarMar
class Sale

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

def initialize(sale_hash)
@id = sale_hash[:id]
@amount = sale_hash[:amount]
@purchase_time = sale_hash[:purchase_time]
@vendor_id = sale_hash[:vendor_id]
@product_id = sale_hash[:product_id]
end

def self.all
CSV.open('./support/sales.csv', 'r').map do |line|
self.new({id: line[0].to_i,
amount: line[1].to_i,
purchase_time: (DateTime.strptime(line[2], '%Y-%m-%d %H:%M:%S %z')),
vendor_id: line[3].to_i,
product_id: line[4].to_i
})

end
end

def self.between(start_time, end_time)
self.all.each.select do |sale|
(start_time < sale.purchase_time) && (sale.purchase_time < end_time)
end
end

def self.find (num)
self.all.find {|sale| sale.id == num}
end

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

def product
FarMar::Product.find(@product_id)
end

end
end

# start_time = DateTime.new(2013, 11, 8, 12, 0, 0, "-8")
# end_time = DateTime.new(2013, 11, 8, 12, 59, 59, "-8")
# FarMar::Sale.between(start_time, end_time)
52 changes: 52 additions & 0 deletions lib/farmar_vendor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# require 'csv'
# require_relative 'farmar_market'

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

def initialize(vendor_hash)

@id = vendor_hash[:id]
@name = vendor_hash[:name]
@employees = vendor_hash[:employees]
@market_id = vendor_hash[:market_id]

end

def self.all
CSV.open('./support/vendors.csv', 'r').map do |line|
self.new({id: line[0].to_i,
name: line[1],
employees:line[2].to_i,
market_id: line[3].to_i
})
end
end

def self.find(num)
self.all.find {|vendor| vendor.id == num}
end

def market
FarMar::Market.find(@market_id)
end

def self.by_market(market_id)
self.all.select {|vendor| vendor.market_id == market_id}
end

def products
return FarMar::Product.by_vendor(@id)
end

def sales
FarMar::Sale.all.select {|sale| sale.vendor_id == @id}
end

def revenue
sales.map(&:amount).reduce(:+)
end

end
end
53 changes: 53 additions & 0 deletions specs/market_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
require_relative 'spec_helper'
# require_relative '../lib/farmar_market'

describe FarMar::Market do
let (:my_market) {FarMar::Market.new({})}
describe "#initialize" do

it "must create a Market object" do
my_market.must_be_instance_of(FarMar::Market)
end

it "must respond to id attribute" do
my_market.must_respond_to(:id)
end

end

describe "self.all" do

it "must return an array" do
FarMar::Market.all.must_be_instance_of(Array)
end

it "contains elements that are Markets that have IDs" do
FarMar::Market.all[0].id.must_equal 1
end

end

describe "self.find" do

it "returns a Market instance" do
FarMar::Market.find(123).must_be_instance_of(FarMar::Market)
end

it "can find a specific market by ID" do
FarMar::Market.find(10).name.must_equal("Saratoga Farmers' Market")
end

end

describe "#vendors" do
another_market = FarMar::Market.new(id: 37)
it "returns an Array" do
another_market.vendors.must_be_instance_of(Array)
end

it "contains information about the market" do
another_market.vendors[0].name.must_equal("Sipes Group")
end

end
end
81 changes: 81 additions & 0 deletions specs/product_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
require_relative 'spec_helper'

describe FarMar::Product do

describe "#initialize" do

let (:my_product) {FarMar::Product.new({})}
it "must create a Product object" do
my_product.must_be_instance_of(FarMar::Product)
end

it "has a name" do
my_product.must_respond_to :name
end

end

describe "self.all" do

it "must return an array" do
FarMar::Product.all.must_be_instance_of(Array)
end

it "has a Product as its first element" do
FarMar::Product.all[0].must_be_instance_of(FarMar::Product)
end

end

describe "self.find" do

it "returns a Market instance" do
FarMar::Product.find(123).must_be_instance_of(FarMar::Product)
end

end

describe "self.by_vendor" do
# I am assuming at this point that my code is tightly linked to the data
# files, so I feel OK writing a test that uses the actual data file.
# Not sure if that is good test writing.
it "returns an array of Products" do
FarMar::Product.by_vendor(37).must_be_instance_of Array
FarMar::Product.by_vendor(37)[0].must_be_instance_of FarMar::Product
FarMar::Product.by_vendor(34)[0].name.must_equal "Nutty Pretzel"
end

end

describe "#vendor" do

new_product = FarMar::Product.new({vendor_id: 4})
it "returns the vendor given by product's vendor id" do
new_product.vendor.id.must_equal 4
new_product.vendor.must_be_instance_of FarMar::Vendor
end

end

describe "#sales" do

new_product = FarMar::Product.new({id:45})
it "must return an array of Sales" do
new_product.sales.must_be_instance_of Array
if new_product.sales.length > 0
new_product.sales[0].must_be_instance_of FarMar::Sale
end
end

end

describe "#number_of_sales" do

product = FarMar::Product.new({id:1})
it "must return a Fixnum amount" do
product.number_of_sales.must_be_instance_of Fixnum
product.number_of_sales.must_equal 7
end

end
end
Loading