Guest User

Untitled

a guest
Nov 24th, 2017
346
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. # The class FinancialReportMailer uses composition
  2. # i.e an instance of another class FinancialReportGenerator
  3. # and it's methods to implement SRP.
  4. # FinancialReportGenerator generates a report.
  5. # The report is passed to FinancialReportMailer, where method, deliver
  6. # is invoked to send the generated report
  7. class FinancialReportMailer
  8. def initialize(report, account)
  9. @report = report
  10. @account = account
  11. end
  12.  
  13. def deliver
  14. Mailer.deliver(
  15. from: 'reporter@example.com',
  16. to: @account.email,
  17. subject: 'Financial report',
  18. body: @report
  19. )
  20. end
  21. end
  22.  
  23. class FinancialReportGenerator
  24. def initialize(transactions)
  25. @transactions = transactions
  26. end
  27.  
  28. def generate
  29. @transactions.map { |t| "amount: #{t.amount} type: #{t.type} date: #{t.created_at}"
  30. }.join("\n")
  31. end
  32. end
  33.  
  34. report = FinancialReportGenerator.new(transactions).generate
  35. FinancialReportMailer.new(report, account).deliver
Add Comment
Please, Sign In to add comment