Skip to content

Commit

Permalink
Refactor index
Browse files Browse the repository at this point in the history
  • Loading branch information
thejonanshow committed Mar 28, 2012
1 parent 3a02b49 commit e59c55c
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 25 deletions.
4 changes: 2 additions & 2 deletions lib/sales_engine.rb
Expand Up @@ -34,9 +34,9 @@ def self.load_directory(directory, options = {})
if options[:index]
Thread.new do
t = Time.now
puts "indexing everything!"
SalesEngine::Persistence.instance.index(:id)
puts "Indexing completed in #{Time.now - t} seconds!"
SalesEngine::Persistence.instance.insert_index(:merchant_id, SalesEngine::Invoice)
puts "\nIndexing completed in #{Time.now - t} seconds!"
end
end
end
Expand Down
4 changes: 4 additions & 0 deletions lib/sales_engine/invoice.rb
Expand Up @@ -25,5 +25,9 @@ def customer
def merchant
@merchant ||= Merchant.find(@merchant_id)
end

def revenue
InvoiceItem.find_all_by_invoice_id(id).map(&:revenue).inject(:+)
end
end
end
4 changes: 4 additions & 0 deletions lib/sales_engine/invoice_item.rb
Expand Up @@ -16,6 +16,10 @@ def initialize(attributes)
validate_attributes
end

def revenue
unit_price * quantity
end

private

def validate_attributes
Expand Down
18 changes: 16 additions & 2 deletions lib/sales_engine/merchant.rb
Expand Up @@ -27,9 +27,23 @@ def invoices
@invoices ||= Invoice.find_all_by_merchant_id(self.id)
end

def revenue(date=nil)

def revenue
@revenue ||= Invoice.find_all_by_merchant_id(id).map(&:revenue).inject(:+)
end

def self.revenue(date=nil)
Invoice.find_all_by_created_at_date(date).map(&:revenue).inject(:+)
end

def self.most_revenue(n)
merchants_and_revenues = []

merchants = Merchant.find_all
merchants.each do |merchant|
merchants_and_revenues.push [merchant.revenue, merchant]
end

# merchants_and_revenues.sort_by { |rev_mer| rev_mer[0] }.map(&:last)[0..n]
end
end
end
18 changes: 15 additions & 3 deletions lib/sales_engine/model.rb
Expand Up @@ -49,6 +49,10 @@ def self.included(target)
target.extend ClassMethods
end

def created_at_date
@created_at.to_date
end

module ClassMethods
def find(id)
find_by_id(id)
Expand Down Expand Up @@ -94,7 +98,7 @@ def find_by(attribute, *values)
indices = SalesEngine::Persistence.instance.fetch_indices(self)

unless indices.empty?
model = indices[attribute][value] if indices[attribute]
model = indices[attribute][value].first if indices[attribute]
end

unless model
Expand All @@ -109,8 +113,16 @@ def find_all_by(attribute, *values)
attribute = attribute.to_sym
value = values[0]

models = SalesEngine::Persistence.instance.fetch(self)
model = models.find_all { |m| m.send(attribute) == value }
indices = SalesEngine::Persistence.instance.fetch_indices(self)

unless indices.empty?
models = indices[attribute][value] if indices[attribute]
end

unless models
models = SalesEngine::Persistence.instance.fetch(self)
model = models.find_all { |m| m.send(attribute) == value }
end
end
end
end
Expand Down
34 changes: 19 additions & 15 deletions lib/sales_engine/persistence.rb
Expand Up @@ -17,6 +17,7 @@ def persist(model)
else
@data[name] = [model]
end

true
end

Expand Down Expand Up @@ -59,6 +60,24 @@ def insert_index(attribute, class_name)
@indices[class_name][attribute] = result
end

def index_attribute_by_class(attribute, class_name)
result = {}

data = @data[class_name]

data.each do |model|
value = model.send(attribute)

if result[value]
result[value].push model
else
result[value] = [model]
end
end

result
end

def index_all
@data.keys.each do |class_name|
datum = @data[class_name]
Expand All @@ -84,20 +103,5 @@ def dump_indices
def import(data)
@data = data
end

private

def index_attribute_by_class(attribute, class_name)
result = {}

data = @data[class_name]

data.each do |model|
value = model.send(attribute)
result[value] = model
end

result
end
end
end
20 changes: 20 additions & 0 deletions spec/merchant_spec.rb
Expand Up @@ -3,6 +3,8 @@
describe SalesEngine::Merchant do
let(:merchant) { Fabricate(:merchant) }

before(:all) { SalesEngine.startup('data/evaluation') }

it 'creates a merchant with valid attributes' do
merchant.nil?.should be_false
end
Expand Down Expand Up @@ -100,4 +102,22 @@
end
end
end

context "Business Intelligence" do
describe ".revenue" do
it "returns all revenue for a given date" do
date = Date.parse "Tue, 20 Mar 2012"

SalesEngine::Merchant.revenue(date).to_f.should be_within(0.001).of(263902466.0)
end
end

describe ".most_revenue" do
it "returns the top n revenue-earners" do
most = SalesEngine::Merchant.most_revenue(3)
most.first.name.should == "Dicki-Bednar"
most.last.name.should == "Okuneva, Prohaska and Rolfson"
end
end
end
end
2 changes: 1 addition & 1 deletion spec/persistence_spec.rb
Expand Up @@ -45,7 +45,7 @@ def initialize(attributes)
attribute = :id
persistence.index(attribute)
indices = persistence.dump_indices
indices[model.class][attribute][model.id].should be model
indices[model.class][attribute][model.id].first.should be model
end
end

Expand Down
4 changes: 2 additions & 2 deletions spec/sales_engine_spec.rb
Expand Up @@ -37,8 +37,8 @@
end

it "creates models" do
#SalesEngine.load_directory(directory)
#SalesEngine::Persistence.instance.dump_data.should_not be_empty
SalesEngine.load_directory(directory)
SalesEngine::Persistence.instance.dump_data.should_not be_empty
end
end
end

0 comments on commit e59c55c

Please sign in to comment.