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

Mary Morrison FarMar #53

Open
wants to merge 22 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
5f30af8
baseline requirements
morrime Sep 6, 2016
2bbaffc
added describe block for sale and vendor spec files
morrime Sep 6, 2016
7f766a0
built self.all methods for each of the data classes, 100% coverage, 8…
morrime Sep 6, 2016
c0e7ea3
fixed error issue with initialization in each lib/ file and in each r…
morrime Sep 6, 2016
a822a09
modified spec files for market, sale, product, and vendor. Added two …
morrime Sep 7, 2016
a396c19
code and spec for by_market,coverage = 100%
morrime Sep 7, 2016
ad9d129
created test and method for #vendor, works, but need more coverage
morrime Sep 7, 2016
2d1d1ef
deleted some empty lines, nothing special. also realized that I am cr…
morrime Sep 8, 2016
b0ee762
finished Product class method by_vendor(vendor_id) and associated spe…
morrime Sep 8, 2016
6c3ba74
created Vendor method products and spec. Coverage = 100%
morrime Sep 8, 2016
26916e8
finished Vendor method sales and spec, coverage = 100%
morrime Sep 8, 2016
849d917
finished Vendor method #revenue and associated spec, coverage = 100%
morrime Sep 8, 2016
a75f7b6
finished Product method #vendor and associated spec, coverage = 100%
morrime Sep 8, 2016
2f0a675
finished Product method #sales and associated spec, coverage = 100%
morrime Sep 8, 2016
7cbdb89
Finished Product method #number_of_sales and associated spec, coverag…
morrime Sep 8, 2016
88291ff
finished Sale method #vendor and associated spec, though I suspect th…
morrime Sep 8, 2016
3f47dfa
Finished method for Sale #product, but spec needs finishing, and last…
morrime Sep 9, 2016
0fa03d5
marked which may need work
morrime Sep 9, 2016
3cf8559
fixed issue with product_spec describe #vendor, test working. Coverag…
morrime Sep 9, 2016
1486277
fixed sale_spec describe #vendor and sale_spec describe #product, ALL…
morrime Sep 9, 2016
99a8269
Finished Sale self.between(beginning_time, end_time) method and assoc…
morrime Sep 9, 2016
4a3c4fa
added require date and time to far_mar
morrime Sep 9, 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
coverage/
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
16 changes: 16 additions & 0 deletions far_mar.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# gems your project needs
require 'csv'
require 'date'
require 'time'

# our namespace module
module FarMar
require_relative 'lib/market'
require_relative 'lib/product'
require_relative 'lib/vendor'
require_relative 'lib/sale'
end

# all of our data classes that live in the module

# ...require all needed classes
45 changes: 45 additions & 0 deletions lib/market.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# lib/market.rb
require 'csv'

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

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

def self.all
all_markets = []
CSV.read('support/markets.csv').each do |line|
market = {}
market[:id] = line[0].to_i
market[:name] = line[1]
market[:address] = line[2]
market[:city] = line[3]
market[:state] = line[4]
market[:zip] = line[5]
all_markets << self.new(market)
end
return all_markets
end

def self.find(id)
all.each do |market|
if market.id == id
return market
end
end
# if id > 500, returns entire array....fix later
end


def vendor
Vendor.by_market(@id)
end
end
60 changes: 60 additions & 0 deletions lib/product.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# lib/farmar_market.rb
require 'csv'

class Product
attr_reader :id, :name, :vendor_id

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

def self.all
all_products = []

CSV.read('support/products.csv').each do |line|
product = {}
product[:id] = line[0].to_i
product[:name] = line[1]
product[:vendor_id] = line[2].to_i

all_products << self.new(product)
end

return all_products
end

def self.find(id)
all.each do |product|
if product.id == id
return product
end
end
# if id > 500, returns entire array....fix later
end


def self.by_vendor(vendor_id)
all_products = self.all
all_products.find_all do |i|
i.vendor_id == vendor_id
end
end

def vendor
Vendor.find(@vendor_id)
end

########## NEEDS WORK ########### ??????
def sales
Sale.all.find_all do |instance|
instance.vendor_id == @vendor_id
end
end

########## NEEDS WORK ########### ?????
def number_of_sales
sales.count
end
end
63 changes: 63 additions & 0 deletions lib/sale.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# lib/farmar_market.rb
require 'csv'
require 'date'
require 'time'

class Sale
attr_reader :id, :amount_in_cents, :purchase_time, :vendor_id, :product_id

def initialize(sale)
@id = sale[:id]
@amount_in_cents = sale[:amount_in_cents]
@purchase_time = sale[:purchase_time]
@vendor_id = sale[:vendor_id]
@product_id = sale[:product_id]
end

def self.all
all_sales = []

CSV.read('support/sales.csv').each do |line|
sale = {}
sale[:id] = line[0].to_i
sale[:amount_in_cents] = line[1].to_i
sale[:purchase_time] = line[2] # Datetime, should convert?
sale[:vendor_id] = line[3].to_i
sale[:product_id] = line[4].to_i

all_sales << self.new(sale)
end

return all_sales
end

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

def vendor
Vendor.find(@vendor_id)
end

def product
Product.find(@product_id)
end

def self.between(beginning_time, end_time)

d1 = DateTime.parse(beginning_time)
d2 = DateTime.parse(end_time)
all_times = []
all.each do |sale|
q = DateTime.parse(sale.purchase_time)
if q >= d1 && q <= d2
all_times << sale
end
end
return all_times
end
end
73 changes: 73 additions & 0 deletions lib/vendor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# lib/farmar_market.rb
require 'csv'

class Vendor
attr_reader :id, :name, :num_of_employees, :market_id


def initialize(vendor)
@id = vendor[:id]
@name = vendor[:name]
@num_of_employees = vendor[:num_of_employees]
@market_id = vendor[:market_id]
end

def self.all
all_vendors = []

CSV.read('support/vendors.csv').each do |line|
vendor = {}
vendor[:id] = line[0].to_i
vendor[:name] = line[1]
vendor[:num_of_employees] = line[2].to_i
vendor[:market_id] = line[3].to_i


all_vendors << self.new(vendor)
end

return all_vendors
end

def self.find(id)
all.each do |vendor|
if vendor.id == id
return vendor
end
end
# if id > 500, returns entire array....fix later
end

def self.by_market(market_id)
all_vendors = self.all
all_vendors.find_all do |i|
i.market_id == market_id
end
end

def products
Product.by_vendor(@id)
end

def market
Market.find(@market_id)
end

def sales
Sale.all.find_all do |instance|
instance.vendor_id == @id
end
end

def revenue
y = []
sales[0..-1].each do |x|
y << x.amount_in_cents
end
y.reduce (:+)
end

end
#market: returns the FarMar::Market instance that is associated with this vendor using the FarMar::Vendor market_id field
#products: returns a collection of FarMar::Product instances that are associated by the FarMar::Product vendor_id field.
#sales: returns a collection of FarMar::Sale instances that are associated by the vendor_id field.
42 changes: 42 additions & 0 deletions specs/market_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
require_relative 'spec_helper'

module FarMar
describe Market do

describe "#initialize" do
let(:market) { Market.new({id: 501, name: "Pike Place", address: "123 Pike St", city: "Seattle", county: "King", state: "WA", zip: "98116"}) }

it "should create an instance of Market" do
market.must_be_instance_of(Market)
end
end

describe "all" do
it "should return an Array" do
Market.all.must_be_kind_of(Array)
end

it "represents all of the objects described in the CSV" do
Market.all.count.must_equal(500)
end
end

describe "find(id)" do
it "should return the id that the passed parameter matches" do
Market.find(12).id.must_equal(12)
end

it "should return the city that the passed parameter matches" do
Market.find(155).city.must_equal("Charles Town")
end
end

describe "#vendor" do

it "should return true if the amount of vendors for the instance of Market is the number of instances of Vendor with that market_id." do
m = Market.find(2)
m.vendor.length.must_equal(3)
end
end
end
end
62 changes: 62 additions & 0 deletions specs/product_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
require_relative 'spec_helper'

module FarMar
describe Product do

describe "#initialize" do
let(:product) {Product.new({id: 8194, name: "Skittles", vendor_id: 500}) }

it "should create an instance of Product" do
product.must_be_instance_of(Product)
end
end

describe "all" do
it "should return an Array" do
Product.all.must_be_kind_of(Array)
end

it "represents all of the objects described in the CSV" do
Product.all.count.must_equal(8193)
end
end

describe "find(id)" do
it "should return the id that the passed argument matches" do
Product.find(122).id.must_equal(122)
end

it "should return the name that the passed argument matches" do
Product.find(200).name.must_equal("Odd Pretzel")
end
end

describe "by_vendor(vendor_id)" do
it "should return all of the products with the given vendor_id" do
Product.by_vendor(245).count.must_equal(4)

end
end

describe "#vendor" do
it "should return an instance of Vendor" do
p = Product.find(2)
p.vendor.must_be_instance_of(Vendor)
end
end

describe "#sales" do
it "returns true if the returned instances of Sale are of the same product id" do
p = Product.find(2)
p.sales.count.must_equal(1)
end
end

describe "#number_of_sales" do
it "returns the number of times this product has been sold" do
p = Product.find(1)
p.number_of_sales.must_equal(7)
end
end
end
end
Loading