Skip to content

Instantly share code, notes, and snippets.

@alanpeabody
Last active August 29, 2015 14:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save alanpeabody/bf9b16b617c833133807 to your computer and use it in GitHub Desktop.
Save alanpeabody/bf9b16b617c833133807 to your computer and use it in GitHub Desktop.
embedding elixir structs step 6
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
@sancero
Copy link

sancero commented Apr 23, 2015

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment