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

FarMar #36

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
39 changes: 39 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
coverage
.DS_Store


*.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
1 change: 1 addition & 0 deletions .ruby-gemset
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
FarMar
1 change: 1 addition & 0 deletions .ruby-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2.3.0
9 changes: 9 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require 'rake/testtask'

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

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


module FarMar


require_relative './lib/market.rb'
require_relative './lib/product.rb'
require_relative './lib/sale.rb'
require_relative './lib/vendor.rb'

puts Market.new({market_id: 5, market_name: "Quincy Farmers Market", address: "0 Denis Ryan Parkway", city: "Quincy", county: "Norfolk", state: "Massachusetts", zip: "2169"})






end
59 changes: 59 additions & 0 deletions lib/market.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
class FarMar::Market
attr_reader :market_id, :market_name, :address, :city, :county, :state, :zip #these are like the instance variables, but now methods!

def initialize (market_hash)
@market_id = market_hash[:market_id]
@market_name = market_hash[:market_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
market_collection = []
market_array = []
market_array = CSV.read("./support/markets.csv", 'r') #this is reading the csv file and creating and array of arrays


market_array.each do |market|
market_hash = {market_id: market[0], market_name: market[1], address: market[2], city: market[3], county: market[4], state: market[5], zip: market[6]}
new_market = FarMar::Market.new(market_hash)
market_collection << new_market
end
return market_collection #this is an array of FarMar::Market instances***
end



def self.find(id_requested)
id_requested = id_requested.to_s
market_array = []
market_array = self.all
market_array.each do |market| #this is an array of SPECIFIC MARKETS
if market.market_id == id_requested #if id matches the user-requested id, do this:
return market #break out of the loop when you find the id that matches
end
end
return nil #return nil if the id is not found
end


def vendors(assoc_market_id)
assoc_vendors = []
FarMar::Vendor.all.each do |vendor|
if vendor.market_id.to_i == assoc_market_id
assoc_vendors << vendor
end
end
return assoc_vendors
end

end

#go back and refactor! enumerables! one line comparisons! let at the beginning of tests and only once!
92 changes: 92 additions & 0 deletions lib/product.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
class FarMar::Product

attr_reader :product_id, :product_name, :vendor_id #these are like the instance variables, but now methods!

def initialize (product_hash)
@product_id = product_hash[:product_id]
@product_name = product_hash[:product_name]
@vendor_id = product_hash[:vendor_id]
end


def self.all
product_collection = []
product_array = []
product_array = CSV.read("./support/products.csv", 'r')


product_array.each do |product|
product_hash = {product_id: product[0], product_name: product[1], vendor_id: product[2]}
new_product = FarMar::Product.new(product_hash)
product_collection << new_product
end
return product_collection #this is an array of FarMar::Product instances***
end


def self.find(id_requested)
id_requested = id_requested.to_s
product_array = []
product_array = self.all
product_array.each do |product| #this is an array of SPECIFIC PRODUCTS
if product.product_id == id_requested
return product #break out of the loop when you find the id that matches
end
end
return nil
end #return nil if the id is not found



def sales(assoc_product_id)
assoc_sales = []
FarMar::Sale.all.each do |sale|
if sale.product_id.to_i == assoc_product_id
assoc_sales << sale
end
end
return assoc_sales
end



def number_of_sales
product_sales = []

FarMar::Sale.all.each do |sale|
if sale.product_id.to_i == product_id.to_i
product_sales << sale
end
end

return product_sales.length
end



def self.by_vendor(vendor_id)
vendor_products = []

FarMar::Product.all.each do |product|
if product.vendor_id.to_i == vendor_id.to_i
vendor_products << product
end
end

return vendor_products
end



def vendor
self.vendor_id #@vendor_id
FarMar::Vendor.all.each do |vendor|
if vendor.vendor_id == self.vendor_id
return vendor
end
end
end

end

#go back and refactor! enumerables! one line comparisons! let at the beginning of tests and only once!
81 changes: 81 additions & 0 deletions lib/sale.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
class FarMar::Sale
attr_reader :sale_id, :amount, :purchase_time, :vendor_id, :product_id

def initialize (sale_hash)
@sale_id = sale_hash[:sale_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
sale_collection = []
sale_array = []
sale_array = CSV.read("./support/sales.csv", 'r')


sale_array.each do |sale|
sale_hash = {sale_id: sale[0], amount: sale[1], purchase_time: sale[2], vendor_id: sale[3], product_id: sale[4]}
new_sale = FarMar::Sale.new(sale_hash)
sale_collection << new_sale
end
return sale_collection #this is an array of FarMar::Sale instances***
end


def self.find(id_requested) #SALE ID!!!!!
id_requested = id_requested.to_s
sale_array = []
sale_array = self.all
sale_array.each do |sale| #this is an array of SPECIFIC SALES
if sale.sale_id == id_requested #if id matches the user-requested id, do this:
return sale #break out of the loop when you find the id that matches
end
end
return nil #return nil if the id is not found
end

def self.between(start_time, end_time)
these_sales = []
begins = Time.parse(start_time)
ends = Time.parse(end_time)

FarMar::Sale.all.each do |sale|
purchase = Time.parse(sale.purchase_time)
if (purchase >= begins) && (purchase <= ends)
these_sales << sale
end
end
return these_sales
end


def vendor
self.vendor_id
FarMar::Vendor.all.each do |vendor|
if vendor.vendor_id == self.vendor_id
return vendor
end
end
end



def product
self.product_id
FarMar::Product.all.each do |product|
if product.product_id == self.product_id
return product
end
end
end


end


#go back and refactor! enumerables! one line comparisons! let at the beginning of tests and only once!
Loading