CI Hex.pm Docs Total Download License

MaxoAdapt generates adapter facades from ordinary Elixir callbacks. The generated functions retain callback documentation and specs, validate implementations, and can use either direct delegates or runtime lookup.

MaxoAdapt is derived from Ian Luites' adapter library, with additional fixes and runtime modes.

Why use it?

  • Fast: :compile and :get_compiled dispatch through direct delegates.
  • Small: the package has no runtime dependencies beyond Logger.
  • Safe: required callback name/arity pairs are validated before mutable configuration.
  • Explicit: the behaviour and generated facade live in one module.
  • Flexible: choose immutable compile-time configuration, global runtime configuration, or process-isolated test configuration.

Installation

def deps do
  [
    {:maxo_adapt, "~> 0.1"}
  ]
end

MaxoAdapt requires Elixir 1.14 or later.

Quick start

defmodule SessionRepo do
  use MaxoAdapt

  behaviour do
    @doc "Look up a session by token."
    @callback get(token :: binary()) :: {:ok, term() | nil} | {:error, atom()}
  end

  @spec get!(binary()) :: term() | nil
  def get!(token) do
    case get(token) do
      {:ok, result} -> result
      {:error, reason} -> raise "SessionRepo: #{reason}"
    end
  end
end

defmodule SessionRepo.PostgreSQL do
  @behaviour SessionRepo

  @impl SessionRepo
  def get(token), do: {:ok, {token, :postgresql}}
end

:ok = SessionRepo.configure(SessionRepo.PostgreSQL)
{:ok, {"token", :postgresql}} = SessionRepo.get("token")

Modes

ModeLookupRuntime switchingTypical use
:compile (default)regenerated direct delegatesyesfast production dispatch
:get_compiledApplication.compile_env/3 + direct delegatesnoimmutable compile-time choice
:get_envApplication.get_env/3 per callyessimple global runtime switching
:get_dictprocess dictionary + live ancestorsyesasync test isolation

Call YourAdapter.__maxo_adapt__/0 to inspect the effective implementation.

Guides

Published API documentation is available at https://hexdocs.pm/maxo_adapt.

Support

Sponsored by Quantor Consulting

License

MaxoAdapt is available under the MIT License.