ruby 单一责任原则违规

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby 单一责任原则违规相关的知识,希望对你有一定的参考价值。

# This class violates single responsibilty principle
# since it handles 2 tasks i.e generating a report
# (generate_report) and sending a report (send_report)
class FinancialReportMailer
  def initialize(transactions, account)
    @transactions = transactions
    @account = account
    @report = ''
  end

  def generate_report!
    @report = @transactions.map {
      |t| "amount: #{t.amount} type: #{t.type} date: #{t.created_at}"
    }.join("\n")
  end

  def send_report
    Mailer.deliver(
      from: 'reporter@example.com',
      to: @account.email,
      subject: 'your report',
      body: @report
    )
  end
end

mailer = FinancialReportMailer.new(transactions, account)
mailer.generate_report!
mailer.send_report

以上是关于ruby 单一责任原则违规的主要内容,如果未能解决你的问题,请参考以下文章