Skip to content

Instantly share code, notes, and snippets.

@Hajto
Created October 23, 2016 08:49
Show Gist options
  • Save Hajto/81ad226e5f8575c65a6dab9f7210087b to your computer and use it in GitHub Desktop.
Save Hajto/81ad226e5f8575c65a6dab9f7210087b to your computer and use it in GitHub Desktop.
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