EctoPostgresEnum v1.1.0 EctoPostgresEnum View Source

Helper module to define enum for ecto and PostgreSQL with support for dynamic values.

Usage

defmodule MyEnum do
  values = [:my, :enum]
  use EctoPostgresEnum, schema: :my_schema, type: :my_type, values: values
end

Options

  • schema - Allows to change PostgreSQL Schema for specified enum.
  • type - Allows to change type identifier. It's useful for migrations and required by Ecto.Type.type/0 callback.
NameTypeRequiredDefault
schemaatomfalsenil (fallbacks to PostgreSQL default: "public")
typeatomfalseatom (underscored last module part)
valueslist(atom)trueN/A

Debug

This library automatically generates few useful functions to work with allowed values.

Example usage:

defmodule MyEnum do
  values = [:my, :enum]
  use EctoPostgresEnum, schema: :my_schema, type: :my_type, values: values
end

defmodule MyApp do
  require MyEnum

  def handle_user_input(input) when MyEnum.valid_string?(input) do
    IO.puts "Alright, #{input} is allowed value!"
  end

  def handle_user_input(input) do
    IO.puts "Ooops, #{input} is not allowed value! Please try again …"
  end
end

MyApp.handle_user_input("my")
{:ok, "my"}

MyApp.handle_user_input("something")
{:error. "Wrong enum value!"}