Last active
August 29, 2015 14:16
-
-
Save alanpeabody/bf9b16b617c833133807 to your computer and use it in GitHub Desktop.
embedding elixir structs step 6
This file contains hidden or 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.Settings do | |
defstruct [ | |
newsletter: false, | |
publish_profile: true, | |
email_notifications: true | |
] | |
defmodule Type do | |
@behaviour Ecto.Type | |
alias App.Models.Settings | |
def type, do: :json | |
def cast(%Settings{} = settings), do: {:ok, settings} | |
def cast(%{} = settings), do: {:ok, struct(Settings, settings)} | |
def cast(_other), do: :error | |
def load(value), do: Poison.decode(value, as: App.Models.Settings) | |
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
Hi Alan,
I added the jsonb version of the module to my fork of your gist. Please see:
Would you mind adding it to your blog post?
Thanks,
Sancero