# `Localize.Currency.Ecto.Type`
[🔗](https://github.com/elixir-localize/localize_sql/blob/v1.0.1/lib/localize/currency/ecto/type.ex#L1)

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.Type

### Casting

`cast/1` accepts a code as an atom (`:USD`) or string (`"USD"`, in any
case), or a `t:Localize.Currency.t/0` struct, and validates it against
`Localize.Currency.validate_currency/1`. An unknown code returns an
error rather than being stored.

# `embed_as`

# `equal?`

---

*Consult [api-reference.md](api-reference.md) for complete listing*
