Ash.Subject (ash v3.5.32)

View Source

Provides a consistent API for common operations across Ash.Changeset, Ash.Query, and Ash.ActionInput. It allows you to write generic code that works with any of these types without needing to pattern match or special-case your logic.

Summary

Functions

Adds an error or list of errors to the subject.

Adds a callback to be executed after the action.

Adds a callback to be executed before the action.

Deletes one or more arguments from the subject.

Fetches an argument value from the subject.

Gets an argument value from the subject.

Gets an argument or attribute value from a Changeset, or just an argument from other subjects.

Puts a key-value pair into the subject's context.

Sets a single argument on the subject.

Sets multiple arguments on the subject.

Sets the context for the subject.

Sets a private argument on the subject.

Sets multiple private arguments on the subject.

Types

Functions

add_error(subject, error)

@spec add_error(t(), Ash.Error.error_input() | [Ash.Error.error_input()]) :: t()

Adds an error or list of errors to the subject.

Supports all subject types (Changeset, Query, ActionInput) and maintains type consistency.

Parameters

  • subject - The subject to add errors to
  • errors - Error or list of errors to add

after_action(subject, callback, opts \\ [])

@spec after_action(
  t(),
  (t(), term() -> {:ok, term()} | {:error, term()}),
  Keyword.t()
) :: t()

Adds a callback to be executed after the action.

Note: Query only supports 2-arity callbacks and ignores opts.

Parameters

  • subject - The subject to add callback to
  • callback - Function that processes the result
  • opts - Options including :prepend? (ignored for Query)

before_action(subject, callback, opts \\ [])

@spec before_action(t(), (t() -> t()), Keyword.t()) :: t()

Adds a callback to be executed before the action.

Parameters

  • subject - The subject to add callback to
  • callback - Function that takes and returns the subject
  • opts - Options including :prepend? to add at beginning

delete_argument(subject, argument_or_arguments)

@spec delete_argument(t(), atom() | binary() | [atom() | binary()]) :: t()

Deletes one or more arguments from the subject.

Parameters

  • subject - The subject to delete arguments from
  • arguments - Single argument name or list of argument names to delete

fetch_argument(subject, argument)

@spec fetch_argument(t(), atom() | binary()) :: {:ok, term()} | :error

Fetches an argument value from the subject.

Returns {:ok, value} if the argument exists, :error otherwise. Supports both atom and string argument names.

Parameters

  • subject - The subject to fetch argument from
  • argument - The argument name (atom or string)

get_argument(subject, argument, default \\ nil)

Gets an argument value from the subject.

Supports both atom and string argument names.

Parameters

  • subject - The subject to get argument from
  • argument - The argument name (atom or string)

get_argument_or_attribute(subject, argument_or_attribute, default \\ nil)

Gets an argument or attribute value from a Changeset, or just an argument from other subjects.

For Changesets, this can retrieve both arguments and attributes. For Query and ActionInput, this only retrieves arguments.

Parameters

  • subject - The subject to get value from
  • name - The argument or attribute name (atom or string)

get_attribute(subject, attribute)

@spec get_attribute(t(), atom()) :: term()

put_context(subject, key, value)

@spec put_context(t(), atom(), term()) :: t()

Puts a key-value pair into the subject's context.

Parameters

  • subject - The subject to update context on
  • key - The context key
  • value - The value to store

set_argument(subject, argument, value)

@spec set_argument(t(), atom() | binary(), term()) :: t()

Sets a single argument on the subject.

Parameters

  • subject - The subject to set argument on
  • argument - The argument name (atom or string)
  • value - The value to set

set_arguments(subject, map)

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

Sets multiple arguments on the subject.

Takes a map of argument names to values and sets them all.

Parameters

  • subject - The subject to set arguments on
  • arguments - Map of argument names to values

set_context(subject, map)

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

Sets the context for the subject.

Merges the provided map into the subject's existing context. For Changeset and Query, delegates to their specific implementations.

Parameters

  • subject - The subject to set context on
  • context - Map of context data to merge

set_private_argument(subject, argument, value)

@spec set_private_argument(
  Ash.Changeset.t() | Ash.ActionInput.t(),
  atom() | binary(),
  term()
) ::
  Ash.Changeset.t() | Ash.ActionInput.t()

Sets a private argument on the subject.

Private arguments are not exposed in the public API. Only supported by Changeset and ActionInput.

Parameters

  • subject - The subject to set private argument on (Changeset or ActionInput)
  • argument - The argument name (atom or string)
  • value - The value to set

set_private_arguments(subject, map)

@spec set_private_arguments(Ash.Changeset.t() | Ash.ActionInput.t(), map()) ::
  Ash.Changeset.t() | Ash.ActionInput.t()

Sets multiple private arguments on the subject.

Takes a map of argument names to values and sets them all as private arguments. Only supported by Changeset and ActionInput.

Parameters

  • subject - The subject to set private arguments on (Changeset or ActionInput)
  • arguments - Map of argument names to values