Trogon.Commanded.Enum (Trogon.Commanded v1.0.0)

Copy Markdown View Source

Defines an Enum type module.

Summary

Functions

Converts the module into a struct with an :value enum field.

Functions

__using__(opts)

(macro)

Converts the module into a struct with an :value enum field.

Using

Derives

Usage

defmodule BankAccountType do
  use Trogon.Commanded.Enum, values: [:business, :personal]
end

{:ok, type} = BankAccountType.new(:business)

You can use it in your Ecto.Schema like this:

defmodule BankAccount do
  use Ecto.Schema

  embedded_schema do
    field :type, BankAccountType
  end
end

Proto-driven Enum

You can derive values from a protobuf enum module instead of a hardcoded list:

defmodule ObjectTypeEnum do
  use Trogon.Commanded.Enum,
    proto: Acme.Type.V1.ObjectType
end

The enum values are derived at compile time, sorted by their proto field number.