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

Alma: Reqs done #64

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
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
36 changes: 36 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
*.gem
*.rbc
/.config
/coverage/
/specs/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
22 changes: 15 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
# FarMar: The Farmers' Market Finder
In this assignment we will be creating an application to look up farmers markets and their related vendors, products, and sales. We will use __CSV__ files as our _database_.

Project Data
* [Markets](#markets)
* [Vendors](#vendors)
* [Products](#products)
* [Sales](#sales)
[Baseline](#baseline)
[Primary](#primary)

## Learning Goals
- Reinforce and practice all of the Ruby and programming concepts we've covered in class
- Practice writing specs and using TDD
Expand Down Expand Up @@ -44,7 +52,7 @@ For example, `Sale` is a very generic _class_ name that could very realistically
You must have __90% test coverage__ from `simplecov`. The HTML files that are generated from `simplecov` should _not_ be included in your git repository. Tests should be in the form of __minitest specs__. Complete the necessary boilerplate to create a `Rakefile` and `spec_helper.rb` so that all of your tests run when you run `$ rake` from the project root.

### Project Data
#### FarMar::Market
#### <a name="markets">FarMar::Market</a>
Each individual market has many vendors associated with it. The `FarMar::Market` data, in order in the CSV, consists of:

1. ID - (Fixnum) a unique identifier for that market
Expand All @@ -55,7 +63,7 @@ Each individual market has many vendors associated with it. The `FarMar::Market`
6. State - (String) state in which the market is located
7. Zip - (String) zipcode in which the market is located

#### FarMar::Vendor
#### <a name="vendors">FarMar::Vendor</a>
Each vendor belongs to a market, the `market_id` field refers to the `FarMar::Market` ID field.
Each vendor has many products for sell. The `FarMar::Vendor` data, in order in the CSV, consists of:

Expand All @@ -64,14 +72,14 @@ Each vendor has many products for sell. The `FarMar::Vendor` data, in order in t
3. No. of Employees - (Fixnum) How many employees the vendor has at the market
4. Market_id - (Fixnum) a reference to which market the vendor attends

#### FarMar::Product
#### <a name="products>FarMar::Product</a>
Each product belongs to a vendor. The `vendor_id` field refers to the `FarMar::Vendor` ID field. The `FarMar::Product` data, in order in the CSV, consists of:

1. ID - (Fixnum) uniquely identifies the product
2. Name - (String) the name of the product (not guaranteed unique)
3. Vendor_id - (Fixnum) a reference to which vendor sells this product

#### FarMar::Sale
#### <a name="sales">FarMar::Sale</a>
Each sale belongs to a vendor __AND__ a product. The `vendor_id` and `product_id` fields refer to the `FarMar::Vendor` and `FarMar::Product` ID fields, respectively. The `FarMar::Sale` data, in order in the CSV, consists of:

1. ID - (Fixnum) uniquely identifies the sale
Expand All @@ -81,7 +89,7 @@ Each sale belongs to a vendor __AND__ a product. The `vendor_id` and `product_id
5. Product_id - (Fixnum) a reference to which product was sold

## Requirements
### Baseline
### <a name="baseline">Baseline </a>
#### Project Setup
1. You'll be working as an individual on this project.
1. Fork the Ada-C6 repo to your Github account.
Expand All @@ -101,7 +109,7 @@ Each sale belongs to a vendor __AND__ a product. The `vendor_id` and `product_id
- Complete the boilerplate necessary for testing. You should be able to `$ rake` from the project root to run your specs. Have at least one spec to verify this setup before submitting your baseline.
- **Once you have completed your baseline, you must submit a pull-request and get it approved by an instructor.**

## Primary Requirements
## <a name="primary">Primary Requirements</a>
### For each of the data classes build the following methods:
1. `self.all`: returns a collection of instances, representing all of the objects described in the CSV
1. `self.find(id)`: returns an instance of the object where the value of the `id` field in the CSV matches the passed parameter.
Expand Down Expand Up @@ -154,4 +162,4 @@ Each sale belongs to a vendor __AND__ a product. The `vendor_id` and `product_id

#### Try some inheritance or some composition
- __Inheritance:__ Create a new _class_ that defines the shared/duplicated methods (i.e., `find`, `all`). Update your data classes to _inherit_ this _class_ .
- __Composition with a Mixin:__ Create a new _module_ that defines the duplicated methods (i.e., `find`, `all`). Update your data classes to _mixin_ this _module_.
- __Composition with a Mixin:__ Create a new _module_ that defines the duplicated methods (i.e., `find`, `all`). Update your data classes to _mixin_ this _module_.
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
11 changes: 11 additions & 0 deletions far_mar.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require_relative "lib/market.rb"
require_relative "lib/vendor.rb"
require_relative "lib/product.rb"
require_relative "lib/sale.rb"
require 'csv'
require 'date'
module FarMar
# string_date = "2013-11-09 23:42:41 -0800"
# date = DateTime.parse(string_date)
# puts date
end
49 changes: 49 additions & 0 deletions lib/market.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
module FarMar
class Market

attr_reader :id, :name, :address, :city, :county, :state, :zipcode

def initialize(id, name, address, city, county, state, zipcode)
@id = id
@name = name
@address = address
@city = city
@county = county
@state = state
@zipcode = zipcode
end

# Returns a collection of instances, representing all of the objects described in the CSV
def self.all
markets = {}
CSV.read('support/markets.csv').each do |line|
#these are the arguments fed into the class instance -don't confuse with an array
market = self.new(line[0].to_i,line[1],line[2],line[3],line[4],line[5],line[6])
markets[market.id] = market
end
return markets
end

# Returns an instance of the object where the value of the id field in the CSV matches the passed parameter.
def self.find(id)
markets = self.all
return markets[id]
end

#Returns a collection of Vendor instances, associated with the market by the market_id field.
def vendors
market_vendors = Vendor.all.select { |vendor_id, vendor|
vendor.market_id == id } # id is attr_reader id, no need for argument
return market_vendors
# Refactoring from:
# market_vendors = {}
# Vendor.all.each do |vendor_id, vendor|
# if vendor.market_id == id # id is attr_reader id, no need for argument
# market_vendors[vendor_id] = vendor
# end
# end #each
# return market_vendors
end

end #class
end #module
77 changes: 77 additions & 0 deletions lib/product.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
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

# Returns a collection of instances, representing all of the objects described in the CSV
def self.all
products = {}
CSV.read('support/products.csv').each do |line|
product = self.new(line[0].to_i,line[1],line[2].to_i)
products[product.id] = product
end
return products
end

# Returns an instance of the object where the value of the id field in the CSV matches the passed parameter.
def self.find(id)
products = self.all
return products[id]
end

# Returns the Vendor instance, associated with this vendor using the Product vendor_id field
def vendor
product_vendor_array = Vendor.all.find { |ven_id, ven|
ven_id == vendor_id }
product_vendor = product_vendor_array[1] # second element in find array
return product_vendor # vendor object

# Refactoring:
# product_vendor = Vendor.new(:id, :name, :number_of_employees, :market_id)
# Vendor.all.each do |ven_id, ven|
# if ven_id == vendor_id
# product_vendor = ven
# break
# end
# end
# return product_vendor # vendor object, not hash
end

# Returns a collection of Sale instances, associated using the Sale product_id field.
def sales
product_sales = Sale.all.select { |sale_id, sale|
sale.product_id == id }
return product_sales

# Refactoring:
# product_sales = {}
# Sale.all.each do |sale_id, sale|
# if sale.product_id == id
# product_sales[sale_id] = sale
# end
# end
# return product_sales
end

#Returns the number of times this product has been sold.
def number_of_sales
return sales.length
end

#Returns all of the products with the given vendor_id
def self.by_vendor(ven_id)
vendor_products = {}
self.all.each do |pro_id, pro|
if ven_id == pro.vendor_id
vendor_products[pro_id] = pro
end
end
return vendor_products
end

end
end
67 changes: 67 additions & 0 deletions lib/sale.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
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 #in cents
@purchase_time = purchase_time
@vendor_id = vendor_id
@product_id = product_id
end

# Returns a collection of instances, representing all of the objects described in the CSV
def self.all
sales = {}
CSV.read('support/sales.csv').each do |line|
sale = self.new(line[0].to_i,line[1].to_i,DateTime.parse(line[2]),line[3].to_i,line[4].to_i)
sales[sale.id] = sale
end
return sales # Never, ever, ever, ever, ever (!!) forget this one again.
end

# Returns an instance of the object where the value of the id field in the CSV matches the passed parameter.
def self.find(id)
vendors = self.all
return vendors[id]
end

# Returns the Vendor instance, associated with this sale, using the Sale vendor_id field
def vendor
sale_vendor = Vendor.new(:id, :name, :number_of_employees, :market_id)
Vendor.all.each do |ven_id, ven|
if ven_id == vendor_id
sale_vendor = ven
end
end
return sale_vendor
end

#Returns the Product instance, associated with this sale using the Sale product_id field
def product
sale_product = Product.new(:id, :name, :vendor_id)
Product.all.each do |pro_id, pro|
if pro_id == product_id
sale_product = pro
break
end
end
return sale_product
end

# Returns a collection of Sale objects where the purchase time is between the two times given as arguments
def self.between(beginning_time, end_time)
sales_within_times = {}
self.all.each do |sal_id, sal|
if beginning_time <= sal.purchase_time
if end_time >= sal.purchase_time
sales_within_times[sal_id] = sal
end
end
end
return sales_within_times
end

end #class
end #module
Loading