ProductPromotion
Logo

Elixir

made by https://0x3d.site

GitHub - KamilLelonek/exnumerator: Enumerable type in Elixir
Enumerable type in Elixir. Contribute to KamilLelonek/exnumerator development by creating an account on GitHub.
Visit Site

GitHub - KamilLelonek/exnumerator: Enumerable type in Elixir

GitHub - KamilLelonek/exnumerator: Enumerable type in Elixir

exnumerator

Enum type in Elixir known from Java or C#.

Build Status

Either in Java or in C# there is enum type available. It is a special data type that enables for a variable to be a set of predefined constants. The variable must be equal to one of the values that have been predefined for it. Common examples include compass directions (values of NORTH, SOUTH, EAST, and WEST) and the days of the week. You should use enum types any time you need to represent a fixed set of constants. That includes natural enum types such as the planets in our solar system and data sets where you know all possible values at compile time—for example, the choices on a menu, command line flags, and so on.

See more:

Rationale

Imagine a question that can be either "pending”, "answered, or "flagged”. Or a phone number that’s a "home”, "office”, "mobile”, or "fax” (if it’s 1982).

Some models call for this kind of data. An attribute that can have only one of a few different values. And that set of values almost never changes. It’s a situation where, if it were plain Elixir, you’d just use an atom.

When it comes to a database though, you could create a PhoneNumberType or QuestionStatus table and a belongs_to relationship to hold these values, but that doesn’t seem worth it. You can use Ecto.Type behaviour for implementing custom types, but it expects 4 functions to be implemented, and it all becomes an overhead when you need to have many of custom enumerated types.

Here exnumerator steps in to give you a handy way to define enumerable types that can be used together with your database. They are kept as string type under the hood so whatever database you use (if not postgres for some reason) you can get full benefits from this library.

Installation

The package can be installed as:

def deps do
  [
    {:exnumerator, "~> 1.6"},
    # ...
  ]
end

Usage

This project is helpful if you have both ecto and postgrex in your project. It makes no sense to use it without a database.

Custom type

# VALUES as STRINGS
defmodule MyProject.Message.StatusAsString do
  use Exnumerator,
    values: ["sent", "read", "received", "delivered"]
end

# VALUES as ATOMS
defmodule MyProject.Message.StatusAsAtom do
  use Exnumerator,
    values: [:sent, :read, :received, :delivered]
end

# VALUES as KEYWORD
defmodule MyProject.Message.StatusAsKeyword do
  use Exnumerator,
    values: [sent: "S", read: "R", received: "RE", delivered: "D"]
end

Database migration

defmodule MyProject.Repo.Migrations.CreateMessage do
  use Ecto.Migration

  def change do
    create table(:messages) do
      add :status, :string
    end
  end
end

Database schema

defmodule MyProject.Message do
  use MyProject.Web, :schema

  schema "messages" do
    field :status, MyProject.Message.StatusAsString
  end
end

Operations

You can see all available values:

iex(1)> MyProject.Message.StatusAsString.values()
["sent", "read", "received", "delivered"]
iex(1)> MyProject.Message.StatusAsAtom.values()
[:sent, :read, :received, :delivered]
iex(1)> MyProject.Message.StatusAsKeyword.values()
[sent: "S", read: "R", received: "RE", delivered: "D"]

When you try to insert a record with some value that is not defined, you will get the following error:

# Status should be a String or an Atom, depending of what you use.

iex(1)> %MyProject.Message{status: "invalid"} |> MyProject.Repo.insert!()
** (Ecto.ChangeError) value `"invalid"` for `MyProject.Message.status`
   in `insert` does not match type MyProject.Message.status

You can also pick a random value from the predefined set:

iex(1)> MyProject.Message.StatusAsString.sample()
"delivered"

iex(1)> MyProject.Message.StatusAsAtom.sample()
:delivered

iex(1)> MyProject.Message.StatusAsKeyword.sample()
{:delivered, "D"}

You can pick the first value from the predefined set too:

iex(1)> MyProject.Message.StatusAsString.first()
"sent"

iex(1)> MyProject.Message.StatusAsAtom.first()
:sent

iex(1)> MyProject.Message.StatusAsKeyword.first()
{:sent, "S"}

Testing

To test this project you need to run:

mix test

Articles
to learn more about the elixir concepts.

Resources
which are currently available to browse on.

mail [email protected] to add your project or resources here 🔥.

FAQ's
to know more about the topic.

mail [email protected] to add your project or resources here 🔥.

Queries
or most google FAQ's about Elixir.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory