jeudi 1 août 2019

Multiple values method response patterns

I am writing service objects that do many things. A service object can look like this:

class SignUpService
  def initialize(params)
    @params = params
  end

  def call
    begin
      org = create_organization!(@params[:org_params])
      log(:org_created, org)

      team = create_team!(org)
      log(:team_created, team)

      account = create_account(team)
      log(:account_created, account)

      send_email(:welcome, account)

    rescue => e
      send_email(:error_alert, @params)
      log(:report_error, e)
    end

  end
end

The SignUpService has many collaborators and has many side effects. Since this class is typically called in a controller its return value can determine if a page re-render or redirect can happen. I'm never really comfortable sending simple objects or truthy values back. I'm wondering if there's a pattern similar to returning a tuple in elixir that I can read about. I'm interested in a pattern that will let the caller know how to proceed depending on various types of responses and the data that yielded it.

Aucun commentaire:

Enregistrer un commentaire