diff options
author | Harald Eilertsen <haraldei@anduin.net> | 2014-11-14 16:14:03 +0100 |
---|---|---|
committer | Harald Eilertsen <haraldei@anduin.net> | 2014-11-14 16:16:22 +0100 |
commit | 057bed03018c3235fbcd3c21abd86f02f65f96c1 (patch) | |
tree | f6b77a9e86fb7e920f884f9f4de595c3839d9415 | |
parent | c4bc044881c05822a66d4f77a4ffad9368235731 (diff) | |
download | imusician-reports-057bed03018c3235fbcd3c21abd86f02f65f96c1.tar.gz imusician-reports-057bed03018c3235fbcd3c21abd86f02f65f96c1.tar.bz2 imusician-reports-057bed03018c3235fbcd3c21abd86f02f65f96c1.zip |
Reports pick up their data from the row rather than the sale object.
-rw-r--r-- | lib/reports/sales_by_country.rb | 23 | ||||
-rw-r--r-- | lib/reports/sales_by_date.rb | 12 | ||||
-rw-r--r-- | lib/reports/sales_by_shop.rb | 23 | ||||
-rw-r--r-- | lib/sale.rb | 6 | ||||
-rw-r--r-- | parse_report.rb | 6 |
5 files changed, 37 insertions, 33 deletions
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 "------------------------------------------" diff --git a/lib/reports/sales_by_date.rb b/lib/reports/sales_by_date.rb index 4bc9094..b2d58df 100644 --- a/lib/reports/sales_by_date.rb +++ b/lib/reports/sales_by_date.rb @@ -1,10 +1,18 @@ module SalesReporter module Reports class SalesByDate - def render(sales) + def initialize + @sales = [] + end + + def add_sale(sale, row) + @sales << sale + end + + def render sales_by_date = {} - sales.each do |s| + @sales.each do |s| d = s.date if sales_by_date[d] sales_by_date[d][:count] += s.quantity diff --git a/lib/reports/sales_by_shop.rb b/lib/reports/sales_by_shop.rb index 66caf63..2c8a70b 100644 --- a/lib/reports/sales_by_shop.rb +++ b/lib/reports/sales_by_shop.rb @@ -1,20 +1,19 @@ module SalesReporter module Reports class SalesByShop - def render(sales) - sales_by_shop = {} + def initialize + @shops = Hash.new { |h,k| h[k] = { :count => 0, :total => 0, :sales => [] } } + end - sales.each do |s| - shop = s.shop - if sales_by_shop[shop] - sales_by_shop[shop][:count] += s.quantity - sales_by_shop[shop][:total] += s.amount - else - sales_by_shop[shop] = {:count => s.quantity, :total => s.amount} - end - end + def add_sale(sale, row) + shop = row['Shop'] + @shops[shop][:sales] << sale + @shops[shop][:count] += sale.quantity + @shops[shop][:total] += sale.amount + end - sorted = sales_by_shop.sort { |a,b| b[1][:count] <=> a[1][:count] } + def render + sorted = @shops.sort { |a,b| b[1][:count] <=> a[1][:count] } puts "Shop: Streams: Revenue:" puts "------------------------------------------" diff --git a/lib/sale.rb b/lib/sale.rb index 17c6f18..f02a471 100644 --- a/lib/sale.rb +++ b/lib/sale.rb @@ -1,14 +1,12 @@ module SalesReporter class Sale - attr_reader :date, :amount, :quantity, :country, :shop + attr_reader :date, :amount, :quantity - def initialize(date_, amount, quantity, country, shop) + def initialize(date_, amount, quantity) @date = sanitize_date(date_) raise TypeError('sanitize failed') unless @date.is_a? Date @amount = amount @quantity = quantity - @country = country - @shop = shop end private diff --git a/parse_report.rb b/parse_report.rb index 48b7c71..7af1a82 100644 --- a/parse_report.rb +++ b/parse_report.rb @@ -28,11 +28,11 @@ end reports = load_reports -sales = [] parse_csv(ARGV[0]) do |row| - sales << SalesReporter::Sale.new(row["Date"], row["revenue EUR"], row["quantity"], row["Country"], row["Shop"]) + sale = SalesReporter::Sale.new(row["Date"], row["revenue EUR"], row["quantity"]) + reports.each { |r| r.add_sale(sale, row) } end reports.each do |report| - report.render(sales) + report.render end |