Skip to content

Commit

Permalink
Ensure currency atoms are loaded before sigil_M
Browse files Browse the repository at this point in the history
`Money.Sigil` was calling `String.to_existing_atom/1` directly rather
than `Cldr.validate_currency/1`. Since currency codes are only loaded
and therefore the atoms materialized when `Cldr` is loaded this created
a situation whereby a valid currency code may raise an `agument error`.
`Money.Sigil` now correctly calls `Cldr.validate_currency/1` which
ensures the currency atoms are loaded before validation. Closes #46.
  • Loading branch information
kipcole9 committed Jan 17, 2018
1 parent 9468d47 commit 6f874b4
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 5 deletions.
10 changes: 8 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# Changelog for Money v2.0.1
# Changelog for Money v2.0.2

This is the changelog for Money v2.0.2 released on January 18th, 2017. For older changelogs please consult the release tag on [GitHub](https://github.com/kipcole9/money/tags)

### Bug Fixes

* `Money.Sigil` was calling `String.to_existing_atom/1` directly rather than `Cldr.validate_currency/1`. Since currency codes are only loaded and therefore the atoms materialized when `Cldr` is loaded this created a situation whereby a valid currency code may raise an `agument error`. `Money.Sigil` now correctly calls `Cldr.validate_currency/1` which ensures the currency atoms are loaded before validation. Closes #46.

This is the changelog for Money v2.0.1 released on January 16th, 2017. For older changelogs please consult the release tag on [GitHub](https://github.com/kipcole9/money/tags)
# Changelog for Money v2.0.1

### Bug Fixes

Expand Down
10 changes: 8 additions & 2 deletions lib/money/sigil.ex
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,13 @@ defmodule Money.Sigil do
defp atomize(currency) do
currency
|> List.to_string()
|> String.upcase()
|> String.to_existing_atom()
|> validate_currency!
end

def validate_currency!(currency) do
case Money.validate_currency(currency) do
{:ok, currency} -> currency
{:error, {_exception, reason}} -> raise Money.UnknownCurrencyError, reason
end
end
end
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 "2.0.1"
@version "2.0.2"

def project do
[
Expand Down
13 changes: 13 additions & 0 deletions test/money_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,19 @@ defmodule MoneyTest do
assert m == Money.new!(:USD, 100)
end

test "raise when a sigil function has an invalid currency" do
assert_raise Money.UnknownCurrencyError, ~r/The currency .* is invalid/, fn ->
Money.Sigil.sigil_M("42", [?A, ?B, ?C])
end
end

test "raise when a sigil has an invalid currency" do
import Money.Sigil
assert_raise Money.UnknownCurrencyError, ~r/The currency .* is invalid/, fn ->
~M[42]ABD
end
end

test "that we get a deprecation message if we use :exchange_rate_service keywork option" do
Application.put_env(:ex_money, :exchange_rate_service, true)

Expand Down

0 comments on commit 6f874b4

Please sign in to comment.