FormBuilderDSL.EnumRegistry (form_builder_dsl v0.1.2)
View SourceRuntime-safe enum registry and utilities using ETS.
This module provides a way to register and manage enums in your application, ensuring they can be safely accessed at both compile-time and runtime. It uses ETS (Erlang Term Storage) for efficient storage and retrieval of enum values.
Usage
First, ensure the enum registry is started in your application's supervision tree:
# In your application.ex
children = [
FormBuilderDSL.EnumRegistry
]
Then you can register and use enums in your forms:
defmodule MyForm do
use FormBuilderDSL
# Register an enum
defenum :status, [:active, :inactive, :pending]
form :user do
# Use the enum in a select field
select :status, options: status_labeled_options()
end
end
Features
- Runtime-safe enum registration and retrieval
- Support for both simple string options and labeled options (for form selects)
- ETS-based storage for efficient access
- Automatic table creation and management
Summary
Functions
Returns enum values as {label, value}
tuples — used for form select fields.
Returns the enum list as a list of strings.
Registers an enum under a given name (replaces previous if exists).
Starts the ETS table if not already started. Works in supervision trees.
Types
Functions
Returns enum values as {label, value}
tuples — used for form select fields.
Parameters
name
- The atom name of the registered enum
Returns
- List of
{label, value}
tuples where:label
is a humanized version of the value (e.g., "Active" for:active
)value
is the string representation of the enum value
Examples
iex> FormBuilderDSL.EnumRegistry.register(:status, [:active, :inactive])
:ok
iex> FormBuilderDSL.EnumRegistry.labeled_options(:status)
[{"Active", "active"}, {"Inactive", "inactive"}]
Returns the enum list as a list of strings.
Parameters
name
- The atom name of the registered enum
Returns
- List of strings representing the enum values
Examples
iex> FormBuilderDSL.EnumRegistry.register(:status, [:active, :inactive])
:ok
iex> FormBuilderDSL.EnumRegistry.options(:status)
["active", "inactive"]
Registers an enum under a given name (replaces previous if exists).
Parameters
name
- The atom name for the enum (e.g.,:status
)values
- List of atom values for the enum (e.g.,[:active, :inactive]
)
Examples
iex> FormBuilderDSL.EnumRegistry.register(:status, [:active, :inactive])
:ok
iex> FormBuilderDSL.EnumRegistry.options(:status)
["active", "inactive"]
Starts the ETS table if not already started. Works in supervision trees.
Examples
iex> FormBuilderDSL.EnumRegistry.start_link([])
{:ok, #PID<0.123.0>}