Skip to content

A simple enum implementation for Elixir

License

Notifications You must be signed in to change notification settings

ImNotAVirus/simple_enum

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

33 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SimpleEnum

Hex.pm version Hex.pm license Build Status Coverage Status

SimpleEnum is a simple library that implements Enumerations in Elixir.

An Enumeration is a user-defined type that consists of a set of several named constants that are known as Enumerators.
The purpose of SimpleEnum is to provide an equivalent for the Elixir language.

SimpleEnum is:

  • fast: being based on a macro system, access to the Enum will be resolved at compile time when it is possible (see. Fast vs Slow access)
  • simple: the use of the library has been designed to be as simple as possible for a developer to use. In addition to providing Enums, it automatically defines their types and provides an introspection system.

Installation

The package can be installed by adding simple_enum to your list of dependencies in mix.exs:

# my_app/mix.exs
def deps do
  [
    {:simple_enum, "~> 0.1"}
  ]
end

Then, update your dependencies:

$ mix deps.get

Optionally, if you use the formatter, add this line to .formatter.exs:

# my_app/.formatter.exs
[
  import_deps: [:simple_enum]
]

Basic Usage

iex> defmodule MyEnums do
...>   import SimpleEnum, only: [defenum: 2]
...>
...>   defenum :color, [:blue, :green, :red]
...>   defenum :day, monday: "MON", tuesday: "TUE", wednesday: "WED"
...> end

iex> require MyEnums

iex> MyEnums.color(:blue)
0
iex> MyEnums.color(0)
:blue
iex> MyEnums.day(:monday)
"MON"
iex> MyEnums.day("MON")
:monday
iex> MyEnums.day("MONN")
** (ArgumentError) invalid value "MONN" for Enum MyEnums.day/1. Expected one of [:monday, :tuesday, :wednesday, "MON", "TUE", "WED"]

Full documentation can be found at https://hexdocs.pm/simple_enum.

Copyright and License

Copyright (c) 2021 DarkyZ aka NotAVirus.

SimpleEnum source code is licensed under the MIT License.