Permalink
Browse files

Extract average

  • Loading branch information...
1 parent 68f50e9 commit 43d8803ffbc973c7110d4cd664c012e4a152314f @brynary committed May 7, 2012
Showing with 6 additions and 4 deletions.
  1. +6 −4 lib/sales_engine/invoice.rb
@@ -52,9 +52,7 @@ def self.pending
end
def self.average_revenue(date = nil)
- invoices = date ? with_date(date) : all
- tr = invoices.map(&:total_paid).inject(:+)
- BigDecimal(tr / invoices.size.to_f).round(2)
+ average(:total_paid, date)
end
def self.with_date(date)
@@ -65,8 +63,12 @@ def self.with_date(date)
end
def self.average_items(date = nil)
+ average(:num_items, date)
+ end
+
+ def self.average(method_name, date = nil)
invoices = date ? with_date(date) : all
- tr = invoices.map(&:num_items).inject(:+)
+ tr = invoices.map { |i| i.send(method_name) }.inject(:+)
BigDecimal(tr / invoices.size.to_f).round(2)
end

0 comments on commit 43d8803

Please sign in to comment.