Created
November 7, 2018 15:51
-
-
Save zacck-zz/528ab5a9658bd8f774f1f7fa6bd2806e to your computer and use it in GitHub Desktop.
This file contains hidden or 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
defmodule LaserRange do | |
@moduledoc """ | |
This module holds the implementation for the `LaserRange` | |
It implements an `Ecto.Type` behaviour | |
""" | |
@behaviour Ecto.Type | |
@doc false | |
@spec type() :: tuple() | |
def type, do: {:array, :integer} | |
@doc """ | |
This function takes a list containg two integers taken as the lower and upper limits | |
of your range | |
Parameters: | |
* `lower` - an integer representing the lower limit of the range | |
* `upper` - an integer representing the upper limit of the range | |
""" | |
@spec cast(list(integer())) :: {atom(), list(integer())} | |
def cast([lower, upper]) do | |
{:ok, [lower, upper]} | |
end | |
def cast(_), do: :error | |
@doc """ | |
This function loads our custom type from the Database type | |
""" | |
@spec load(Postgrex.Range.t()) :: {atom(), list(integer())} | |
def load(%Postgrex.Range{lower: lower, upper: nil}) do | |
{:ok, [lower, nil]} | |
end | |
def load(%Postgrex.Range{lower: lower, upper: upper}) do | |
{:ok, [lower, upper]} | |
end | |
@doc """ | |
This function takes in our custom type and returns the Database Type | |
""" | |
@spec dump(list(integer())) :: {atom(), Postgrex.Range.t()} | |
def dump([lower, upper]) do | |
{:ok, %Range{lower: lower, upper: upper, upper_inclusive: true}} | |
end | |
def dump(_), do: :error | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment