Skip to content

Commit

Permalink
Converting from_currency to a binary to_currency that is the same as …
Browse files Browse the repository at this point in the history
…from_currency is a noop. Close #76
  • Loading branch information
kipcole9 committed Aug 20, 2018
1 parent 934b7ab commit 15feae6
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 13 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
# Changelog for Money v2.7.2

This is the changelog for Money v2.7.2 released on August 21st, 2018. For older changelogs please consult the release tag on [GitHub](https://github.com/kipcole9/money/tags)

### Bug Fixes

* Converting from currency to a binary `to_currency` is a no-op as it already is for an atom `to_currency`. Thanks to @lostkobrakai. Closes #76.

# Changelog for Money v2.7.1

This is the changelog for Money v2.7.1 released on August 16th, 2018. For older changelogs please consult the release tag on [GitHub](https://github.com/kipcole9/money/tags)
Expand Down
27 changes: 15 additions & 12 deletions lib/money.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1197,20 +1197,27 @@ defmodule Money do

def to_currency(money, to_currency, rates \\ Money.ExchangeRates.latest_rates())

def to_currency(%Money{currency: currency} = money, to_currency, _rates)
when currency == to_currency do
def to_currency(%Money{} = money, currency, {:ok, %{} = rates}) do
to_currency(money, currency, rates)
end

def to_currency(_money, _to_currency, {:error, reason}) do
{:error, reason}
end

def to_currency(%Money{currency: currency} = money, currency, _rates) do
{:ok, money}
end

def to_currency(%Money{currency: currency} = money, to_currency, %{} = rates)
when is_atom(to_currency) or is_binary(to_currency) do
with {:ok, to_code} <- validate_currency(to_currency) do
if currency == to_code, do: money, else: to_currency(money, to_currency, {:ok, rates})
def to_currency(%Money{} = money, to_currency, %{} = rates)
when is_binary(to_currency) do
with {:ok, currency_code} <- validate_currency(to_currency) do
to_currency(money, currency_code, rates)
end
end

def to_currency(%Money{currency: from_currency, amount: amount}, to_currency, {:ok, rates})
when is_atom(to_currency) or is_binary(to_currency) do
def to_currency(%Money{currency: from_currency, amount: amount}, to_currency, %{} = rates)
when is_atom(to_currency) do
with {:ok, currency_code} <- validate_currency(to_currency),
{:ok, base_rate} <- get_rate(from_currency, rates),
{:ok, conversion_rate} <- get_rate(currency_code, rates) do
Expand All @@ -1223,10 +1230,6 @@ defmodule Money do
end
end

def to_currency(_money, _to_currency, {:error, reason}) do
{:error, reason}
end

@doc """
Convert `money` from one currency to another and raises on error
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.7.1"
@version "2.7.2"

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 @@ -398,6 +398,11 @@ defmodule MoneyTest do
assert Money.to_currency(Money.new(:USD, 100), :AUD, rates) == {:ok, Money.new(:AUD, 200)}
end

test "money conversion with binary to_currency that is the same as from currency" do
rates = %{USD: Decimal.new(0.3), AUD: Decimal.new(2)}
assert Money.to_currency(Money.new(:USD, 100), "USD", rates) == {:ok, Money.new(:USD, 100)}
end

test "money to_string" do
assert Money.to_string(Money.new(:USD, 100)) == {:ok, "$100.00"}
end
Expand Down

0 comments on commit 15feae6

Please sign in to comment.