Skip to content

Commit

Permalink
mix format
Browse files Browse the repository at this point in the history
  • Loading branch information
kipcole9 committed Sep 18, 2024
1 parent 1565777 commit a2c0df2
Show file tree
Hide file tree
Showing 11 changed files with 84 additions and 42 deletions.
66 changes: 44 additions & 22 deletions lib/money.ex
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,7 @@ defmodule Money do
{:error,
{Money.Invalid,
"A currency code, symbol or description must be specified but " <>
"was not found in #{inspect(string)}"}}
"was not found in #{inspect(string)}"}}
end

# No currency was in the string so we'll derive it from
Expand Down Expand Up @@ -1266,7 +1266,7 @@ defmodule Money do
@doc since: "5.18.0"

@spec min(money_1 :: Money.t(), money_2 :: Money.t()) ::
{:ok, Money.t()} | {:error, {module(), String.t()}}
{:ok, Money.t()} | {:error, {module(), String.t()}}

def min(%Money{currency: same_currency} = money_1, %Money{currency: same_currency} = money_2) do
case compare(money_1, money_2) do
Expand Down Expand Up @@ -1309,7 +1309,7 @@ defmodule Money do
@doc since: "5.18.0"

@spec max(money_1 :: Money.t(), money_2 :: Money.t()) ::
{:ok, Money.t()} | {:error, {module(), String.t()}}
{:ok, Money.t()} | {:error, {module(), String.t()}}

def max(%Money{currency: same_currency} = money_1, %Money{currency: same_currency} = money_2) do
case compare(money_1, money_2) do
Expand Down Expand Up @@ -1352,7 +1352,7 @@ defmodule Money do
@doc since: "5.18.0"

@spec min!(money_1 :: Money.t(), money_2 :: Money.t()) ::
Money.t() | no_return()
Money.t() | no_return()

def min!(%Money{currency: same_currency} = money_1, %Money{currency: same_currency} = money_2) do
case compare(money_1, money_2) do
Expand Down Expand Up @@ -1396,7 +1396,7 @@ defmodule Money do
@doc since: "5.18.0"

@spec max!(money_1 :: Money.t(), money_2 :: Money.t()) ::
Money.t() | no_return()
Money.t() | no_return()

def max!(%Money{currency: same_currency} = money_1, %Money{currency: same_currency} = money_2) do
case compare(money_1, money_2) do
Expand Down Expand Up @@ -1454,13 +1454,19 @@ defmodule Money do
@doc since: "5.18.0"

@spec clamp(money :: Money.t(), minimum :: Money.t(), maximum :: Money.t()) ::
{:ok, Money.t()} | {:error, {module(), String.t()}}
{:ok, Money.t()} | {:error, {module(), String.t()}}

def clamp(%__MODULE__{currency: same_currency} = money, %__MODULE__{currency: same_currency} = minimum, %__MODULE__{currency: same_currency} = maximum) do
def clamp(
%__MODULE__{currency: same_currency} = money,
%__MODULE__{currency: same_currency} = minimum,
%__MODULE__{currency: same_currency} = maximum
) do
if compare(minimum, maximum) == :lt do
Money.max(minimum, Money.min!(maximum, money))
else
{:error, {ArgumentError, "Minimum must be less than maximum. Found #{inspect minimum} and #{inspect(maximum)}"}}
{:error,
{ArgumentError,
"Minimum must be less than maximum. Found #{inspect(minimum)} and #{inspect(maximum)}"}}
end
end

Expand Down Expand Up @@ -1508,13 +1514,18 @@ defmodule Money do
@doc since: "5.18.0"

@spec clamp!(money :: Money.t(), minimum :: Money.t(), maximum :: Money.t()) ::
Money.t() | no_return()
Money.t() | no_return()

