Enumex.Static (Enumex v1.0.0)
View SourceThe module provides a foundation for static enum definitions, based on the enum values that are known at compile time, allowing you to work with fixed sets of values that remain constant throughout your application's runtime.
Covers the Simple Approach by default,
providing Elixir-side validations for databases like SQLite
.
However, when used alongside Enumex.Static.Migration
and Enumex.Static.Adapter
,
it supports also the Advanced Approach
by performing database-level validations in databases like PostgreSQL
.
Auto-increment
When defining enum without explicit indexes,
values are automatically assigned sequential indexes starting from start_index
.
defmodule MyApp.MyEnums do
use Enumex.Static
# Generates indexes 1 and 2
enum :enum_name, [:first, :second]
# Generates indexes 3 and 4
enum :enum_name, [start_index: 3], [:third, :fourth]
end
See HTTP status codes example.
Summary
Main
The type representing a data used in component bindings for static enum definitions.
Defines the enum using DSL values format or Block format.
Type representing the possible value formats used in the DSL. It ensures type safety for enum definitions while giving flexibility in how values are specified.
Defines a single enum value within an enum block.
Main
@type comp_binding_data() :: {:enum_opts, [{Enumex.Value.enum_name(), keyword()}]} | {:grouped_values, [{Enumex.Value.enum_name(), [Enumex.Value.t()]}]} | {:ids, [{Enumex.Value.enum_name() | [Enumex.Value.id()]}]} | {:indexes, [{Enumex.Value.enum_name() | [Enumex.Value.index()]}]} | {:values, [Enumex.Value.t()]}
The type representing a data used in component bindings for static enum definitions.
@spec enum( Enumex.macro_input(Enumex.Value.enum_name()), Enumex.macro_input(Enumex.Value.opts()), enum_dsl_values() | Macro.input() ) :: Macro.output()
Defines the enum using DSL values format or Block format.
Arguments
name
: The atom representing the enum nameenum_opts
: Optional keyword list of options that could be used in componentsvalues
: List of values in various formats
DSL values format
defmodule MyApp.MyEnums do
enum :values_list, [:first, second: 2]
enum :values_list_with_options, [some_option: :value], [:first, second: 2]
enum :values_map, %{first: 1, second: 2}
enum :values_map_with_options, [some_option: :value], %{first: 1, second: 2}
end
See enum DSL values for a complete list of all supported value formats.
Block Format
defmodule MyApp.MyEnums do
enum :enum_with_block do
value :first
value :second, 2
end
end
@type enum_dsl_values() :: Enumex.macro_input( [Enumex.Value.id() | {Enumex.Value.id(), Enumex.Value.index()}] | %{required(Enumex.Value.id()) => Enumex.Value.index()} )
Type representing the possible value formats used in the DSL. It ensures type safety for enum definitions while giving flexibility in how values are specified.
Formats
- List of ids:
[:first, :second]
- Keyword list with a value id and index:
[first: 1, second: 2]
- Mixed list:
[:first, second: 2]
- Map:
%{first: 1, second: 2}
@spec value( Enumex.macro_input(Enumex.Value.id()), Enumex.macro_input(Enumex.Value.index()), Enumex.macro_input(Enumex.Value.opts()) ) :: Macro.output()
Defines a single enum value within an enum block.
Arguments
id
: The atom representing id of the enum valueindex
: The integer representing index of the enum valueopts
: The keyword representing additional options for the enum value.
Examples
defmodule MyApp.MyEnums do
enum :enum_name do
value :first
value :second, 2
end
end