-
Notifications
You must be signed in to change notification settings - Fork 2
Open
Description
This may be a separate module.
The basic idea add a way for a Z.Struct to also be an Ecto.Type so that you can marshal to and from PostgreSQL composite types.
For example given the following Z.Struct
defmodule Money do
use Z.Struct
use Z.Ecto.Type, type: :money_with_currency
schema do
field :amount, :integer, required: true
field :currency, :atom, default: :USD, required: true, parse: true, enum: [:USD]
end
endgenerate the following code
use Ecto.Type
def type, do: :money_with_currency
def load({amount, currency}) do
Money.new(amount: amount, currency: currency)
end
def dump(%Money{} = money), do: {:ok, {money.amount, to_string(money.currency)}}
def dump(_), do: :error
def cast(money) do
Money.validate(money, cast: true, required: true)
endMetadata
Metadata
Assignees
Labels
No labels