Created
October 23, 2016 08:49
-
-
Save Hajto/81ad226e5f8575c65a6dab9f7210087b 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 Kpsz.Point do | |
@behaviour Ecto.Type | |
@moduledoc """ | |
Representation of geological point in the database | |
""" | |
defmodule State do | |
defstruct x: 0, y: 0 | |
end | |
@extractor ~r/\(([\d.]+),([\d.]+)\)/ | |
def type, do: :string | |
def cast(%State{} = point), do: point | |
def cast({x,y}), do: {:ok, %State{x: x, y: y} } | |
def cast(_) do | |
IO.inspect "LOAD crash" | |
:error | |
end | |
def load(string) when is_binary(string) do | |
case Regex.run(@extractor, string) do | |
[_whole, x, y] -> {:ok, %State{x: x, y: y} } | |
_ -> | |
:error | |
end | |
end | |
def load(_), do: :error | |
def dump(%State{x: x, y: y}), do: {:ok, "(#{x},#{y})" } | |
def dump(_),do: :error | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment