Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
coverage
.DS_store
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
14 changes: 14 additions & 0 deletions far_mar.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

require 'csv'


module FarMar;end


# all of our data classes that live in the module
require_relative 'lib/shared_methods'
require_relative 'lib/farmar_market'
require_relative 'lib/farmar_vendor'
require_relative 'lib/farmar_sale'
require_relative 'lib/farmar_product'
# ...require all needed classes
45 changes: 45 additions & 0 deletions lib/farmar_market.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
class FarMar::Market < FarMar::SharedStuff
attr_reader :name, :address, :county, :state, :zip, :market_id, :city
def initialize(line) #for all, csv length -1?
@market_id = line[0].to_i
@name = line[1].to_s
@address = line[2].to_s
@city = line[3].to_s
@county = line[4].to_s
@state = line[5].to_s
@zip = line[6].to_i
end

def self.pull_from_csv
CSV.read('./support/markets.csv')
end

def self.make_new_instance(market_instance)
FarMar::Market.new(market_instance)
end

def vendors #search by market id
FarMar::Vendor.all.select {|vendor_id, instance| instance.market_id == self.market_id}
end

def self.find_by_name(match) #finds first instance of matching name

self.pull_from_csv.each do |instance|
if instance[1].include?(match.downcase.to_s)
return make_new_instance(instance)
end
end
return "No instance found"
end

def self.find_all_by_name(match)
all_match = FarMar::Market.all.select {|vendor_id, instance| instance.name.include?(match.downcase.to_s)}
if all_match.empty?
return "No instance found"
else
return all_match.values #array of instances
end

end

end
34 changes: 34 additions & 0 deletions lib/farmar_product.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
class FarMar::Product < FarMar::SharedStuff
attr_reader :name, :vendor_id, :product_id
def initialize(line) #for all, csv length -1?
@product_id = line[0].to_i
@name = line[1].to_s
@vendor_id = line[2].to_i
end

def self.pull_from_csv
CSV.read('./support/products.csv')
end

def self.make_new_instance(instance)
FarMar::Product.new(instance)
end

def vendor
vendor = FarMar::Vendor.all.select {|vendor_id, instance| instance.vendor_id == self.vendor_id}
return vendor.values[0]
end

def sales
FarMar::Sale.all.select{|sid, instance| instance.product_id == self.product_id}
end

def number_of_sales
sales.length
end

def self.by_vendor(vendor_id)
FarMar::Product.all.select{|vid, instance| instance.vendor_id == vendor_id}
end

end
45 changes: 45 additions & 0 deletions lib/farmar_sale.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
class FarMar::Sale < FarMar::SharedStuff
attr_reader :sale_id, :amount, :purchase_time, :vendor_id, :product_id
def initialize(line) #for all, csv length -1?
@sale_id = line[0].to_i
@amount = line[1].to_i
@purchase_time = line[2].to_i #unix_time, or as close as I could get.
@vendor_id = line[3].to_i
@product_id = line[4].to_i
end

def self.pull_from_csv
CSV.read('./support/sales.csv')
end

def self.make_new_instance(instance)
to_unix_time = instance[2].split("-")
to_unix_time[2] = to_unix_time[2].split
to_unix_time[2] = to_unix_time[2].first
instance[2] = Date.new(to_unix_time[0].to_i,to_unix_time[1].to_i,to_unix_time[2].to_i).to_time
FarMar::Sale.new(instance)
end

def vendor
vendor = FarMar::Vendor.all.select{|vid, instance| instance.vendor_id == self.vendor_id}
return vendor.values[0] #returns instance
end

def product
product = FarMar::Product.all.select{|pid, instance| instance.product_id == self.product_id}
return product.values[0] #returns instance
end

def self.between(beginning_time, end_time) #input format day/month/year only
end_array = end_time.split("/")
end_array.map! {|string| string.to_i}
end_time = Date.new(end_array[2], end_array[0], end_array[1]).to_time

start_array = beginning_time.split("/")
start_array.map! {|string| string.to_i}
beginning_time = Date.new(start_array[2], start_array[0], start_array[1]).to_time

hash_of_instances = self.all.select {|sid, instance| instance.purchase_time.to_i >= beginning_time.to_i && instance.purchase_time.to_i <= end_time.to_i}
return hash_of_instances.values
end
end
41 changes: 41 additions & 0 deletions lib/farmar_vendor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
class FarMar::Vendor < FarMar::SharedStuff
attr_reader :market_id, :name, :vendor_id, :employees
def initialize(line)
@vendor_id = line[0].to_i
@name = line[1]
@employees = line[2].to_i
@market_id = line[3].to_i
end

def self.pull_from_csv
CSV.read('./support/vendors.csv')
end

def self.make_new_instance(instance)
FarMar::Vendor.new(instance)
end

def market #find market instance of vendor instance
FarMar::Market.find(market_id)
end

def products
FarMar::Product.all.reject{|pid, instance| instance.vendor_id != self.vendor_id}
end

def sales
FarMar::Sale.all.reject{|sid, instance| instance.vendor_id != self.vendor_id}
end

def revenue
individual_revenues = sales.map {|sid, instance| instance.amount}
sum = individual_revenues.inject(:+) #sum
return sum[0]
end

def self.by_market(market_id)
self.all.reject{|vid, instances| instances.market_id != market_id}
end


end
21 changes: 21 additions & 0 deletions lib/shared_methods.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class FarMar::SharedStuff

def self.all
instances = {}
self.pull_from_csv.each do |instance|
instances[instance[0]] = make_new_instance(instance)
end
instances
end


def self.find(id) #search by id
self.pull_from_csv.each do |instance|
if instance[0].to_i == id.to_i
return make_new_instance(instance)
end
end
return "No instance found"
end

end
57 changes: 57 additions & 0 deletions specs/markets_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
require_relative './spec_helper'

describe FarMar::Market do
#mid, name, address, county, state, zip
MARKET_TEST = [1, "name", "someplace", "not country", "of being", "44444"]
let(:market) {FarMar::Market.new(MARKET_TEST)}
let(:market_all) {FarMar::Market.all}

describe "Market new" do
it "can create new instances of Market with correct name" do
market.name.must_equal "name"
end
end

describe "Market all" do
it "can create a hash filled with market ids as keys and instances as values" do
market_all.must_be_instance_of Hash
end
end

describe "Market find" do
it "can find an instance of a market by matching id" do
FarMar::Market.find(286).must_be_instance_of FarMar::Market
end

it "will return 'No instance found' if no instance found" do
FarMar::Market.find(333333).must_equal "No instance found"
end
end

describe "Market#vendors" do
it "can find instances of vendors by market id" do
market.vendors.must_be_instance_of Hash
end
end

describe "Market find_by_name" do
it "can find an instance of market matching the passed name" do
FarMar::Market.find_by_name("farmer").must_be_instance_of FarMar::Market
end

it "will return 'No instance found' if search terms don't match an instance" do
FarMar::Market.find_by_name("wut").must_be_instance_of String
end
end

describe "Market find_all_by_name" do
it "can find a collection (array) of markets matching the passed name" do
FarMar::Market.find_all_by_name("farmer").must_be_instance_of Array
end

it "will return 'No instance found' if search terms don't match any instances" do
FarMar::Market.find_all_by_name("xxxxxxb").must_be_instance_of String
end
end

end
45 changes: 45 additions & 0 deletions specs/product_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
require_relative './spec_helper'

describe FarMar::Product do

PROUCT_TEST = [1, "name", 3] #pid, name, vid
let(:item) {FarMar::Product.new(PROUCT_TEST)}
describe "Product new" do
it "can create new instances of Product with correct name" do
item.name.must_equal "name"
end
end

describe "Product find" do
it "can find an instance of a product by matching product id" do
FarMar::Product.find(286).must_be_instance_of FarMar::Product
end
it "will return nil if no instance found" do
FarMar::Product.find(333333).must_equal "No instance found"
end
end

describe "Product#vendor" do
it "can find an instance of vendor from an instance of product('s vendor_id)" do
item.vendor.must_be_instance_of FarMar::Vendor
end
end

describe "Product#sales" do
it "can find all instances of sale from an instance of product id" do
item.sales.must_be_instance_of Hash
end
end

describe "Product#number_of_sales" do
it "can find the number of sale from an instance of product id" do
item.number_of_sales.must_equal 7
end
end

describe "Product by_vendor_id" do
it "returns all products with vendor_id" do
FarMar::Product.by_vendor(3).must_be_instance_of Hash
end
end
end
40 changes: 40 additions & 0 deletions specs/sale_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
require_relative './spec_helper'

describe FarMar::Sale do

SALE_TEST = [1, 3, Time.new, 2, 4] #sid, amount, time, vid, pid
let(:transaction) {FarMar::Sale.new(SALE_TEST)}
describe "Sale new" do
it "can create new instances of Sale with correct amount" do
transaction.amount.must_equal 3
end
end

describe "Sale find" do
it "can find an instance of a sale by matching sale id" do
FarMar::Sale.find(5).must_be_instance_of FarMar::Sale
end
it "will return nil if no instance found" do
FarMar::Sale.find(333333).must_equal "No instance found"
end
end

describe "Sale#vendor" do
it "can find an instance of a vendor matching sale instance referenced" do
transaction.vendor.must_be_instance_of FarMar::Vendor
end
end

describe "Sale#product" do
it "can find an instance of a product matching sale instance referenced" do
transaction.product.must_be_instance_of FarMar::Product
end
end

describe "Sale between" do
it "can find a collection of instances whose purchase time is between two passed times" do
FarMar::Sale.between("11/8/2013", "11/8/2013").must_be_instance_of Array
end
end

end
11 changes: 11 additions & 0 deletions specs/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require 'simplecov'
SimpleCov.start
require 'minitest'
require 'minitest/spec'
require 'minitest/autorun'
require 'minitest/reporters'
require_relative '../far_mar'



Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new
Loading