From c803789afe413082511121a722b842a0df9e253c Mon Sep 17 00:00:00 2001 From: Kip Cole Date: Thu, 25 Jan 2018 16:38:05 +0530 Subject: [PATCH] Fixes `from_float!/2` from_float/2 would fail since `new/2` does not return `{:ok, Money.t}`. Note that from `ex_money` 3.0, `Money.new/2` will return `{:ok, Money.t}` to be consistent with canonical approaches in Elixir. Closes #48. Thanks for @lostkobrakai. --- CHANGELOG.md | 10 ++++++++-- lib/money.ex | 15 ++++++++++++--- mix.exs | 2 +- 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0bda1ad..3acdf75 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,12 @@ -# Changelog for Money v2.0.3 +# Changelog for Money v2.0.4 + +This is the changelog for Money v2.0.4 released on January 25th, 2017. For older changelogs please consult the release tag on [GitHub](https://github.com/kipcole9/money/tags) + +### Bug Fixes + +* Fixed `from_float!/2` which would fail since `new/2` does not return `{:ok, Money.t}`. Note that from `ex_money` 3.0, `Money.new/2` will return `{:ok, Money.t}` to be consistent with canonical approaches in Elixir. Closes #48. Thanks for @lostkobrakai. -This is the changelog for Money v2.0.3 released on January 24th, 2017. For older changelogs please consult the release tag on [GitHub](https://github.com/kipcole9/money/tags) +# Changelog for Money v2.0.3 ### Bug Fixes diff --git a/lib/money.ex b/lib/money.ex index 57a78ba..74ce4a6 100644 --- a/lib/money.ex +++ b/lib/money.ex @@ -99,7 +99,7 @@ defmodule Money do """ @spec new(currency_code, integer | Decimal.t | String.t) :: - {:ok, Money.t()} | {:error, {Exceptiom.t, String.t}} + Money.t() | {:error, {Exceptiom.t, String.t}} def new(currency_code, amount) when is_binary(currency_code) and is_integer(amount) do case validate_currency(currency_code) do @@ -247,7 +247,7 @@ defmodule Money do @since "2.0.0" @max_precision_allowed 15 @spec from_float(currency_code, float) :: - {:ok, Money.t()} | {:error, {Exception.t, String.t}} + Money.t() | {:error, {Exception.t, String.t}} def from_float(currency_code, amount) when (is_binary(currency_code) or is_atom(currency_code)) and is_float(amount) do @@ -284,14 +284,23 @@ defmodule Money do * `amount` is a float + ## Examples + + iex> Money.from_float!(:USD, 1.234) + #Money<:USD, 1.234> + + Money.from_float!(:USD, 1.234567890987654) + #=> ** (Money.InvalidAmountError) The precision of the float 1.234567890987654 is greater than 15 which could lead to unexpected results. Reduce the precision or call Money.new/2 with a Decimal or String amount + (ex_money) lib/money.ex:293: Money.from_float!/2 + """ @since "2.0.0" @spec from_float!(currency_code, float) :: Money.t() | no_return() def from_float!(currency_code, amount) do case from_float(currency_code, amount) do - {:ok, money} -> money {:error, {exception, reason}} -> raise exception, reason + money -> money end end diff --git a/mix.exs b/mix.exs index 7afb444..4583172 100644 --- a/mix.exs +++ b/mix.exs @@ -1,7 +1,7 @@ defmodule Money.Mixfile do use Mix.Project - @version "2.0.3" + @version "2.0.4" def project do [