View Source Moar.Sugar (Moar v1.60.0)

Syntactic sugar functions.

These functions are intended to be used by importing the functions or the whole module:

import Moar.Sugar, only: [noreply: 1]

def handle_event("foo", _params, socket) do
  socket |> assign(foo: "bar") |> noreply()
end

Summary

Functions

Wraps a term in an :error tuple. Useful in pipelines.

Unwraps an :error tuple, raising if the term is not an :error tuple.

Wraps a term in a :noreply tuple. Useful in pipelines.

Wraps a term in an :ok tuple. Useful in pipelines.

Unwraps an :ok tuple, raising if the term is not an :ok tuple.

Accepts two arguments and returns the second.

Functions

@spec error(term()) :: {:error, term()}

Wraps a term in an :error tuple. Useful in pipelines.

iex> %{} |> Map.put(:count, "unknown") |> Moar.Sugar.error()
{:error, %{count: "unknown"}}
@spec error!({:error, term()}) :: term()

Unwraps an :error tuple, raising if the term is not an :error tuple.

iex> {:error, 1} |> Moar.Sugar.error!()
1
@spec noreply(term()) :: {:noreply, term()}

Wraps a term in a :noreply tuple. Useful in pipelines.

iex> %{} |> Map.put(:count, 0) |> Moar.Sugar.noreply()
{:noreply, %{count: 0}}
@spec ok(term()) :: {:ok, term()}

Wraps a term in an :ok tuple. Useful in pipelines.

iex> %{} |> Map.put(:count, 10) |> Moar.Sugar.ok()
{:ok, %{count: 10}}
@spec ok!({:ok, term()}) :: term()

Unwraps an :ok tuple, raising if the term is not an :ok tuple.

iex> {:ok, 1} |> Moar.Sugar.ok!()
1
Link to this function

returning(first, second)

View Source
@spec returning(any(), any()) :: any()

Accepts two arguments and returns the second.

Useful at the end of the pipeline when you want to return a different value than the last result of the pipeline, such as when the pipeline has side effects.

iex> %{} |> Map.put(:count, 20) |> Moar.Sugar.returning(:count_updated)
:count_updated