Enumex.Dynamic.Components.Context behaviour (Enumex v1.0.0)

View Source

Provides a complete set of CRUD operations defining Phoenix context functions for dynamic enum.

Dependencies

Requires Ecto.Repo module from :ecto dependency as well as EctoChangeset and EctoSchema component.

Required bindings

  • repo - the ecto repository module.

Usage

The component is used within an Enumex.Dynamic module:

defmodule MyApp.MyEnums do
  use Enumex.Dynamic, components: [{Enumex.Dynamic.Components.Context, repo: MyApp.Repo}]

  # enum definitions goes here
end

Summary

Types

Type representing a result of the schema modification operations. It provides type safety for handling both successful and failed modifications.

Callbacks

Changes an enum schema with the given attributes and returns a changeset that can be used for validation and persistence.

Creates a new enum schema with the given attributes and returns {:ok, schema} if successful, or {:error, changeset} if validation fails.

Deletes an enum schema from the database and returns {:ok, schema} if successful, or {:error, changeset} if deletion fails.

Retrieves an enum schema by value id and Returns the enum schema if found, or raises an error if not found.

Retrieves all enum schemas and returns a list of enum schemas, or raises an error if the operation fails.

Updates an existing enum schema with the given attributes and returns {:ok, schema} if successful, or {:error, changeset} if validation fails.

Types

schema_modification_result()

@type schema_modification_result() ::
  {:ok, Ecto.Schema.schema()} | {:error, Ecto.Changeset.t()}

Type representing a result of the schema modification operations. It provides type safety for handling both successful and failed modifications.

Example

case result do
  {:ok, schema} -> schema
  {:error, changeset} -> handle_errors(changeset)
end

Callbacks

change(schema, map)

@callback change(Ecto.Schema.schema(), map()) :: Ecto.Changeset.t()

Changes an enum schema with the given attributes and returns a changeset that can be used for validation and persistence.

Parameters

  • schema: The enum schema to modify
  • attrs: A map of attributes to change

Example

change(enum_schema, %{id: "first", index: 1})

create(map)

@callback create(map()) :: schema_modification_result()

Creates a new enum schema with the given attributes and returns {:ok, schema} if successful, or {:error, changeset} if validation fails.

Parameters

  • attrs: A map of attributes to create the enum context with

Example

case create(%{id: "first", index: 1}) do
  {:ok, enum_schema} -> enum_schema
  {:error, changeset} -> handle_errors(changeset)
end

delete(schema)

Deletes an enum schema from the database and returns {:ok, schema} if successful, or {:error, changeset} if deletion fails.

Parameters

  • schema: The enum schema to delete

Example

case delete(enum_schema) do
  {:ok, enum_schema} -> enum_schema
  {:error, changeset} -> handle_errors(changeset)
end

get!(any)

@callback get!(any()) :: Ecto.Schema.schema() | no_return()

Retrieves an enum schema by value id and Returns the enum schema if found, or raises an error if not found.

Parameters

  • id: The id of the enum schema to retrieve

Example

enum_schema = get!("first")

list()

@callback list() :: [Ecto.Schema.schema()] | no_return()

Retrieves all enum schemas and returns a list of enum schemas, or raises an error if the operation fails.

Example

list()

update(schema, map)

@callback update(Ecto.Schema.schema(), map()) :: schema_modification_result()

Updates an existing enum schema with the given attributes and returns {:ok, schema} if successful, or {:error, changeset} if validation fails.

Parameters

  • schema: The enum schema to update
  • attrs: A map of attributes to change

Example

case update(enum_schema, %{id: "second", index: 2}) do
  {:ok, updated_enum_schema} -> updated_enum_schema
  {:error, changeset} -> handle_errors(changeset)
end