Created
September 23, 2021 04:25
-
-
Save kipcole9/0c22bc507301302d8035651b1d35325b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if Code.ensure_loaded?(Ecto.Type) do | |
defmodule Bitstring do | |
@moduledoc """ | |
Implements the Ecto.Type behaviour for the Postgres | |
type "bit varying" | |
""" | |
use Ecto.Type | |
def type do | |
:"bit varying" | |
end | |
# When loading from the database | |
def load(tuple, loader \\ nil, params \\ []) | |
def load(nil, _loader, _params) do | |
{:ok, nil} | |
end | |
def load(bitstring, _loader, _params) do | |
{:ok, bitstring} | |
end | |
# Dumping to the database. | |
def dump(bitstring, dumper \\ nil, params \\ []) | |
def dump(bitstring, _dumper, _params) when is_bitstring(bitstring) do | |
{:ok, bitstring} | |
end | |
def dump(nil, _, _) do | |
{:ok, nil} | |
end | |
def dump(_, _, _) do | |
:error | |
end | |
# Casting in changesets | |
def cast(bitstring, params \\ []) | |
def cast(nil, _params) do | |
{:ok, nil} | |
end | |
def cast(bitstring, _params) when is_bitstring(bitstring) do | |
{:ok, bitstring} | |
end | |
def cast(_bitstring, _params) do | |
:error | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment