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

Dianne Laguerta--FarMar #46

Open
wants to merge 45 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
dbf0a9f
set up the structure for FarMar project
dlaguerta Sep 6, 2016
e3f4abd
added gitignore
dlaguerta Sep 6, 2016
14f39a8
deleting coverage
dlaguerta Sep 6, 2016
ab5a579
removed coverage hopefully
dlaguerta Sep 6, 2016
b6daa18
deleted require_relative comments, added and passed test for self.al…
dlaguerta Sep 6, 2016
73fae9c
added test for self.find method, passed test. yay.
dlaguerta Sep 6, 2016
4434cd0
added comments to remember to do the vendor method in Markets class
dlaguerta Sep 6, 2016
e6b62e3
updating master with changes to Vendor, product spec, and vendor spec
dlaguerta Sep 7, 2016
951980c
added spec for Vendor.all, added self.all method, make spec pass
dlaguerta Sep 7, 2016
d303cad
added vendor spec test, writing the vendor self.find(id) method now
dlaguerta Sep 7, 2016
8bfc64a
wrote and passed self.find(id) method for Vendor
dlaguerta Sep 7, 2016
bb7c204
added spec and method for initializing Product instances, passed
dlaguerta Sep 7, 2016
5ffeb12
added spec for product self.all, created method, passed test
dlaguerta Sep 7, 2016
c7b793b
fixed error with vendor id, verified that a vendor id is stored in hash
dlaguerta Sep 7, 2016
f9ed21d
fixed error in spec for self.find
dlaguerta Sep 7, 2016
16d70ce
fixed errors in self.find methods for both clasess
dlaguerta Sep 7, 2016
724445b
fixed market error, added spec for product's self.find method
dlaguerta Sep 7, 2016
77a174f
added initialize method and spec for Sale class
dlaguerta Sep 7, 2016
200123f
added self.all method for sale class, passed test
dlaguerta Sep 7, 2016
68fb5cf
added self.find method for sale class, passed test
dlaguerta Sep 7, 2016
9bd4d45
added require CSV to top to for running in irb
dlaguerta Sep 7, 2016
bc75dd4
added edits
dlaguerta Sep 7, 2016
b130202
I broke everything. Now I have to change all self.all and initialize …
dlaguerta Sep 8, 2016
3452f98
fixed errors so all specs pass at this point, edits self.find method …
dlaguerta Sep 8, 2016
74a2bb7
fixed product hashes, fixed, specs, fixed methods yay
dlaguerta Sep 8, 2016
a765554
fixed sales methods and specs
dlaguerta Sep 8, 2016
e67c0e7
added markets method in Vendor class, passed spec
dlaguerta Sep 8, 2016
2f76e8a
added spec and method for by_market in Vendor class, cleaned up code
dlaguerta Sep 8, 2016
3dddaba
added product methods and spec
dlaguerta Sep 8, 2016
31a17e9
added revenue method and spec, works great i think
dlaguerta Sep 8, 2016
1f29c64
changed purchase time to datetime
dlaguerta Sep 8, 2016
9bf2643
in middle of Product class's sales method, fixed Sales class to have …
dlaguerta Sep 8, 2016
c790698
added method for sales in Product class, passed spec
dlaguerta Sep 8, 2016
49851d2
added number of sales method and spec, passed
dlaguerta Sep 8, 2016
950aa14
added spec for self.by_vendor method in Product class, passed
dlaguerta Sep 8, 2016
065086b
finished first method and specs for Sale class
dlaguerta Sep 8, 2016
c84283c
finished primary requirements for FarMar
dlaguerta Sep 9, 2016
0b8d713
cleaned up code comments
dlaguerta Sep 9, 2016
722c734
cleaned up specs and far_mar file
dlaguerta Sep 9, 2016
0c93402
refactoring sales method in Product
dlaguerta Sep 9, 2016
97afca0
added argument error spec to Product class
dlaguerta Sep 9, 2016
c37c05e
changed all .each methods to enumerable methods
dlaguerta Sep 9, 2016
f7ff203
used .reduce for revenue method
dlaguerta Sep 9, 2016
e40abe6
added select method
dlaguerta Sep 9, 2016
5509f45
added another spec...i think. I got distracted
dlaguerta 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
35 changes: 35 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
*.gem
*.rbc
/.config
/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
require_relative 'lib/Market'
require_relative 'lib/Product'
require_relative 'lib/Sale'
require_relative 'lib/Vendor'

