From 057bed03018c3235fbcd3c21abd86f02f65f96c1 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Fri, 14 Nov 2014 16:14:03 +0100 Subject: Reports pick up their data from the row rather than the sale object. --- lib/reports/sales_by_country.rb | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) (limited to 'lib/reports/sales_by_country.rb') diff --git a/lib/reports/sales_by_country.rb b/lib/reports/sales_by_country.rb index 4017dbf..6729ba6 100644 --- a/lib/reports/sales_by_country.rb +++ b/lib/reports/sales_by_country.rb @@ -1,20 +1,19 @@ module SalesReporter module Reports class SalesByCountry - def render(sales) - sales_by_country = {} + def initialize + @countries = Hash.new { |h,k| h[k] = { :count => 0, :total => 0, :sales => [] } } + end - sales.each do |s| - c = s.country - if sales_by_country[c] - sales_by_country[c][:count] += s.quantity - sales_by_country[c][:total] += s.amount - else - sales_by_country[c] = {:count => s.quantity, :total => s.amount} - end - end + def add_sale(sale, row) + c = row['Country'] + @countries[c][:sales] << sale + @countries[c][:count] += sale.quantity + @countries[c][:total] += sale.amount + end - sorted = sales_by_country.sort { |a,b| b[1][:count] <=> a[1][:count] } + def render + sorted = @countries.sort { |a,b| b[1][:count] <=> a[1][:count] } puts "Country: Streams: Revenue:" puts "------------------------------------------" -- cgit v1.2.3