An Ecto.Type storing a currency as its ISO 4217 code in a text column.
A currency is identified by its code — :USD, :EUR, :JPY — and
that code is all that needs storing; the currency's name, symbol and
digits are reference data looked up from the code, not per-row data. So
the column is ordinary text holding the code, needing no migration
beyond the column itself.
Values cast and load as the validated code atom, so a stored currency
can carry a foreign key, a unique index, or a CHECK constraint like
any other text, and an unknown code is rejected on the way in.
Schema
# in a migration
add :currency, :string
# in a schema
field :currency, Localize.Currency.Ecto.TypeCasting
cast/1 accepts a code as an atom (:USD) or string ("USD", in any
case), or a Localize.Currency.t/0 struct, and validates it against
Localize.Currency.validate_currency/1. An unknown code returns an
error rather than being stored.
Summary
Functions
Callback implementation for Ecto.Type.embed_as/1.
Callback implementation for Ecto.Type.equal?/2.
Functions
Callback implementation for Ecto.Type.embed_as/1.
Callback implementation for Ecto.Type.equal?/2.