end
38 changes: 38 additions & 0 deletions lib/Market.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
module FarMar
class Market
attr_reader :id, :name, :address, :city, :county, :state, :zip, :vendors_at_market

def initialize(id, name, address, city, county, state, zip)
@id = id.to_i
@name = name
@address= address
@city = city
@county = county
@state = state
@zip = zip
end #end initialize method

def self.all
markets_hash = {}
CSV.read("support/markets.csv").each do |line|
id = line[0].to_i
name = line[1]
address = line[2]
city = line[3]
county = line[4]
state = line[5]
zip = line[6]
markets_hash[id] = self.new(id, name, address, city, county, state, zip)
end
return markets_hash
end

def self.find(id)
return self.all[id]
end

def vendors
FarMar::Vendor.by_market(@id)
end #end vendors method
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 @@
require 'CSV'

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

def initialize(id, name, vendor_id)
@id = id.to_i
@name = name.to_s
@vendor_id = vendor_id.to_i
end

def self.all
products_hash = {}
CSV.read("./support/products.csv").each do |line|
id = line[0].to_i
name = line[1]
vendor_id = line[2].to_i
products_hash[id] = self.new(id, name, vendor_id)
end
return products_hash
end

def self.find(id)
if id.is_a?Fixnum
return self.all[id]
else
raise ArgumentError.new("Must pass a valid ID number")
end
end

def vendor
all_vendors = FarMar::Vendor.all
all_vendors.each do |vendor_key, vendor_values|
if @vendor_id == vendor_values.id
return vendor_values
end
end
end

def sales #using select method
all_sales = FarMar::Sale.all
all_sales.select do |sale_key, sale_values|
@id == sale_values.product_id
end
end

def number_of_sales
product_sales = self.sales
return product_sales.length
end

def self.by_vendor(a_vendor_id)
all_products = self.all
all_products.select do |product, product_values|
product_values.vendor_id == a_vendor_id
end
end
end #end class
end #end module
61 changes: 61 additions & 0 deletions lib/Sale.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
require 'CSV'

module FarMar
class Sale
attr_reader :id, :amount, :purchase_time, :vendor_id,
:product_id

def initialize(id, amount, purchase_time, vendor_id, product_id)
@id = id.to_i
@amount = amount.to_i
@purchase_time = purchase_time
@vendor_id = vendor_id.to_i
@product_id = product_id.to_i

end

def self.all
sales_hash = {}
CSV.read("./support/sales.csv").each do |line|
id = line[0].to_i
amount = line[1]
purchase_time = DateTime.parse(line[2])
vendor_id = line[3]
product_id = line[4]
sales_hash[id] = self.new(id, amount, purchase_time, vendor_id, product_id)
end
return sales_hash
end

def self.find(id)
return self.all[id]
end

def vendor
all_vendors = FarMar::Vendor.all
all_vendors.select do |vendor, vendor_values|
vendor_values.id == @vendor_id
return vendor_values
end
end

def product
all_products = FarMar::Product.all
all_products.select do |product, product_values|
product_values.id == @product_id
return product_values
end
end

def self.between(beginning_time, end_time) #assuming beginning and end times are given in DateTime format

between_times = []
self.all.select do |key, value|
value.purchase_time >= beginning_time && value.purchase_time <= end_time
between_times << value
end
return between_times
end

end
end
78 changes: 78 additions & 0 deletions lib/Vendor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
require 'CSV'
module FarMar
class Vendor
attr_reader :id, :name, :num_employees, :market_id

def initialize(id, name, num_employees, market_id)
@id = id.to_i
@name = name.to_s
@num_employees = num_employees.to_i
@market_id = market_id.to_i
end

