Created
March 11, 2015 23:50
-
-
Save alanpeabody/3f35b799b75730fabe51 to your computer and use it in GitHub Desktop.
elixir embedding step 7
This file contains 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 App.Models.Address do | |
defstruct [:address, :address2, :city, :region, :postal, :country] | |
defmodule Type do | |
@behaviour Ecto.Type | |
alias App.Models.Address | |
def type, do: :json | |
def cast(addrs) when is_list(addrs) do | |
{:ok, Enum.map(addrs, &do_cast/1)} | |
end | |
def cast(_other), do: :error | |
defp do_cast(%Address{} = addr), do: addr | |
defp do_cast(%{} = addr), do: struct(Address, addr) | |
def load(value), do: Poison.decode(value, as: [App.Models.Address]) | |
def dump(value), do: Poison.encode(value) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment