Localize.PhoenixRuntime.Attrs (localize_phoenix_runtime v0.1.0-alpha.1)

View Source

Provides an interface to access and update Localize attributes in sockets, or connections (hereinafter containers).

Summary

Functions

Retrieves the value for key from the container's attributes, or returns default.

Retrieves the value for key from the container's attributes.

Merges the given value into the container's attributes.

Returns true if the given key or attribute tuple represents a private attribute.

Replaces the container's attributes with the provided map.

Assigns value to key in the container's attributes.

Updates the container's attributes by applying the given function.

Updates the value assigned to key in the container's attributes by applying the given function.

Types

attrs_fun()

@type attrs_fun() :: (map() -> Enumerable.t())

container()

@type container() :: Phoenix.Socket.t() | Phoenix.LiveView.Socket.t() | Plug.Conn.t()

key()

@type key() :: atom()

t()

@type t() :: %{optional(key()) => value()}

update_fun()

@type update_fun() :: (value() -> value())

value()

@type value() :: any()

Functions

get(sock_or_conn, key \\ nil, default \\ nil)

@spec get(container(), key() | nil, value() | map()) :: value() | map()

Retrieves the value for key from the container's attributes, or returns default.

When no key is provided, returns the entire attributes map.

get!(sock_or_conn, key, error_msg \\ nil)

@spec get!(container(), key(), String.t() | nil) :: value() | no_return()

Retrieves the value for key from the container's attributes.

Raises an error (with an optional custom message) if the key is not found.

merge(sock_or_conn, value)

@spec merge(container(), keyword() | map()) :: container()

Merges the given value into the container's attributes.

The value can be either a list of key-value pairs or a map.

private?(key)

@spec private?({atom(), any()} | atom()) :: boolean()

Returns true if the given key or attribute tuple represents a private attribute.

A private attribute is one whose name starts with "__".

put(sock_or_conn, value)

@spec put(container(), map()) :: container()

Replaces the container's attributes with the provided map.

put(sock_or_conn, key, value)

@spec put(container(), key(), value()) :: container()

Assigns value to key in the container's attributes.

update(sock_or_conn, fun)

@spec update(container(), attrs_fun()) :: container()

Updates the container's attributes by applying the given function.

The function receives the current attributes map and must return an enumerable, which is then converted into a new map.

update(sock_or_conn, key, fun)

@spec update(container(), key(), update_fun()) :: container()

Updates the value assigned to key in the container's attributes by applying the given function.