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:
:compileand:get_compileddispatch 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"}
]
endMaxoAdapt 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
| Mode | Lookup | Runtime switching | Typical use |
|---|---|---|---|
:compile (default) | regenerated direct delegates | yes | fast production dispatch |
:get_compiled | Application.compile_env/3 + direct delegates | no | immutable compile-time choice |
:get_env | Application.get_env/3 per call | yes | simple global runtime switching |
:get_dict | process dictionary + live ancestors | yes | async 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
License
MaxoAdapt is available under the MIT License.