gjaldon

gjaldon

EctoEnum for ActiveRecord-like enums in Ecto updated!

As the title states, EctoEnum has just been updated after some time of hardly any activity in the repo. Here’s the latest release: Release 0.3.2 · gjaldon/ecto_enum · GitHub

The latest release still isn’t compatible with Ecto 1.1 to 2.0. I’ll be working on that in the upcoming days. There’s been significant change in how custom Ecto types work in 1.1 so it will take a bit more work. :slight_smile:

Would appreciate any feedback! Enjoy!

Most Liked

idi527

idi527

Just in case anyone is looking for a way to use postgres enum types with ecto, here’s how (without any additional dependencies).

defmodule Invoice.Status do
  @moduledoc """
  The status of payment; whether the invoice has been paid or not.

  Enumeration members:
  - `:payment_automatically_applied`
  - `:payment_complete`
  - `:payment_declined`
  - `:payment_due`
  - `:payment_past_due`
  """

  @behaviour Ecto.Type

  statuses = [
    :payment_automatically_applied,
    :payment_complete,
    :payment_declined,
    :payment_due,
    :payment_past_due
  ] |> Enum.map(fn status ->
    {status, to_string(status)}
  end)

  @doc "Returns the underlying schema type for the custom type"
  @spec type :: :invoice_status
  def type, do: :invoice_status

  @doc "Casts the given input to the custom type"
  @spec cast(atom | binary) :: {:ok, atom} | :error
  def cast(status)

  for {atom, string} <- statuses do
    def cast(unquote(atom)), do: {:ok, unquote(atom)}
    def cast(unquote(string)), do: {:ok, unquote(atom)}
  end

  def cast(_other), do: :error

  @doc "Loads the given term into a custom type"
  @spec load(binary) :: {:ok, atom}
  def load(status)

  for {atom, string} <- statuses do
    def load(unquote(string)), do: {:ok, unquote(atom)}
  end

  @doc "Dumps the given term into an Ecto native type"
  @spec dump(atom | binary) :: {:ok, binary} | :error
  def dump(status)

  for {atom, string} <- statuses do
    def dump(unquote(atom)), do: {:ok, unquote(string)}
    def dump(unquote(string)), do: {:ok, unquote(string)}
  end

  def dump(_other), do: :error
end
gjaldon

gjaldon

No doubt you could write your own code to create a custom Ecto type. The point of this small and focused library is to do the metaprogramming for you so don’t need to write that code every time you need to create an Enum in Ecto. With EctoEnum, you just need to write a few lines of code to have a StatusEnum, DayOfWeekEnum, etc… Also, this library actually has tests and is used by many other devs. That’s the point of having libraries and sharing packages, so we can focus on other work.

gjaldon

gjaldon

Oh and EctoEnum works with Postgres’s Enum type or a plain ol’ integer. It will have support for MySQL’s Enum type in the future too.

Where Next?

Popular in Announcing Top

sasajuric
I’d like to announce a small library called boundaries. This is an experimental project which explores the idea of enforcing boundaries ...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 43622 214
New
tfwright
After working on it for a couple of months and using it in production for most of that time, today I’ve released LiveAdmin, a LiveView ba...
New
bluzky
You may know https://ui.shadcn.com/, a UI component library for React. I really love it’s design style and components. I’ve built some co...
384 13736 119
New
woutdp
Hi! I wanted to introduce my latest project LiveSvelte. It allows you to render Svelte inside LiveView with end-to-end reactivity. It’s ...
New
wojtekmach
Hey everyone! Req is an HTTP client for Elixir that I’ve been working on for quite some time. There is already a lot of HTTP clients out...
New
Crowdhailer
Raxx is an alternative to Plug and is inspired by projects such as Rack(Ruby) and Ring(Clojure). 1.0-rc.1 is now available. To use it re...
New
ahamez
Hi everyone, I’ve been working on this protobuf library for 3 years. We use it in the company I work for, EasyMile, to communicate with ...
New
hpopp
After just over two years in development, this latest version of Pigeon is what I finally consider done in regards to my original vision ...
New
anshuman23
Hello all, I have been working on my proposed project called Tensorflex as part of Google Summer of Code 2018.. Tensorflex can be used f...
New

Other popular topics Top

TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 41539 114
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30877 112
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 43622 214
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
boundedvariable
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New

We're in Beta

About us Mission Statement