def clamp!(%__MODULE__{currency: same_currency} = money, %__MODULE__{currency: same_currency} = minimum, %__MODULE__{currency: same_currency} = maximum) do
def clamp!(
%__MODULE__{currency: same_currency} = money,
%__MODULE__{currency: same_currency} = minimum,
%__MODULE__{currency: same_currency} = maximum
) do
if compare(minimum, maximum) == :lt do
Money.max!(minimum, Money.min!(maximum, money))
else
raise ArgumentError, "Minimum must be less than maximum. Found #{inspect minimum} and #{inspect(maximum)}"
raise ArgumentError,
"Minimum must be less than maximum. Found #{inspect(minimum)} and #{inspect(maximum)}"
end
end

Expand Down Expand Up @@ -1556,11 +1567,16 @@ defmodule Money do

@spec within?(money :: Money.t(), minimum :: Money.t(), maximum :: Money.t()) :: boolean()

def within?(%__MODULE__{currency: same_currency} = money, %__MODULE__{currency: same_currency} = minimum, %__MODULE__{currency: same_currency} = maximum) do
def within?(
%__MODULE__{currency: same_currency} = money,
%__MODULE__{currency: same_currency} = minimum,
%__MODULE__{currency: same_currency} = maximum
) do
if compare(minimum, maximum) == :lt do
compare(money, minimum) in [:gt, :eq] && compare(money, maximum) in [:lt, :eq]
else
raise ArgumentError, "Minimum must be less than maximum. Found #{inspect minimum} and #{inspect(maximum)}"
raise ArgumentError,
"Minimum must be less than maximum. Found #{inspect(minimum)} and #{inspect(maximum)}"
end
end

Expand Down Expand Up @@ -1699,8 +1715,11 @@ defmodule Money do
"""
@doc since: "5.3.0"
@spec sum([t(), ...], ExchangeRates.t() | {:ok, ExchangeRates.t()} | {:error, {module(), String.t()}}) ::
{:ok, t} | {:error, {module(), String.t()}}
@spec sum(
[t(), ...],
ExchangeRates.t() | {:ok, ExchangeRates.t()} | {:error, {module(), String.t()}}
) ::
{:ok, t} | {:error, {module(), String.t()}}

def sum(money_list, rates \\ latest_rates_or_empty_map())

Expand Down Expand Up @@ -2232,9 +2251,9 @@ defmodule Money do
end

def to_currency(%Money{currency: from_currency, amount: amount} = money, to_currency, rates)
when is_atom(to_currency) or is_digital_token(to_currency) and is_map(rates) do
when is_atom(to_currency) or (is_digital_token(to_currency) and is_map(rates)) do
with {:ok, to_currency_code} <- validate_currency(to_currency),
{:ok, cross_rate} <- cross_rate(from_currency, to_currency_code, rates) do
{:ok, cross_rate} <- cross_rate(from_currency, to_currency_code, rates) do
converted_amount = Decimal.mult(amount, cross_rate)
{:ok, %{money | currency: to_currency, amount: converted_amount}}
end
Expand Down Expand Up @@ -2582,9 +2601,10 @@ defmodule Money do
defp do_digits_from_options(_currency_data, other),
do: {:error, invalid_digits_error(other)}

defp invalid_digits_error(other), do:
{Money.InvalidDigitsError,
"Unknown or invalid :fractional_digits option found: #{inspect(other)}"}
defp invalid_digits_error(other),
do:
{Money.InvalidDigitsError,
"Unknown or invalid :fractional_digits option found: #{inspect(other)}"}

@doc """
Return a zero amount `t:Money.t/0` in the given currency.
Expand Down Expand Up @@ -2684,7 +2704,9 @@ defmodule Money do
else
def integer?(%{amount: %Decimal{coef: :NaN}}), do: false
def integer?(%{amount: %Decimal{coef: :inf}}), do: false
def integer?(%{amount: %Decimal{coef: coef, exp: exp}}), do: exp >= 0 or zero_after_dot?(coef, exp)

def integer?(%{amount: %Decimal{coef: coef, exp: exp}}),
do: exp >= 0 or zero_after_dot?(coef, exp)

