View Source Rivet.Utils.Callbacks (rivet_utils v2.0.1)

Contributor: Brandon Gillespie

Summary

Functions

iex> do_callback(%{callbacks: nil}, nil, nil)
:error
iex> do_callback(%{callbacks: %{mine: fn _, x -> {:ok, x} end}}, :mine, :narf)
{:ok, :narf}
iex> do_callback(%{callbacks: %{not: fn _, x -> x end}}, :diff, :narf)
:error
iex> do_callback(nil, :diff, :narf)
:error
iex> %{red: func} = init_callbacks([:red])
%{red: func}
iex> init_callbacks(nil)
nil
iex> %{thisone: func} = put_callback(%{}, :thisone)
...> is_function(func)
true
iex> take_callbacks(%{callbacks: %{one: 1, two: 2}}, [:two])
%{two: 2}
iex> take_callbacks(%{callbacks: %{one: 1, two: 2}}, [])
nil

Functions

Link to this function

do_callback(callbacks, a, b)

View Source
@spec do_callback(%{callbacks: map()} | map(), atom(), term()) ::
  :error | {:ok, term()}
iex> do_callback(%{callbacks: nil}, nil, nil)
:error
iex> do_callback(%{callbacks: %{mine: fn _, x -> {:ok, x} end}}, :mine, :narf)
{:ok, :narf}
iex> do_callback(%{callbacks: %{not: fn _, x -> x end}}, :diff, :narf)
:error
iex> do_callback(nil, :diff, :narf)
:error
iex> %{red: func} = init_callbacks([:red])
%{red: func}
iex> init_callbacks(nil)
nil
Link to this function

put_callback(callbacks, key)

View Source
@spec put_callback(map(), atom()) :: map()
iex> %{thisone: func} = put_callback(%{}, :thisone)
...> is_function(func)
true
Link to this function

take_callbacks(map, only)

View Source
@spec take_callbacks(map(), list()) :: map()
iex> take_callbacks(%{callbacks: %{one: 1, two: 2}}, [:two])
%{two: 2}
iex> take_callbacks(%{callbacks: %{one: 1, two: 2}}, [])
nil