Axn.Context (Axn v0.2.0)

View Source

Context struct that flows through the step pipeline, carrying request data, user information, and any step-added fields. Provides helper functions similar to Plug.Conn and Phoenix.Component.

Summary

Functions

Assigns multiple values from a map or keyword list to the context assigns.

Assigns a value to a key in the context assigns.

Gets a value from the private storage of the context.

Gets a value from the private storage of the context with a default.

Updates the params in the context.

Puts a value in the private storage of the context.

Updates the result in the context.

Types

t()

@type t() :: %Axn.Context{
  action: atom() | nil,
  assigns: map(),
  module: module() | nil,
  params: map(),
  private: map(),
  result: any()
}

Functions

assign(ctx, assigns)

@spec assign(t(), map() | keyword()) :: t()

Assigns multiple values from a map or keyword list to the context assigns.

Examples

iex> ctx = %Axn.Context{}
iex> updated_ctx = Axn.Context.assign(ctx, %{current_user: %{id: 123}, theme: "dark"})
iex> updated_ctx.assigns
%{current_user: %{id: 123}, theme: "dark"}

iex> ctx = %Axn.Context{}
iex> updated_ctx = Axn.Context.assign(ctx, current_user: %{id: 123}, theme: "dark")
iex> updated_ctx.assigns
%{current_user: %{id: 123}, theme: "dark"}

assign(ctx, key, value)

@spec assign(t(), atom(), any()) :: t()

Assigns a value to a key in the context assigns.

Examples

iex> ctx = %Axn.Context{}
iex> updated_ctx = Axn.Context.assign(ctx, :current_user, %{id: 123})
iex> updated_ctx.assigns
%{current_user: %{id: 123}}

get_private(ctx, key)

@spec get_private(t(), atom()) :: any()

Gets a value from the private storage of the context.

Examples

iex> ctx = %Axn.Context{private: %{correlation_id: "abc123"}}
iex> Axn.Context.get_private(ctx, :correlation_id)
"abc123"

iex> ctx = %Axn.Context{}
iex> Axn.Context.get_private(ctx, :non_existent)
nil

get_private(ctx, key, default)

@spec get_private(t(), atom(), any()) :: any()

Gets a value from the private storage of the context with a default.

Examples

iex> ctx = %Axn.Context{private: %{correlation_id: "abc123"}}
iex> Axn.Context.get_private(ctx, :correlation_id, "default")
"abc123"

iex> ctx = %Axn.Context{}
iex> Axn.Context.get_private(ctx, :non_existent, "my_default")
"my_default"

put_params(ctx, params)

@spec put_params(t(), map()) :: t()

Updates the params in the context.

Examples

iex> ctx = %Axn.Context{}
iex> updated_ctx = Axn.Context.put_params(ctx, %{name: "John", age: 25})
iex> updated_ctx.params
%{name: "John", age: 25}

put_private(ctx, key, value)

@spec put_private(t(), atom(), any()) :: t()

Puts a value in the private storage of the context.

Examples

iex> ctx = %Axn.Context{}
iex> updated_ctx = Axn.Context.put_private(ctx, :correlation_id, "abc123")
iex> updated_ctx.private
%{correlation_id: "abc123"}

put_result(ctx, result)

@spec put_result(t(), any()) :: t()

Updates the result in the context.

Examples

iex> ctx = %Axn.Context{}
iex> updated_ctx = Axn.Context.put_result(ctx, {:ok, %{id: 123}})
iex> updated_ctx.result
{:ok, %{id: 123}}