defp zero_after_dot?(coef, exp) when coef >= 10 and exp < 0,
do: Kernel.rem(coef, 10) == 0 and zero_after_dot?(Kernel.div(coef, 10), exp + 1)
Expand Down Expand Up @@ -2855,7 +2877,7 @@ defmodule Money do
end

defp get_rate(currency, rates) do
keys = is_atom(currency) && [currency, Atom.to_string(currency)] || [currency]
keys = (is_atom(currency) && [currency, Atom.to_string(currency)]) || [currency]

rates
|> Map.take(keys)
Expand Down
2 changes: 1 addition & 1 deletion lib/money/backend.ex
Original file line number Diff line number Diff line change
Expand Up @@ -964,7 +964,7 @@ defmodule Money.Backend do
"""
@spec round(Elixir.Money.t(), Keyword.t()) ::
Elixir.Money.t() | {:error, {module(), binary()}}
Elixir.Money.t() | {:error, {module(), binary()}}

def round(%Elixir.Money{} = money, options \\ []) do
Elixir.Money.round(money, options)
Expand Down
7 changes: 4 additions & 3 deletions lib/money/exchange_rates/exchange_rates_retriever.ex
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ defmodule Money.ExchangeRates.Retriever do
end

defp process_response({:error, reason}, _url, _config) do
{:error, {Money.ExchangeRateError, "#{inspect reason}"}}
{:error, {Money.ExchangeRateError, "#{inspect(reason)}"}}
end

defp if_none_match_header(url) do
Expand Down Expand Up @@ -473,13 +473,14 @@ defmodule Money.ExchangeRates.Retriever do
defp seconds(milliseconds) do
seconds = div(milliseconds, 1000)
plural = if seconds == 1, do: "second", else: "seconds"
{:ok, formatted_seconds} = Cldr.Number.to_string(seconds, backend: Money.get_env(:default_cldr_backend))

{:ok, formatted_seconds} =
Cldr.Number.to_string(seconds, backend: Money.get_env(:default_cldr_backend))

{formatted_seconds, plural}
end

defp exchange_rate_service_error do
{Money.ExchangeRateError, "Exchange rate service does not appear to be running"}
end

end
2 changes: 1 addition & 1 deletion lib/money/exchange_rates/exchange_rates_supervisor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ defmodule Money.ExchangeRates.Supervisor do
defp start_retriever! do
case ExchangeRates.Retriever.start() do
{:ok, _pid} -> :ok
{:error, reason} -> raise "Unhandled error starting retriever; #{inspect reason}"
{:error, reason} -> raise "Unhandled error starting retriever; #{inspect(reason)}"
end
end
end
6 changes: 3 additions & 3 deletions lib/money/protocol/inspect.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ defimpl Inspect, for: Money do
end

def do_inspect(%Money{format_options: []} = money, _opts) do
"Money.new(#{inspect(money.currency)}, #{inspect Decimal.to_string(money.amount)})"
"Money.new(#{inspect(money.currency)}, #{inspect(Decimal.to_string(money.amount))})"
end

def do_inspect(money, _opts) do
Expand All @@ -24,6 +24,6 @@ defimpl Inspect, for: Money do
|> String.trim_leading("[")
|> String.trim_trailing("]")

"Money.new(#{inspect money.currency}, #{inspect Decimal.to_string(money.amount)}, #{format_options})"
"Money.new(#{inspect(money.currency)}, #{inspect(Decimal.to_string(money.amount))}, #{format_options})"
end
end
end
2 changes: 1 addition & 1 deletion lib/money/protocol/phoenix_html_safe.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ if Cldr.Config.ensure_compiled?(Phoenix.HTML.Safe) &&
Phoenix.HTML.Safe.to_iodata(Money.to_string!(money))
end
end
end
end
2 changes: 1 addition & 1 deletion lib/money/subscription.ex
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ defmodule Money.Subscription do
"""
# @doc since: "2.3.0"
@spec current_plan(Subscription.t() | map, Keyword.t()) ::
Plan.t() | {Change.t(), Plan.t()} | nil
Plan.t() | {Change.t(), Plan.t()} | nil

