Skip to content

Commit

Permalink
Fix formatting a that has no key. Fixes #123
Browse files Browse the repository at this point in the history
  • Loading branch information
kipcole9 committed Feb 18, 2021
1 parent 5a157f9 commit bb294f1
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 4 deletions.
10 changes: 9 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
# Changelog for Money v5.5.1

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)

### Bug Fixes

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

# Changelog for Money v5.5.0

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)
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)

### Enhancements

Expand Down
7 changes: 5 additions & 2 deletions lib/money.ex
Original file line number Diff line number Diff line change
Expand Up @@ -701,19 +701,22 @@ defmodule Money do

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

options =
default_options
|> Keyword.merge(money.format_options)
|> Keyword.merge(format_options)
|> Keyword.merge(options)

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

def to_string(%Money{} = money, %Cldr.Number.Format.Options{} = options) do
format_options = Map.get(money, :format_options, [])

options =
money.format_options
format_options
|> Map.new()
|> Map.merge(options)
|> Map.put(:currency, money.currency)
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule Money.Mixfile do
use Mix.Project

@version "5.5.0-dev"
@version "5.5.1"

def project do
[
Expand Down
5 changes: 5 additions & 0 deletions test/money_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -542,4 +542,9 @@ defmodule MoneyTest do
assert Money.div!(money_with_options, 3).format_options == format_options

end

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

0 comments on commit bb294f1

Please sign in to comment.