interactor v0.1.0 Interactor behaviour

A tool for modeling events that happen in your application.

TODO: More on interactor concept

Interactor provided a behaviour and functions to execute the behaviours.

To use simply use Interactor in a module and implement the handle_call/1 callback. When use-ing you can optionaly include a Repo option which will be used to execute any Ecto.Changesets or Ecto.Multi structs you return.

Interactors supports three callbacks:

  • before_call/1 - Useful for manipulating input etc.
  • handle_call/1 - The meat, usually returns an Ecto.Changeset or Ecto.Multi.
  • after_call/1 - Useful for metrics, publishing events, etc

Interactors can be called in three ways:

  • Interactor.call/2 - Executes callbacks, optionaly insert, and return results.
  • Interactor.call_task/2 - Same as call, but returns a Task that can be awated on.
  • Interactor.call_aysnc/2 - Same as call, but does not return results.

Example:

defmodule CreateArticle do
  use Interactor, repo: Repo

  def handle_call(%{attributes: attrs, author: author}) do
    cast(%Article{}, attrs, [:title, :body])
    |> put_change(:author_id, author.id)
  end
end

Interactor.call(CreateArticle, %{attributes: params, author: current_user})

Summary

Functions

Executes the before_call/1, handle_call/1, and after_call/1 callbacks

Executes call/2 asynchronously via a supervised task. Returns {:ok, pid}

Wraps call/2 in a supervised Task. Returns the Task

Callbacks

A callback executed after handle_call and after the Repo executes

A callback executed before handle_call. Useful for normalizing inputs

The primary callback. Typically returns an Ecto.Changeset or an Ecto.Multi

Functions

call(interactor, context)

Executes the before_call/1, handle_call/1, and after_call/1 callbacks.

If an Ecto.Changeset or Ecto.Multi is returned by handle_call/1 and a repo options was passed to use Interactor the changeset or multi will be executed and the results returned.

call_async(interactor, map)

Specs

call_async(module, map) :: {:ok, pid}

Executes call/2 asynchronously via a supervised task. Returns {:ok, pid}.

Primary use case is task you want completely asynchronos with no care for return values.

call_task(interactor, map)

Specs

call_task(module, map) :: Task.t
call_task(module, map) :: Task.t

Wraps call/2 in a supervised Task. Returns the Task.

Useful if you want async, but want to await results.

Callbacks

after_call(any)

Specs

after_call(any) :: any

A callback executed after handle_call and after the Repo executes.

Useful for publishing events, tracking metrics, and other non-transaction worthy calls.

before_call(map)

Specs

before_call(map) :: map

A callback executed before handle_call. Useful for normalizing inputs.

handle_call(map)

Specs

handle_call(map) :: any

The primary callback. Typically returns an Ecto.Changeset or an Ecto.Multi.