def current_plan(subscription, options \\ [])

Expand Down
1 change: 0 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ defmodule Money.Mixfile do
{:benchee, "~> 1.0", optional: true, only: :dev},
{:exprof, "~> 0.2", only: :dev, runtime: false},
{:ex_doc, "~> 0.31", only: [:dev, :release]},

{:gringotts, "~> 1.1", optional: true}
]
|> Enum.reject(&is_nil/1)
Expand Down
15 changes: 14 additions & 1 deletion mix/test_cldr.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,20 @@ defmodule Money.Cldr do

use Cldr,
locales: [
"en", "de", "it", "es", "fr", "da", "zh-Hant-HK", "zh-Hans", "ja", "en-001", "th", "es-CO", "pt-CV", "ar-EG"
"en",
"de",
"it",
"es",
"fr",
"da",
"zh-Hant-HK",
"zh-Hans",
"ja",
"en-001",
"th",
"es-CO",
"pt-CV",
"ar-EG"
],
default_locale: "en",
providers: [Cldr.Number, Money]
Expand Down
20 changes: 14 additions & 6 deletions test/money_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -129,18 +129,22 @@ defmodule MoneyTest do

test "that creating a money with a NaN is invalid" do
assert Money.new(:USD, "NaN") ==
{:error, {Money.InvalidAmountError, "Invalid money amount. Found Decimal.new(\"NaN\")."}}
{:error,
{Money.InvalidAmountError, "Invalid money amount. Found Decimal.new(\"NaN\")."}}

assert Money.new(:USD, "-NaN") ==
{:error, {Money.InvalidAmountError, "Invalid money amount. Found Decimal.new(\"-NaN\")."}}
{:error,
{Money.InvalidAmountError, "Invalid money amount. Found Decimal.new(\"-NaN\")."}}
end

test "that creating a money with a Inf is invalid" do
assert Money.new(:USD, "Inf") ==
{:error, {Money.InvalidAmountError, "Invalid money amount. Found Decimal.new(\"Infinity\")."}}
{:error,
{Money.InvalidAmountError, "Invalid money amount. Found Decimal.new(\"Infinity\")."}}

assert Money.new(:USD, "-Inf") ==
{:error, {Money.InvalidAmountError, "Invalid money amount. Found Decimal.new(\"-Infinity\")."}}
{:error,
{Money.InvalidAmountError, "Invalid money amount. Found Decimal.new(\"-Infinity\")."}}
end

test "that creating a money with a string amount that is invalid returns and error" do
Expand Down Expand Up @@ -430,8 +434,12 @@ defmodule MoneyTest do

test "money conversion with digital_token" do
rates = %{:USD => Decimal.new(50_000), "4H95J0R2X" => Decimal.new(1)}
assert Money.to_currency(Money.new(:USD, 50_000), "4H95J0R2X", rates) == {:ok, Money.new("4H95J0R2X", "1.00000")}
assert Money.to_currency(Money.new("4H95J0R2X", 1), :USD, rates) == {:ok, Money.new(:USD, 50_000)}

assert Money.to_currency(Money.new(:USD, 50_000), "4H95J0R2X", rates) ==
{:ok, Money.new("4H95J0R2X", "1.00000")}

assert Money.to_currency(Money.new("4H95J0R2X", 1), :USD, rates) ==
{:ok, Money.new(:USD, 50_000)}
end

test "money to_string" do
Expand Down
3 changes: 1 addition & 2 deletions test/money_token_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,5 @@ defmodule Money.DigitalToken.Test do

test "Formatting digital token" do
assert {:ok, "₿ 100.234235"} = Money.to_string(Money.new("BTC", "100.234235"))

end
end
end

0 comments on commit a2c0df2

Please sign in to comment.