From 6f874b41f55c00212965c11cbc9f142755b5194e Mon Sep 17 00:00:00 2001 From: Kip Cole Date: Thu, 18 Jan 2018 02:33:31 +0800 Subject: [PATCH] Ensure currency atoms are loaded before sigil_M `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. --- CHANGELOG.md | 10 ++++++++-- lib/money/sigil.ex | 10 ++++++++-- mix.exs | 2 +- test/money_test.exs | 13 +++++++++++++ 4 files changed, 30 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4b53912..8966fca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/money/sigil.ex b/lib/money/sigil.ex index 8faef66..3ac1d70 100644 --- a/lib/money/sigil.ex +++ b/lib/money/sigil.ex @@ -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 diff --git a/mix.exs b/mix.exs index 3e6ea3d..50695eb 100644 --- a/mix.exs +++ b/mix.exs @@ -1,7 +1,7 @@ defmodule Money.Mixfile do use Mix.Project - @version "2.0.1" + @version "2.0.2" def project do [ diff --git a/test/money_test.exs b/test/money_test.exs index c878c10..0282bde 100644 --- a/test/money_test.exs +++ b/test/money_test.exs @@ -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)