collectiveidea / interactor

Interactor provides a common interface for performing complex user interactions.
MIT License
3.36k stars 212 forks source link

Use `#call` instead of `#perform` #45

Closed ersatzryan closed 10 years ago

ersatzryan commented 10 years ago

Interactor's primary public API method is the perform class method. Why perform? Good question. The answer is because we had to pick something.

The suggestion has been made to alias all of these methods and leave it to personal preference. There are two problems with that suggestion:

  1. Interactor's purpose is to provide convention around a simple pattern. To dilute the API with multiple approaches to its most basic function would weaken the convention.
  2. While aliases may work on the class level from which the interactor is called, the developer still must define an instance method to describe the interactor's actual behavior. Aliasing an arbitrary number of methods in a way that can determine which method the developer actually defined (overwrote) muddies the waters and adds a great deal of complexity.

    Proposed Solution

On both the class level and the instance level, interactors will use the call method rather than perform, because… Ruby. The call method is a common Ruby convention for method objects. It also has nice symmetry with Ruby's procs and lambdas.

Fun argument here.

class SessionsController < ApplicationController
  def create
    AuthenticateUser.call(email: params[:email], password: params[:password])
  end
end

class AuthenticateUser
  include Interactor

  def call
    # TODO
  end
end
errinlarsen commented 10 years ago

:+1:

trestrantham commented 10 years ago

I am not in favor of using call specifically because it allows procs if for no other reason than I want Interactor to be dependably serializable. I also agree with @jasonroelofs's point from the fun argument.

zmoazeni commented 10 years ago

I was looking at the code to submit a PR for this. Do you want to keep around the perform! concept?

laserlemon commented 10 years ago

Yeah, I think call! is useful for running interactors in environments where failure should be loud, like in a background process.

laserlemon commented 10 years ago

Included in the v3 branch.