diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..57a9b2fa --- /dev/null +++ b/.gitignore @@ -0,0 +1,36 @@ +*.gem +*.rbc +/.config +/coverage/ +/InstalledFiles +/pkg/ +/spec/reports/ +/test/tmp/ +/test/version_tmp/ +/tmp/ +coverage/ + +## 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 diff --git a/Rakefile b/Rakefile new file mode 100644 index 00000000..82e5a661 --- /dev/null +++ b/Rakefile @@ -0,0 +1,7 @@ +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..6603f21a --- /dev/null +++ b/far_mar.rb @@ -0,0 +1,299 @@ +require 'csv' + +module FarMar +end + +require_relative './lib/market' +require_relative './lib/vendor' +require_relative './lib/product' +require_relative './lib/sale' + + +##TEST #21... FarMar::Sale.self.between(beginning_time, end_time) +sample_begin_time = "hello" +# sample_begin_time = FarMar::Sale.all.first.purchase_time +sample_end_time = "bye" +# sample_end_time = FarMar::Sale.all.last.purchase_time +print "sample begin time in TDD: " +puts sample_begin_time +puts "" +print "sample end time in TDD: " +puts sample_end_time +puts "\n" +test = FarMar::Sale.between(sample_begin_time, sample_end_time) +test.each do |item| + puts item.purchase_time +end +# I checked the first 50 objects and all of them are within the sample beginning and end times... great! + +# tests to confirm the formatting for FarMar::Sale.purchase_time is now in DateTime format... +# s = FarMar::Sale.all.last +# puts s.purchase_time +# puts DateTime.parse("2013-11-12 12:00:35 -0800") + + + + +##TEST #20... FarMar::Sale.product +# s = FarMar::Sale.all.last +# puts s.product +# print "I expect the product id below to be:" +# puts s.product_id +# puts "" +# puts s.product.id +# puts "Awesome!" + + + + +##TEST #19... FarMar::Sale.vendor +# s = FarMar::Sale.all.last +# puts s.vendor +# print "I expect the vendor id below to be:" +# puts s.vendor_id +# puts "" +# puts s.vendor.id +# puts "Awesome!" + + + + +##TEST #18... FarMar::Product.number_of_sales +# p = FarMar::Product.all.last +# puts p.sales.to_s +# puts "" +# print "I expect this number to correspond to the number of elements in the above array:" +# puts p.number_of_sales +# puts "" +# puts "Awesome!" + + + + +##TEST #17... FarMar::Product.sales +# p = FarMar::Product.all.last +# puts p.sales.to_s +# print "I expect the two product id's below to be:" +# puts p.id +# puts "" +# puts p.sales.first.product_id +# puts p.sales.last.product_id +# puts "Awesome!" + + + + +##TEST #16... FarMar::Product.vendor +# p = FarMar::Product.all.last +# puts p.vendor +# print "I expect the vendor id below to be:" +# puts p.vendor_id +# puts "" +# puts p.vendor.id +# +# puts "Awesome!" + + + + +##TEST #15... FarMar::Vendor.revenue +# v = FarMar::Vendor.all.last +# puts v.sales.to_s +# print "Look at the above amounts and see if this sum looks accurate:" +# puts v.revenue +# puts "" +# puts "Exactly correct. Awesome!" + + + + +##TEST #14... FarMar::Vendor.sales +# v = FarMar::Vendor.all.last +# puts v.sales.to_s +# print "I expect the two vendor id's below to be:" +# puts v.id +# puts "" +# puts v.sales.first.vendor_id +# puts v.sales.last.vendor_id +# puts "Awesome!" + + + + +##TEST #13... FarMar::Vendor.products +# v = FarMar::Vendor.all.last +# puts v.products.to_s +# print "I expect the two vendor id's below to be:" +# puts v.id +# puts "" +# puts v.products.first.vendor_id +# puts v.products.last.vendor_id +# puts "Awesome!" + + + + +##TEST #12... FarMar::Vendor.market +# v = FarMar::Vendor.all.last +# print "I expect the market id below to be:" +# puts v.market_id +# puts "" +# puts v.market.id +# +# puts "Awesome!" + + + + +##TEST #11... FarMar::Market.vendors +# m = FarMar::Market.all.last +# print "I expect the two market id's below to be:" +# puts m.id +# puts "" +# puts m.vendors.first.market_id +# puts m.vendors.last.market_id +# puts "Awesome!" + + + + +##TEST #10... FarMar::Product.by_vendor(vendor_id) +# print "Here's the vendor I'm calling in the product method below:" +# puts vendor_id = FarMar::Vendor.all.last.id +# +# product_tests = FarMar::Product.by_vendor(vendor_id) +# +# puts "Here are the product objects" +# puts product_tests +# +# puts "Here's the confirmation that all of the vendor_id's in these objects (last line before the line break) match the vendor id noted above..." +# product_tests.each do |item| +# puts item.id +# puts item.name +# puts item.vendor_id +# puts "" +# end + +# puts "Glorious!" + + + + +##TEST #9... FarMar::Vendor.by_market(market_id) +# print "Here's the market I'm calling in the vendor method below:" +# puts market_id = FarMar::Market.all.last.id +# +# vendor_tests = FarMar::Vendor.by_market(market_id) +# +# puts "Here are the vendor objects" +# puts vendor_tests +# +# puts "Here's the confirmation that all of the market_id's in these objects (last line before the line break) match the market id noted above..." +# vendor_tests.each do |item| +# puts item.id +# puts item.name +# puts item.market_id +# puts "" +# end +# +# puts "Glorious!" + + + + +##TEST #8... FarMar::Sale.find(id) +# test = FarMar::Sale.find(FarMar::Sale.all[0].id) +# puts test +# +# puts "Woo hoo!" + + + + +# #TEST #7... FarMar::Sale.all +# test = FarMar::Sale.all +# +# puts "Woo hoo!" +# +# test.each do |item| +# puts item.id +# puts item.purchase_time +# puts "" +# end +# +# puts "end of test" + + + + +##TEST #6... FarMar::Product.find(id) +# test = FarMar::Product.find(FarMar::Product.all[0].id) +# puts test +# +# puts "Woo hoo!" + + + + +# #TEST #5... FarMar::Product.all +# test = FarMar::Product.all +# +# puts "Woo hoo!" +# +# test.each do |item| +# puts item.id +# puts item.name +# puts "" +# end +# +# puts "end of test" + + + + +##TEST #4... FarMar::Vendor.find(id) +# test = FarMar::Vendor.find(FarMar::Vendor.all[0].id) +# puts test +# +# puts "Woo hoo!" + + + + +# #TEST #3... FarMar::Vendor.all +# test = FarMar::Vendor.all +# +# puts "Woo hoo!" +# +# test.each do |item| +# puts item.id +# puts item.name +# puts "" +# end +# +# puts "end of test" + + + + +##TEST #2... FarMar::Market.find(id) +# test = FarMar::Market.find(FarMar::Market.all[0].id) +# puts test +# +# puts "Woo hoo!" + + + + +##TEST #1... FarMar::Market.all +# test = FarMar::Market.all +# +# puts "Woo hoo!" +# +# test.each do |item| +# puts item.id +# puts item.name +# puts "" +# end +# +# puts "end of test" diff --git a/lib/market.rb b/lib/market.rb new file mode 100644 index 00000000..f73e8398 --- /dev/null +++ b/lib/market.rb @@ -0,0 +1,50 @@ +class FarMar::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 + markets_from_csv = [] + CSV.read('./support/markets.csv').each do |line| + csv_market = { + id: line[0].to_i, + name: line[1], + address: line[2], + city: line[3], + county: line[4], + state: line[5], + zip: line[6] + } + markets_from_csv << self.new(csv_market) + end + markets_from_csv + end + + def self.ids + market_ids = [] + self.all.each do |m_object| + market_ids << m_object.id + end + return market_ids + end + + def self.find(id) + if self.ids.include?(id) + self.all.find {|m_object| m_object.id == id} + else + raise ArgumentError.new("Oops, your number input is not a valid market id.") + end + end + + def vendors + return FarMar::Vendor::by_market(@id) + end +end diff --git a/lib/product.rb b/lib/product.rb new file mode 100644 index 00000000..37afd6a9 --- /dev/null +++ b/lib/product.rb @@ -0,0 +1,58 @@ +class FarMar::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 + products_from_csv = [] + CSV.read('./support/products.csv').each do |line| + csv_product = { + id: line[0].to_i, + name: line[1], + vendor_id: line[2].to_i, + } + products_from_csv << self.new(csv_product) + end + products_from_csv + end + + def self.ids + product_ids = [] + self.all.each do |p_object| + product_ids << p_object.id + end + return product_ids + end + + def self.find(id) + if self.ids.include?(id) + self.all.find {|p_object| p_object.id == id} + else + raise ArgumentError.new("Oops, your number input is not a valid product id.") + end + end + + def vendor + return FarMar::Vendor.find(@vendor_id) + end + + def sales + return FarMar::Sale::all.select {|s_object| s_object.product_id == @id} + end + + def number_of_sales + return sales.length + end + + def self.by_vendor(vendor_id) + if FarMar::Vendor.ids.include?(vendor_id) + return self.all.select {|p_object| p_object.vendor_id == vendor_id} + else + raise ArgumentError.new("Oops, your number input is not a valid vendor id.") + end + end +end diff --git a/lib/sale.rb b/lib/sale.rb new file mode 100644 index 00000000..e76bf714 --- /dev/null +++ b/lib/sale.rb @@ -0,0 +1,54 @@ +class FarMar::Sale + attr_reader :id, :amount, :vendor_id, :product_id, :purchase_time + + def initialize(sale_hash) + @id = sale_hash[:id] + @amount = sale_hash[:amount] + @purchase_time = DateTime.parse(sale_hash[:purchase_time]) + @vendor_id = sale_hash[:vendor_id] + @product_id = sale_hash[:product_id] + end + + def self.all + sales_from_csv = [] + CSV.read('./support/sales.csv').each do |line| + csv_sale = { + id: line[0].to_i, + amount: line[1].to_i, + purchase_time: line[2], #need to revisit this format with the CSV file and the FarMar specs + vendor_id: line[3].to_i, + product_id: line[4].to_i, + } + sales_from_csv << self.new(csv_sale) + end + sales_from_csv + end + + def self.ids + sale_ids = [] + self.all.each do |s_object| + sale_ids << s_object.id + end + return sale_ids + end + + def self.find(id) + if self.ids.include?(id) + self.all.find {|s_object| s_object.id == id} + else + raise ArgumentError.new("Oops, your number input is not a valid sale id.") + end + end + + def vendor + return FarMar::Vendor.find(@vendor_id) + end + + def product + return FarMar::Product.find(@product_id) + end + + def self.between(beginning_time, end_time) + return self.all.select {|s_object| s_object.purchase_time > beginning_time && s_object.purchase_time < end_time} + end +end diff --git a/lib/vendor.rb b/lib/vendor.rb new file mode 100644 index 00000000..d0260112 --- /dev/null +++ b/lib/vendor.rb @@ -0,0 +1,68 @@ +class FarMar::Vendor + attr_reader :id, :name, :number_employees, :market_id + + def initialize(vendor_hash) + @id = vendor_hash[:id] + @name = vendor_hash[:name] + @number_employees = vendor_hash[:number_employees] + @market_id = vendor_hash[:market_id] + end + + def self.all + vendors_from_csv = [] + CSV.read('./support/vendors.csv').each do |line| + csv_vendor = { + id: line[0].to_i, + name: line[1], + number_employees: line[2].to_i, + market_id: line[3].to_i, + } + vendors_from_csv << self.new(csv_vendor) + end + vendors_from_csv + end + + def self.ids + vendor_ids = [] + self.all.each do |v_object| + vendor_ids << v_object.id + end + return vendor_ids + end + + def self.find(id) + if self.ids.include?(id) + self.all.find {|v_object| v_object.id == id} + else + raise ArgumentError.new("Oops, your number input is not a valid vendor id.") + end + end + + def market + return FarMar::Market.find(@market_id) + end + + def products + return FarMar::Product::by_vendor(@id) + end + + def sales + return FarMar::Sale::all.select {|s_object| s_object.vendor_id == @id} + end + + def revenue + sale_amounts_by_vendor = [] + self.sales.each do |s_object| + sale_amounts_by_vendor << s_object.amount + end + sale_amounts_by_vendor.reduce(:+) + end + + def self.by_market(market_id) + if FarMar::Market.ids.include?(market_id) + return self.all.select {|v_object| v_object.market_id == market_id} + else + raise ArgumentError.new("Oops, your number input is not a valid market id.") + end + end +end diff --git a/specs/market_spec.rb b/specs/market_spec.rb new file mode 100644 index 00000000..185b681d --- /dev/null +++ b/specs/market_spec.rb @@ -0,0 +1,49 @@ +require_relative 'spec_helper' + +describe FarMar::Market do + describe "#initialize" do + let(:m) {FarMar::Market.new({id: 123, name: "Marina", address: "456 Ruby St", city: "Rochester", county: "Graham", state: "WY", zip: "00529"})} + it "can create a new instance of Market" do + m.must_be_instance_of(FarMar::Market) + end + + it "can assign instance variables according to the input hash" do + m.county.must_equal("Graham") + m.id.must_be_instance_of(Fixnum) + m.zip.must_be_instance_of(String) + end + end + + describe "self.all" do + it "will output an array" do + FarMar::Market.all.must_be_instance_of(Array) + end + + it "will contain a market object as each element of the array" do + FarMar::Market.all[0].must_be_instance_of(FarMar::Market) + FarMar::Market.all[0].id.must_be_instance_of(Fixnum) + end + end + + describe "self.find(id)" do + it "will output a market object" do + FarMar::Market.find(FarMar::Market.all[0].id).must_be_instance_of(FarMar::Market) + end + + it "must take a valid argument" do + proc {FarMar::Market.find(2000000000)}.must_raise(ArgumentError) + end + end + + describe "vendors" do + let(:m) {FarMar::Market.all.last} + it "will output an array" do + m.vendors.must_be_instance_of(Array) + end + + it "will output vendor objects that correspond to the current market_id" do + m.vendors.first.market_id.must_equal(m.id) + m.vendors.last.market_id.must_equal(m.id) + end + end +end diff --git a/specs/product_spec.rb b/specs/product_spec.rb new file mode 100644 index 00000000..320fc2cd --- /dev/null +++ b/specs/product_spec.rb @@ -0,0 +1,85 @@ +require_relative 'spec_helper' + +describe FarMar::Product do + let(:p) {FarMar::Product.all.last} + describe "#initialize" do + let (:p_manual) {FarMar::Product.new({id: 2345, name: "Gluten Free Ice Cream Sandwich", vendor_id: 1234})} + it "can create a new instance of Product" do + p_manual.must_be_instance_of(FarMar::Product) + end + + it "can assign instance variables according to the input hash" do + p_manual.name.must_equal("Gluten Free Ice Cream Sandwich") + p_manual.vendor_id.must_be_instance_of(Fixnum) + end + end + + describe "self.all" do + it "will output an array" do + FarMar::Product.all.must_be_instance_of(Array) + end + + it "will contain a product object as each element of the array" do + FarMar::Product.all[0].must_be_instance_of(FarMar::Product) + FarMar::Product.all[0].id.must_be_instance_of(Fixnum) + end + end + + describe "self.find(id)" do + it "will output a product object" do + FarMar::Product.find(FarMar::Product.all[0].id).must_be_instance_of(FarMar::Product) + end + + it "must take a valid argument" do + proc {FarMar::Product.find(2000000000)}.must_raise(ArgumentError) + end + end + + describe "vendor" do + it "will output a vendor object" do + p.vendor.must_be_instance_of(FarMar::Vendor) + end + + it "will output the object whose vendor id corresponds to the current product instance's vendor id" do + p.vendor.id.must_equal(p.vendor_id) + end + end + + describe "sales" do + it "will output an array" do + p.sales.must_be_instance_of(Array) + end + + it "will output sale objects that correspond to the current product id" do + p.sales.first.product_id.must_equal(p.id) + p.sales.last.product_id.must_equal(p.id) + end + end + + describe "number_of_sales" do + it "will output a number" do + p.number_of_sales.must_be_instance_of(Fixnum) + end + end + + describe "self.by_vendor(vendor_id)" do + let(:vendor_id) {FarMar::Vendor.all.last.id} + it "will output an array" do + FarMar::Product.by_vendor(vendor_id).must_be_instance_of(Array) + end + + it "will contain a product object as each element of the array" do + FarMar::Product.by_vendor(vendor_id)[0].must_be_instance_of(FarMar::Product) + FarMar::Product.by_vendor(vendor_id)[0].name.must_be_instance_of(String) + end + + it "will output product objects whose vendor id corresponds to the vendor id passed into the method" do + FarMar::Product.by_vendor(vendor_id).first.vendor_id.must_equal(vendor_id) + FarMar::Product.by_vendor(vendor_id).last.vendor_id.must_equal(vendor_id) + end + + it "must take a valid argument" do + proc {FarMar::Product.by_vendor(2000000000)}.must_raise(ArgumentError) + end + end +end diff --git a/specs/sale_spec.rb b/specs/sale_spec.rb new file mode 100644 index 00000000..e80b86d7 --- /dev/null +++ b/specs/sale_spec.rb @@ -0,0 +1,70 @@ +require_relative 'spec_helper' + +describe FarMar::Sale do + let(:s) {FarMar::Sale.all.last} + describe "#initialize" do + let (:s_manual) {FarMar::Sale.new({id: 345678, amount: 500, purchase_time: "2013-11-12 12:00:35 -0800", vendor_id: 1234, product_id: 2345})} + it "can create a new instance of Sale" do + s_manual.must_be_instance_of(FarMar::Sale) + end + + it "can assign instance variables according to the input hash" do + s_manual.id.must_equal(345678) + s_manual.product_id.must_be_instance_of(Fixnum) + end + end + + describe "self.all" do + it "will output an array" do + FarMar::Sale.all.must_be_instance_of(Array) + end + + it "will contain a sale object as each element of the array" do + FarMar::Sale.all[0].must_be_instance_of(FarMar::Sale) + FarMar::Sale.all[0].id.must_be_instance_of(Fixnum) + end + end + + describe "self.find(id)" do + it "will output a sale object" do + FarMar::Sale.find(FarMar::Sale.all[0].id).must_be_instance_of(FarMar::Sale) + end + + it "must take a valid argument" do + proc {FarMar::Sale.find(2000000000)}.must_raise(ArgumentError) + end + end + + describe "vendor" do + it "will output a vendor object" do + s.vendor.must_be_instance_of(FarMar::Vendor) + end + + it "will output the object whose vendor id corresponds to the current sale instance's vendor id" do + s.vendor.id.must_equal(s.vendor_id) + end + end + + describe "product" do + it "will output a product object" do + s.product.must_be_instance_of(FarMar::Product) + end + + it "will output the object whose product id corresponds to the current sale instance's product id" do + s.product.id.must_equal(s.product_id) + end + end + + describe "self.between(beginning_time, end_time)" do + let(:sample_begin_time) {FarMar::Sale.all.first.purchase_time} + let(:sample_end_time) {FarMar::Sale.all.last.purchase_time} + it "will output an array" do + FarMar::Sale.between(sample_begin_time, sample_end_time).must_be_instance_of(Array) + end + + it "will contain a sale object as each element of the array" do + FarMar::Sale.between(sample_begin_time, sample_end_time).first.must_be_instance_of(FarMar::Sale) + FarMar::Sale.between(sample_begin_time, sample_end_time).last.must_be_instance_of(FarMar::Sale) + end + end +end diff --git a/specs/spec_helper.rb b/specs/spec_helper.rb new file mode 100644 index 00000000..e78bc89d --- /dev/null +++ b/specs/spec_helper.rb @@ -0,0 +1,14 @@ +require 'simplecov' +SimpleCov.start + +require_relative '../far_mar' + +require 'minitest' +require 'minitest/spec' +require "minitest/autorun" +require "minitest/reporters" +require 'minitest/pride' + +Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new + +#The specs folder is where we put all of our tests. diff --git a/specs/vendor_spec.rb b/specs/vendor_spec.rb new file mode 100644 index 00000000..11eab90c --- /dev/null +++ b/specs/vendor_spec.rb @@ -0,0 +1,97 @@ +require_relative 'spec_helper' + +describe FarMar::Vendor do + let(:v) {FarMar::Vendor.all.last} + describe "#initialize" do + let(:v_manual) {FarMar::Vendor.new({id: 1234, name: "Sweet Bumpas", number_employees: 2, market_id: 123})} + it "can create a new instance of Vendor" do + v_manual.must_be_instance_of(FarMar::Vendor) + end + + it "can assign instance variables according to the input hash" do + v_manual.id.must_equal(1234) + v_manual.number_employees.must_be_instance_of(Fixnum) + v_manual.name.must_be_instance_of(String) + end + end + + describe "self.all" do + it "will output an array" do + FarMar::Vendor.all.must_be_instance_of(Array) + end + + it "will contain a vendor object as each element of the array" do + FarMar::Vendor.all[0].must_be_instance_of(FarMar::Vendor) + FarMar::Vendor.all[0].id.must_be_instance_of(Fixnum) + end + end + + describe "self.find(id)" do + it "will output a vendor object" do + FarMar::Vendor.find(FarMar::Vendor.all[0].id).must_be_instance_of(FarMar::Vendor) + end + + it "must take a valid argument" do + proc {FarMar::Vendor.find(2000000000)}.must_raise(ArgumentError) + end + end + + describe "market" do + it "will output a market object" do + v.market.must_be_instance_of(FarMar::Market) + end + + it "will output the market object whose market id corresponds to the current vendor instance's market id" do + v.market.id.must_equal(v.market_id) + end + end + + describe "products" do + it "will output an array" do + v.products.must_be_instance_of(Array) + end + + it "will output product objects that correspond to the current vendor id" do + v.products.first.vendor_id.must_equal(v.id) + v.products.last.vendor_id.must_equal(v.id) + end + end + + describe "sales" do + it "will output an array" do + v.sales.must_be_instance_of(Array) + end + + it "will output sale objects that correspond to the current vendor id" do + v.sales.first.vendor_id.must_equal(v.id) + v.sales.last.vendor_id.must_equal(v.id) + end + end + + describe "revenue" do + it "will return a fixnum" do + v.revenue.must_be_instance_of(Fixnum) + end + end + + describe "self.by_market(market_id)" do + let(:market_id) {FarMar::Market.all.last.id} + it "will output an array" do + FarMar::Vendor.by_market(market_id).must_be_instance_of(Array) + end + + it "will contain a vendor object as each element of the array" do + FarMar::Vendor.by_market(market_id)[0].must_be_instance_of(FarMar::Vendor) + FarMar::Vendor.by_market(market_id)[0].name.must_be_instance_of(String) + end + + it "will output vendor objects whose market id corresponds to the market id passed into the method" do + FarMar::Vendor.by_market(market_id).first.market_id.must_equal(market_id) + FarMar::Vendor.by_market(market_id).last.market_id.must_equal(market_id) + end + + it "must take a valid argument" do + proc {FarMar::Vendor.by_market(2000000000)}.must_raise(ArgumentError) + end + end +end