-
Notifications
You must be signed in to change notification settings - Fork 85
/
point.ex
34 lines (22 loc) · 849 Bytes
/
point.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
defmodule Geo.Point do
@moduledoc """
Defines the Point struct. Implements the Ecto.Type behaviour
"""
@type t :: %Geo.Point{ coordinates: {number, number}, srid: integer }
defstruct coordinates: {0, 0}, srid: nil
if Code.ensure_loaded?(Ecto.Type) do
@behaviour Ecto.Type
def type, do: :geometry
def blank?(_), do: false
def load(%Geo.Point{} = point), do: {:ok, point}
def load(_), do: :error
def dump(%Geo.Point{} = point), do: {:ok, point}
def dump(_), do: :error
def cast(%Geo.Point{} = point), do: {:ok, point}
def cast(%{"type" => _, "coordinates" => _} = point), do: { :ok, Geo.JSON.decode(point) }
if Code.ensure_loaded?(Poison) do
def cast(point) when is_binary(point), do: { :ok, Poison.decode!(point) |> Geo.JSON.decode }
end
def cast(_), do: :error
end
end