diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..3b6ee585 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/coverage diff --git a/Rakefile b/Rakefile new file mode 100644 index 00000000..66bfb941 --- /dev/null +++ b/Rakefile @@ -0,0 +1,8 @@ + +require 'rake/testtask' + +Rake::TestTask.new do |t| + t.test_files = FileList['specs/*_spec.rb'] +end + +task default: :test diff --git a/far_mar.rb b/far_mar.rb new file mode 100644 index 00000000..9822cb56 --- /dev/null +++ b/far_mar.rb @@ -0,0 +1,12 @@ +# 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/market' +require_relative './lib/product' +require_relative './lib/sale' +require_relative './lib/vendor' diff --git a/lib/market.rb b/lib/market.rb new file mode 100644 index 00000000..2af4aedd --- /dev/null +++ b/lib/market.rb @@ -0,0 +1,46 @@ +module FarMar + class Market + attr_reader :id + + def initialize(id, name, address, city, county, state, zip) + @id = id + @name = name + @address = address + @city = city + @county = county + @state = state + @zip = zip + end + + def self.all + all_markets = {} + CSV.read('support/markets.csv').each do |line| + all_markets[line[0].to_i] = self.new(line[0].to_i, line[1], line[2], line[3],line[4], line[5], line[6]) + end + return all_markets + end + + def self.find (id) + markets = self.all + return markets[id] + end + + def vendors + FarMar::Vendor.by_market(@id) + end + + #products returns a collection of FarMar::Product instances that are associated to the market through theFarMar::Vendor class. + def products + products_by_market = [] + #gives me an array of vendors + venders_by_market = FarMar::Vendor.by_market(@id) + + venders_by_market.each do | value | + products = FarMar::Product.by_vendor(value.id) + products_by_market.concat(products) + end + + return products_by_market + end + end +end diff --git a/lib/product.rb b/lib/product.rb new file mode 100644 index 00000000..b6c89f57 --- /dev/null +++ b/lib/product.rb @@ -0,0 +1,61 @@ +module FarMar + class Product + attr_reader :id, :name, :vendor_id + + def initialize(id, name, vendor_id) + @id = id + @name = name + @vendor_id = vendor_id + end + + def self.all + all_products = {} + CSV.read('support/products.csv').each do |line| + all_products[line[0].to_i] = self.new(line[0].to_i, line[1], line[2].to_i) + end + return all_products + end + + def self.find (id) + products = self.all + return products[id] + end + + #self.by_vendor(vendor_id): returns all of the products with the given vendor_id + def self.by_vendor(ven_id) + products = self.all + products_by_vendor = [] + + products.each do | product_key, value | + if value.vendor_id == ven_id + products_by_vendor << value + end + end + return products_by_vendor + end + + #vendor: returns the FarMar::Vendor instance that is associated with this vendor using the FarMar::Product.vendor_id field + def vendor + FarMar::Vendor.find(@vendor_id) + end + + #sales: returns a collection of FarMar::Sale instances that are associated using the FarMar::Sale.product_id field. + def sales + sales_by_product_id = [] + all_sales = FarMar::Sale.all + + all_sales.each do |sale_key, sale_value| + if sale_value.product_id == @id + sales_by_product_id << sale_value + end + + end + return sales_by_product_id + end + + # number_of_sales: returns the number of times this product has been sold. + def number_of_sales + return sales.length + end + end +end diff --git a/lib/sale.rb b/lib/sale.rb new file mode 100644 index 00000000..2d787922 --- /dev/null +++ b/lib/sale.rb @@ -0,0 +1,51 @@ +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 + @amount = amount + @purchase_time = purchase_time + @vendor_id = vendor_id + @product_id = product_id + end + + def self.all + all_sales = {} + CSV.read('support/sales.csv').each do | line | + all_sales[ line[0].to_i ] = self.new(line[0].to_i, line[1].to_i, line[2], line[3].to_i, line[4].to_i) + end + return all_sales + end + + def self.find (id) + sales = self.all + return sales[id] + end + + #vendor: returns the FarMar::Vendor instance that is associated with this sale using the FarMar::Sale.vendor_id field + def vendor + FarMar::Vendor.find(@vendor_id) + end + + #product: returns the FarMar::Product instance that is associated with this sale using the FarMar::Saleproduct_id field + def product + FarMar::Product.find(@product_id) + end + + # self.between(beginning_time, end_time): returns a collection ofFarMar::Sale objects where the purchase time is between the two timesgiven as arguments + def self.between (start_time, end_time) + all_sales = self.all + sales_between = [] + s_time = DateTime.parse(start_time) + e_time = DateTime.parse(end_time) + + all_sales.each do | sale_key, sale_values | + if (DateTime.parse(sale_values.purchase_time) >= s_time) && (DateTime.parse(sale_values.purchase_time) < e_time) + sales_between << sale_values + end + end + return sales_between + end + end +end diff --git a/lib/vendor.rb b/lib/vendor.rb new file mode 100644 index 00000000..e1fc53d9 --- /dev/null +++ b/lib/vendor.rb @@ -0,0 +1,109 @@ +module FarMar + class Vendor + attr_reader :id, :name, :num_of_employees, :market_id, :revenue_from_sales + + def initialize(id, name, num_of_employees, market_id) + @id = id + @name = name + @num_of_employees = num_of_employees + @market_id = market_id + end + + def self.all + all_vendors = {} + CSV.read('support/vendors.csv').each do | line | + all_vendors[line[0].to_i] = self.new(line[0].to_i, line[1], line[2].to_i, line[3].to_i) + end + return all_vendors + end + + def self.find(id) + vendors = self.all + return vendors[id] + end + + def self.by_market(id) + all_vendors = self.all + vendors_by_market = [] + + #key is vendor_id and the value is the vendor object + all_vendors.each do | vendor_key, value| + if value.market_id == id + vendors_by_market << value + end + end + #returns all of the vendors with the given market_id + return vendors_by_market + end + + #market: returns the FarMar::Market instance that is associated with this vendor using the FarMar::Vendor.market_id field + def market + FarMar::Market.find(@market_id) + end + + def products + FarMar::Product.by_vendor(@id) + end + + #sales: returns a collection of FarMar::Sale instances that are associated by the vendor_id field. + # def sales + # @sales_by_vendor = {} + # + # products.each do | product | + # if product.sales && product.sales.any? + # @sales_by_vendor[product.id] = product.sales + # end + # end + # return @sales_by_vendor + # end + # + # def revenue + # sales + # revenue_by_product = @sales_by_vendor.map do |k, v| + # all_sales = [] + # v.each do | sale | + # all_sales << sale.amount + # end + # all_sales.reduce(:+) + # end + # @revenue_from_sales = revenue_by_product.reduce(:+) + # return @revenue_from_sales + # end + + + def sales + sales_by_vendor = [] + @all_sales ||= FarMar::Sale.all + @all_sales.each do | sales_key, sale_value | + if sale_value.vendor_id == @id + sales_by_vendor << sale_value + end + end + return sales_by_vendor + end + + def revenue + @revenue_from_sales = 0 + @sales_by_vendor ||= sales + @sales_by_vendor.each do | sales_value | + @revenue_from_sales += sales_value.amount + end + return @revenue_from_sales + end + + # # should return the top n vendor instances ranked by total revenue + # def self.most_revenue(n) + # all_vendors = self.all + # all_vendors.each do | key, value | + # value.revenue + # end + # + # puts "---------" + # highest = all_vendors.sort_by { | value | value.revenue_from_sales } + # puts highest + # + # # highest.reverse + # # return highest.take(n) + # end + end +end diff --git a/specs/market_spec.rb b/specs/market_spec.rb new file mode 100644 index 00000000..652cb69d --- /dev/null +++ b/specs/market_spec.rb @@ -0,0 +1,61 @@ +require_relative 'spec_helper' + +describe FarMar::Market do + describe "#initialize" do + greenlake = FarMar::Market.new(123, "greenlake", "123 green", "Green Lake", "Seattle", "WA", "98105") + + it "should create a new instance" do + greenlake.must_be_instance_of(FarMar::Market) + end + end + + describe "self.all" do + it "should return a hash of instances, representing all of the objects described in the CSV" do + FarMar::Market.all.must_be_kind_of(Hash) + end + end + + describe "self.find(id)" do + it "should return an instance of the object where the value of the id field in the CSV matches the passed parameter" do + FarMar::Market.find(1).id.must_equal(1) + end + end + + describe "#vendors" do + let(:market_test) {FarMar::Market.find(2)} + it "should return the correct number of FarMar::Vendor instances that are associated with the market instance" do + # market_test = FarMar::Market.find(2) + market_test.vendors.length.must_equal(3) + end + + it "should return the correct id of the stated FarMar::Vendor instance that is associated with the market instance" do + # market_test = FarMar::Market.find(2) + market_test.vendors[0].id.must_equal(7) + end + + it "should return true if the correct number of FarMar::Vendor instances that are associated with the market instance are returned by the indstance method" do + market_test2 = FarMar::Market.find(1) + market_test2.vendors.length.must_equal(6) + end + end + + # products returns a collection of FarMar::Product instances that are associated to the market through theFarMar::Vendor class. + describe "#products" do + let(:product_test) {FarMar::Market.find(2)} + it "should returns a collection of FarMar::Product instances that are associated to the market through theFarMar::Vendor class" do + product_test.products.length.must_equal(9) + end + + it "should returns a the correct id of one item in the collection of FarMar::Product instances that are associated to the market through theFarMar::Vendor class" do + puts product_test.products[0].id + product_test.products[0].id.must_equal(14) + end + end + + # self.search(search_term) returns a collection of FarMar::Market instances where the market name or vendor name contain the search_term. For example FarMar::Market.search('school') would return 3 results, one being the market with id 75 (Fox School Farmers FarMar::Market). + # describe "self.search(search_term)" do + # it "should returna collection of FarMar::Market instances where the market name or vendor name contain the search_term." do + # + # end + # end +end diff --git a/specs/product_spec.rb b/specs/product_spec.rb new file mode 100644 index 00000000..edc67f3a --- /dev/null +++ b/specs/product_spec.rb @@ -0,0 +1,62 @@ +require_relative 'spec_helper' + +describe FarMar::Product do + describe "#initialize" do + test_product = FarMar::Product.new(45, "soap", 1234) + + it "should create a new instance" do + test_product.must_be_instance_of(FarMar::Product) + end + end + + describe "self.all" do + it "should return a hash of instances, representing all of the objects described in the CSV" do + FarMar::Product.all.must_be_kind_of(Hash) + end + end + + describe "self.find(id)" do + it "should return an instance of the object where the value of the id field in the CSV matches the passed parameter" do + FarMar::Product.find(1).id.must_equal(1) + end + end + + describe "self.by_vendor(ven_id)" do + it "should return True if the correct number of products is returned for the given vendor_id" do + FarMar::Product.by_vendor(5).length.must_equal(3) + end + + it "should return a correct product id that is part of the collection returned for the given vendor_id" do + FarMar::Product.by_vendor(5)[0].id.must_equal(8) + end + end + + #vendor: returns the FarMar::Vendor instance that is associated with this vendor using the FarMar::Productvendor_id field + describe "#vendor" do + it "should be true if the FarMar::Vendor instance associated with the FarMar::Product.vendor_id is correct" do + vendor_test = FarMar::Product.find(4) + # puts vendor_test.vendor + vendor_test.vendor.id.must_equal(3) + end + end + + #sales: returns a collection of FarMar::Sale instances that are associated using the FarMar::Sale product_id field. + describe "#sales" do + let(:sales_test) {FarMar::Product.find(8)} + it "should be true if the number of FarMar::Sale instances in the collection, associated with the FarMar::Sale.product_id field, is correct" do + sales_test.sales.length.must_equal(5) + end + + it "should return a correct sale id that is part of the collection returned for the given product_id" do + sales_test.sales[0].id.must_equal(23) + end + end + + # number_of_sales: returns the number of times this product has been sold. + describe "#number_of_sales" do + it "should return true if the number of times this product has been sold is correct." do + num_sales_test = FarMar::Product.find(4) + num_sales_test.number_of_sales.must_equal(8) + end + end +end diff --git a/specs/sale_spec.rb b/specs/sale_spec.rb new file mode 100644 index 00000000..4de9b857 --- /dev/null +++ b/specs/sale_spec.rb @@ -0,0 +1,63 @@ +require_relative 'spec_helper' + +describe FarMar::Sale do + describe "#initialize" do + test_sale = FarMar::Sale.new(1, 150, 3/5/15, 2, 5) + + it "should create a new instance" do + test_sale.must_be_instance_of(FarMar::Sale) + end + end + + describe "self.all" do + it "should return a hash of instances, representing all of the objects described in the CSV" do + FarMar::Sale.all.must_be_kind_of(Hash) + end + end + + describe "self.find(id)" do + it "should return an instance of the object where the value of the id field in the CSV matches the passedparameter" do + FarMar::Sale.find(1).id.must_equal(1) + end + end + + #vendor: returns the FarMar::Vendor instance that is associated with this sale using the FarMar::Sale.vendor_id field + describe "#vendor" do + it "should be true if the FarMar::Vendor instance has an id that is associated with this sale using the FarMar::Sale vendor_id" do + vendor_test = FarMar::Sale.find(4) + # puts vendor_test.id + # puts vendor_test.vendor_id + vendor_test.vendor.id.must_equal(1) + end + end + + #product: returns the FarMar::Product instance that is associated with this sale using the FarMar::Saleproduct_id field + describe "#product" do + it "should be true if the FarMar::Product instanct id that is associated with this sale is correct, using the FarMar::Sale.product_id attribute" do + product_test = FarMar::Sale.find(9) + product_test.product.id.must_equal(4) + end + end + + # self.between(beginning_time, end_time): returns a collection of FarMar::Sale objects where the purchase time is between the two times given as arguments + describe "self.between(beginning_time, end_time)" do + it "should return a collection of FarMar::Sale objects where the purchase time is between the two times given as arguments" do + start_time = "2013-11-07 04:34:56 -0800" + end_time = "2013-11-07 04:34:57 -0800" + FarMar::Sale.between(start_time, end_time).length.must_equal(1) + end + + it "should return a collection of FarMar::Sale objects > 1 where the purchase time is between the two times given as arguments" do + start_time = "2013-11-07 04:30:00 -0800" + end_time = "2013-11-07 06:00:00 -0800" + FarMar::Sale.between(start_time, end_time).length.must_be(:>, 1) + end + + it "should return an object with the correct date that has the same purchase_time as teh start_time given" do + start_time = "2013-11-07 04:34:56 -0800" + end_time = "2013-11-07 04:34:57 -0800" + test_date = FarMar::Sale.between(start_time, end_time)[0].purchase_time + test_date.must_equal(start_time) + end + end +end diff --git a/specs/spec_helper.rb b/specs/spec_helper.rb new file mode 100644 index 00000000..1d2d6d90 --- /dev/null +++ b/specs/spec_helper.rb @@ -0,0 +1,14 @@ +#adds coverage folder to tree +#to run coverage report type this into terminal: open coverage/index.html +require 'simplecov' +SimpleCov.start + +require 'minitest' +require 'minitest/spec' +require "minitest/autorun" +require "minitest/reporters" +require 'minitest/pride' + +require_relative '../far_mar' + +Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new diff --git a/specs/vendor_spec.rb b/specs/vendor_spec.rb new file mode 100644 index 00000000..52e53c4e --- /dev/null +++ b/specs/vendor_spec.rb @@ -0,0 +1,86 @@ +require_relative 'spec_helper' + +describe FarMar::Vendor do + describe "#initialize" do + test_vendor = FarMar::Vendor.new(784, "honeybees", 4, 560) + + it "should create a new instance" do + test_vendor.must_be_instance_of(FarMar::Vendor) + end + end + + describe "self.all" do + it "should return a hash of instances, representing all of the objects described in the CSV" do + FarMar::Vendor.all.must_be_kind_of(Hash) + end + end + + # should return an instance of the object where the value of the id field in the CSV matches the passed parameter + describe "self.find(id)" do + it "should return true if the passed parameter of through the class method returns an instance that matches the id field in the CSV file" do + FarMar::Vendor.find(2).id.must_equal(2) + end + end + + describe "#market" do + it "should return True if the correct FarMar::Market instance that is associated with this vendor is outputed using the FarMar::Vendor.market_id field" do + vendor_9 = FarMar::Vendor.find(9) + puts vendor_9 + vendor_9.market.id.must_equal(2) + end + end + + describe "#products" do + let(:product_test) {FarMar::Vendor.find(5)} + it "should return true if the number of FarMar::Product instances that are associated with the vendor instance is correct" do + product_test.products.length.must_equal(3) + end + + it "should return true if the correct FarMar::Product id from the collection of instances that are associated with the vendor instance is correct" do + product_test.products[0].id.must_equal(8) + end + end + + describe "self.by_market(market_id)" do + it "should return an array of vendors with the given market_id" do + FarMar::Vendor.by_market(2).must_be_kind_of(Array) + end + + it "should return True if the correct number of vendors is returned for the given market_id" do + FarMar::Vendor.by_market(2).length.must_equal(3) + end + + it "should return true if the correct vendor id from the collection of instances that are associated with the market id is correct" do + FarMar::Vendor.by_market(2)[0].id.must_equal(7) + end + + end + + #sales: returns a collection of FarMar::Sale instances that are associated by the vendor_id field. + describe "#sales" do + it "should return true if a sample of the collection of FarMar::Sale instances associated with the correct vendor_id field given are the same" do + sales_test = FarMar::Vendor.find(1) + # puts sales_test.id + # puts sales_test.sales + # sales_test.sales.length.must_equal(1) + # sales_test.sales[1].length.must_equal(7) + sales_test.sales.length.must_equal(7) + + + end + end + + #revenue: returns the the sum of all of the vendor's sales (in cents) + describe "#revenue" do + it "should return true if the sum of all of the vendor's sales(in cents) is correct" do + revenue_test = FarMar::Vendor.find(4) + revenue_test.revenue.must_equal(26866) + end + end + + # describe "self.most_revenue(n)" do + # it "should return the top n vendor instances ranked by total revenue" do + # FarMar::Vendor.most_revenue(1).length.must_equal(1) + # end + # end +end