Skip to content

Instantly share code, notes, and snippets.

@Hajto
Created October 19, 2016 14:44
Show Gist options
  • Save Hajto/783e2a921b8cbe91cefb816c81204001 to your computer and use it in GitHub Desktop.
Save Hajto/783e2a921b8cbe91cefb816c81204001 to your computer and use it in GitHub Desktop.
defmodule Kpsz.Ecto.Point do
@behaviour Ecto.Type
@moduledoc """
Representation of geological point in the database
"""
@extractor ~r/\(([\d.]+),([\d.]+)\)/
def type, do: :string
def cast(string) when is_binary(string), do: {:ok, string}
def cast(_), do: :error
def load(string) do
case Regex.run(@extractor, string) do
[_whole, x, y] -> {:ok, {x, y} }
_ -> :error
end
end
def loard(_), do: :error
def dump({ x, 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