Skip to content

Instantly share code, notes, and snippets.

@worace
Created March 9, 2016 19:37
Show Gist options
  • Select an option

  • Save worace/9d379b8ca10060642bd6 to your computer and use it in GitHub Desktop.

Select an option

Save worace/9d379b8ca10060642bd6 to your computer and use it in GitHub Desktop.
diff --git a/Gemfile b/Gemfile
index da92010..2218160 100644
--- a/Gemfile
+++ b/Gemfile
@@ -5,3 +5,4 @@ gem 'reek'
gem 'rake'
gem 'rspec', '~> 3.1.0'
gem 'pry'
+gem "ruby-prof"
diff --git a/Gemfile.lock b/Gemfile.lock
index eb603bf..067cae2 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -57,6 +57,7 @@ GEM
rspec-mocks (3.1.3)
rspec-support (~> 3.1.0)
rspec-support (3.1.2)
+ ruby-prof (0.15.9)
slop (3.6.0)
thread_safe (0.3.5)
unparser (0.2.4)
@@ -82,6 +83,7 @@ DEPENDENCIES
rake
reek
rspec (~> 3.1.0)
+ ruby-prof
BUNDLED WITH
1.10.6
diff --git a/lib/invoice_repository.rb b/lib/invoice_repository.rb
index f34b834..d24e97b 100644
--- a/lib/invoice_repository.rb
+++ b/lib/invoice_repository.rb
@@ -1,11 +1,12 @@
require_relative "invoice"
class InvoiceRepository
- attr_reader :invoices, :engine
+ attr_reader :invoices, :engine, :by_merchant_id
def initialize(engine, raw_invoices)
@engine = engine
@invoices = load_data(raw_invoices)
+ @by_merchant_id = all.group_by(&:merchant_id)
end
def inspect
@@ -13,29 +14,39 @@ class InvoiceRepository
end
def load_data(raw_invoices)
- raw_invoices.reduce([]) do |invoices, row|
- invoices << Invoice.new(self, row.to_h)
+ # raw_invoices.reduce([]) do |invoices, row|
+ # invoices << Invoice.new(self, row.to_h)
+ # end
+
+ invoices = {}
+ raw_invoices.each do |row|
+ i = Invoice.new(self, row.to_h)
+ invoices[i.id] = i
end
+
+ invoices
end
def all
- invoices
+ invoices.values
end
def find_by_id(id)
- invoices.detect { |i| i.id == id }
+ invoices[id]
end
def find_all_by_customer_id(customer_id)
- invoices.select { |i| i.customer_id == customer_id }
+ all.select { |i| i.customer_id == customer_id }
end
def find_all_by_merchant_id(merchant_id)
- invoices.select { |i| i.merchant_id == merchant_id }
+ # "indexing"
+ by_merchant_id[merchant_id] || []
+ # invoices.select { |i| i.merchant_id == merchant_id }
end
def find_all_by_status(status)
- invoices.select { |i| i.status == status }
+ all.select { |i| i.status == status }
end
def find_merchant_by_id(merchant_id)
diff --git a/lib/item_repository.rb b/lib/item_repository.rb
index 3973145..b3f96c9 100644
--- a/lib/item_repository.rb
+++ b/lib/item_repository.rb
@@ -1,11 +1,12 @@
require_relative "item"
class ItemRepository
- attr_reader :items, :engine
+ attr_reader :items, :engine, :grouped_by_merchant_id
def initialize(engine, raw_items)
@engine = engine
@items = load_data(raw_items)
+ @grouped_by_merchant_id = items.group_by(&:merchant_id)
end
def load_data(raw_items)
@@ -35,7 +36,9 @@ class ItemRepository
end
def find_all_by_merchant_id(merchant_id)
- items.select { |item| item.merchant_id == merchant_id }
+ grouped_by_merchant_id[merchant_id] || []
+ # [Item1, Item2, Item3, Item4]
+ # items.select { |item| item.merchant_id == merchant_id }
end
def find_all_with_description(description)
diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb
index 2f59b40..366b265 100644
--- a/spec/spec_helper.rb
+++ b/spec/spec_helper.rb
@@ -25,10 +25,14 @@ module BlackThursdaySpecHelpers
end
end
+require "ruby-prof"
+
RSpec.configure do |config|
config.disable_monkey_patching!
config.before(:suite) do
+ RubyProf.start
+
BlackThursdaySpecHelpers.engine = SalesEngine.from_csv({
items: File.join('data', 'items.csv'),
merchants: File.join('data', 'merchants.csv'),
@@ -39,6 +43,16 @@ RSpec.configure do |config|
})
end
+ config.after(:suite) do
+ puts "AFTER THE TESTSSS"
+
+ result = RubyProf.stop
+
+ # printer = RubyProf::FlatPrinter.new(result)
+ printer = RubyProf::GraphHtmlPrinter.new(result)
+ printer.print(File.open("./profile.html", "w"))
+ end
+
# File.join 'data'
config.include BlackThursdaySpecHelpers
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment