Skip to content

Commit

Permalink
Fixes from_float!/2
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
kipcole9 committed Jan 25, 2018
1 parent 9e9caef commit c803789
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 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.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

Expand Down
15 changes: 12 additions & 3 deletions lib/money.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

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 "2.0.3"
@version "2.0.4"

def project do
[
Expand Down

0 comments on commit c803789

Please sign in to comment.