View Source Money.Ecto.Composite.Type (Money SQL v1.11.0)

Implements the Ecto.Type behaviour for a user-defined Postgres composite type called :money_with_currency.

This is the preferred option for Postgres database since the serialized money amount is stored as a decimal number,

Summary

Functions

Casts user input into Money.t/0 struct.

Casts user input into Money.t/0 struct.

Compares two money structs and return an boolean indicating if they are equal or not.

Compares two money structs and return an boolean indicating if they are equal or not.

Functions

Casts user input into Money.t/0 struct.

See Money,Ecto.Composite.Type.cast/2.

Casts user input into Money.t/0 struct.

Its important to note that user input is expected to be in the format expected for the current locale (as determined by Cldr.get_locale/0) or in the locale specified by the :locale parameter.

This can lead to unexpected results if the locale and the user data are not aligned. Consider the following example.

  • The current locale is :de. This means that the decimal separatator is defined ot be , and the grouping separatr is defined to be .

  • The user data (often, but not always, from a form) is %{"currency" => "EUR", amount: "1.00"}.

In this case cast/2 will return the equivalent of Money.new(:EUR, "100") not Money.new(:EUR, "1.00").

Arguments

  • money is a map containing the keys currency andamount as either strings or atoms OR a string that can be parsed to produce a Money.t/0 struct.

  • params is a keyword list of option that is passed to Money.new/3.

Returns

  • {:ok, money} or

  • :error

Notes

  • If either the money or amount values are nil, then {:ok, nil} will be returned.

  • amount can be a string, an integer or a Decimal.t/0.

  • If a string is parsed then an attempt to parse the string into a currency and an amount is made using Money.parse/2. Parsing is locale specific.

Compares two money structs and return an boolean indicating if they are equal or not.

Arguments

Returns

  • true or false.
Link to this function

equal?(term1, term2, params)

View Source

Compares two money structs and return an boolean indicating if they are equal or not.

Arguments

Returns

  • true or false.