def self.all
vendors_hash = {}
CSV.read("./support/vendors.csv").each do |line|
id = line[0].to_i
name = line[1]
num_employees = line[2].to_i
market_id = line[3].to_i
vendors_hash[id] = self.new(id, name, num_employees, market_id)
end
return vendors_hash
end

def self.find(id)
return self.all[id]
end

def market
all_markets = FarMar::Market.all
all_markets.select do |market, value|
@market_id == value.id
return value
end
end

def products
all_products = FarMar::Product.all
vendors_products = []
all_products.each do |product, product_values|
if product_values.vendor_id == @id
vendors_products << product_values
end
end
return vendors_products
end

def sales
all_sales = FarMar::Sale.all
vendor_sales =[]
all_sales.each do |sale, sale_values|
if sale_values.vendor_id == @id
vendor_sales << sale_values
end
end
return vendor_sales
end

def revenue #trying reduce
vendor_sales = self.sales #returns an array of sales for vendor
# in parentheses, total_revenue = 0
vendor_sales.inject(0) do |total_revenue, sale|
total_revenue + sale.amount
end
end

def self.by_market(a_market_id)
all_vendors = self.all #returns hash of all vendors
vendors_list = []
all_vendors.each do |vendor_key, vendor_value|
if vendor_value.market_id == a_market_id
vendors_list << vendor_value
end
end
return vendors_list
end
end #end class
end #end module
34 changes: 34 additions & 0 deletions specs/market_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
require_relative 'spec_helper'
module FarMar
describe Market do

describe "#initialize" do
let(:market) { Market.new(112, "ada", "213d", "jax","duval", "Fl", 23332) }
it "should create an instance of Market" do
market.must_be_instance_of(Market)
end
end

describe "self.all" do
let(:markets) { Market.all }
it "should create an array of instances of markets through the CSV file" do
Market.all.must_be_kind_of(Hash)
end

describe "self.find(id)" do
let(:markets) { Market.find(8) }
it "should return an instance of a specific market from the CSV file when you pass its id" do
markets.id.must_equal(8)
end
end #end self.find method

describe "#vendors" do
it "should return a collection of vendors with the same market id" do
foots_market = FarMar::Market.find(20)
foots_market.vendors.length.must_equal(7)
end
end

end
end
end
63 changes: 63 additions & 0 deletions specs/product_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
require_relative 'spec_helper'
module FarMar
describe Product do
describe "#initialize" do
let(:product) { Product.new(23, "milk", 112) }
it "should create an instance of a product" do
product.must_be_instance_of(Product)
end #end initialize method
end

describe "self.all" do
let(:products) { Product.all }

it "should create an array of instances of markets through the CSV file" do
Product.all.must_be_kind_of(Hash)
end
end

describe "self.find(id)" do
let(:foots) { Product.find(12) }
it "should raise an error if a number is not passed" do
proc { Product.find('A') }.must_raise(ArgumentError)
end

it "should return instance of a product by its product id" do
foots.id.must_equal(12)
end
end #end self.find method

describe "#vendor" do
let (:heavy_chicken) {Product.find(3)}
it "should return the vendor instance that matches the product's vendor id" do
heavy_chicken.vendor.id.must_equal(2)
end
end #end vendor spec

describe "#sales" do
let(:products) { Product.find(4) } #the product with an id of 4 has 8 sales
it "should return a collection of sales instances for the specific product" do
# puts products.sales
products.sales.must_be_instance_of(Hash)
end

it "should return the exact number of sales related to that product" do
products.sales.length.must_equal(8)
end
end #end sales spec

describe "#number_of_sales" do
let(:products) { Product.find(4) }
it "should return a number of times the product has sold" do
products.number_of_sales.must_equal(8)
end
end

describe "self.by_vendor(vendor_id)" do
it "should list all products by vendor id" do
all = FarMar::Product.by_vendor(5)
all.length.must_equal(3)
end
end
end #end Product
end #end module
Loading