Enumex
View SourceA comprehensive library for working with both static and dynamic enum structures in Elixir.
Mix dependency
def deps do
[
{:enumex, "~> 1.0"}
]
end
Rebar dependency
{deps,[
{enumex, "~> 1.0"}
]}.
Usage
See HTTP status codes and User roles examples.
Use cases
Note: Some components provide database integration, meaning that any enum module without them does not require any database connection or additional project dependencies. In such case we simply work either on static, compile-time values or dynamic, runtime-determined values.
Simple approach
The simple approach is most effective with single-project databases that do not support custom enum types, such as SQLite. In this case, all validations are performed on the Elixir side only.
Advanced approach
The advanced approach uses adapters for databases that support custom types, such as PostgreSQL. Dual-layer validation at the Elixir and database levels improves compatibility especially with projects written in a variety of languages.
Custom approach
The custom approach uses database relations instead of relying on support for custom enum type. This approach offers both database flexibility and validation support, while allowing to work with values determined at runtime.
graph LR
DatabaseIntegration{Database Integration} -->|No| ValueType{Value Type}
DatabaseIntegration{Database Integration} -->|Yes| Approach{Use case}
ValueType -->|compile-time| CompileTime
ValueType -->|runtime-determined| RuntimeDetermined
Approach -->|Single Project| SimpleApproach
Approach -->|Multi Project| AdvancedApproach
Approach -->|Generic| CustomApproach
CompileTime["
* Module: <a href="Enumex.Static.html"><code class="inline">Enumex.Static</code></a>
• Values type: <code class="inline">static</code>"]
RuntimeDetermined["
* Module: <a href="Enumex.Dynamic.html"><code class="inline">Enumex.Dynamic</code></a>
• Values type: <code class="inline">dynamic</code>"]
AdvancedApproach["
* Database type: <strong>enumetared</strong>
* Module: <a href="Enumex.Static.html"><code class="inline">Enumex.Static</code></a>
• Validations: <strong>both</strong>
• Values type: <code class="inline">static</code>"]
CustomApproach["
* Database type: <strong>relation</strong>
* Module: <a href="Enumex.Dynamic.html"><code class="inline">Enumex.Dynamic</code></a>
• Validations: <strong>both</strong>
• Values type: <code class="inline">dynamic</code>"]
SimpleApproach["
* Database type: <strong>literal</strong>
* Module: <a href="Enumex.Static.html"><code class="inline">Enumex.Static</code></a>
• Validations: <strong>Elixir</strong>-side
• Values type: <code class="inline">static</code>"]