Skip to content

Commit bb294f1

Browse files
committed
Fix formatting a that has no key. Fixes #123
1 parent 5a157f9 commit bb294f1

4 files changed

Lines changed: 20 additions & 4 deletions

File tree

CHANGELOG.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
1+
# Changelog for Money v5.5.1
2+
3+
This is the changelog for Money v5.5.1 released on February 18th, 2021. For older changelogs please consult the release tag on [GitHub](https://github.com/kipcole9/money/tags)
4+
5+
### Bug Fixes
6+
7+
* Fix formatting a `t:Money` that has no `:format_options` key. That can happen if re-hydrating a `t:Money` using `:erlang.binary_to_term/1` from an older version of `ex_money` that doesn't have the `:format_options` key in the struct. Thanks to @coladarci. Fixes #123.
8+
19
# Changelog for Money v5.5.0
210

3-
This is the changelog for Money v5.5.0 released on ______, 2021. For older changelogs please consult the release tag on [GitHub](https://github.com/kipcole9/money/tags)
11+
This is the changelog for Money v5.5.0 released on February 10th, 2021. For older changelogs please consult the release tag on [GitHub](https://github.com/kipcole9/money/tags)
412

513
### Enhancements
614

lib/money.ex

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -701,19 +701,22 @@ defmodule Money do
701701

702702
def to_string(%Money{} = money, options) when is_list(options) do
703703
default_options = [backend: Money.default_backend(), currency: money.currency]
704+
format_options = Map.get(money, :format_options, [])
704705

705706
options =
706707
default_options
707-
|> Keyword.merge(money.format_options)
708+
|> Keyword.merge(format_options)
708709
|> Keyword.merge(options)
709710

710711
backend = options[:backend]
711712
Cldr.Number.to_string(money.amount, backend, options)
712713
end
713714

714715
def to_string(%Money{} = money, %Cldr.Number.Format.Options{} = options) do
716+
format_options = Map.get(money, :format_options, [])
717+
715718
options =
716-
money.format_options
719+
format_options
717720
|> Map.new()
718721
|> Map.merge(options)
719722
|> Map.put(:currency, money.currency)

mix.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
defmodule Money.Mixfile do
22
use Mix.Project
33

4-
@version "5.5.0-dev"
4+
@version "5.5.1"
55

66
def project do
77
[

test/money_test.exs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,4 +542,9 @@ defmodule MoneyTest do
542542
assert Money.div!(money_with_options, 3).format_options == format_options
543543

544544
end
545+
546+
test "to_string/2 on a Money that doesn't have a :format_options key" do
547+
money = %{__struct__: Money, amount: Decimal.new(3), currency: :USD}
548+
assert Money.to_string(money) == {:ok, "$3.00"}
549+
end
545550
end

0 commit comments

Comments
 (0)