Skip to content

Instantly share code, notes, and snippets.

@alanpeabody
Created March 11, 2015 23:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alanpeabody/3f35b799b75730fabe51 to your computer and use it in GitHub Desktop.
Save alanpeabody/3f35b799b75730fabe51 to your computer and use it in GitHub Desktop.
elixir embedding step 7
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