From c4bc044881c05822a66d4f77a4ffad9368235731 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Fri, 14 Nov 2014 15:35:02 +0100 Subject: Refactor code, use blocks instead of returning arrays. --- parse_report.rb | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/parse_report.rb b/parse_report.rb index 9a2340c..48b7c71 100644 --- a/parse_report.rb +++ b/parse_report.rb @@ -9,35 +9,30 @@ def parse_csv(csv_file) :converters => :all, } - sales = [] - CSV.foreach(csv_file, csv_opts) do |row| if row.count > 0 - sale = SalesReporter::Sale.new(row["Date"], row["revenue EUR"], row["quantity"], row["Country"], row["Shop"]) - sales << sale + yield row end end - - sales end def load_reports - reports = [] - Dir[File.join('lib', 'reports', '*.rb')].each do |f| require_relative f end - SalesReporter::Reports.constants.each do |report| - reports << SalesReporter::Reports.const_get(report).new + SalesReporter::Reports.constants.map do |report| + SalesReporter::Reports.const_get(report).new end - - reports end -sales = parse_csv(ARGV[0]) 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"]) +end + reports.each do |report| report.render(sales) end -- cgit v1.2.3