defmodule SuperPlug do @moduledoc """ Give your Plug superpowers! ## Primary Superpower `call/1` initializes default options. iex> conn = %{assigns: %{}} ...> FooPlug.call(conn) %{assigns: %{barred?: false}} ## Secondary Superpower `call/2` initializes keyword options. iex> conn = %{assigns: %{}} ...> FooPlug.call(conn, bar?: true) %{assigns: %{barred?: true}} ## Tertiary Superpower `call/2` short circuits if an error is assigned. iex> conn = %{assigns: %{error: :not_found}} ...> FooPlug.call(conn) %{assigns: %{error: :not_found}} """ defmacro __using__(_opts) do quote do # 1. `call/1` initializes default options. def call(conn) do call(conn, init([])) end # 2. `call/2` initializes keyword options. def call(conn, opts) when is_list(opts) do call(conn, init(opts)) end # 3. `call/2` short circuits if an error is assigned. def call(conn = %{assigns: %{error: _}}, _) do conn end end end end