Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow %Currency{} structs in currency_for_code/3 #4

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions lib/cldr/backend.ex
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,8 @@ defmodule Cldr.Currency.Backend do

## Arguments

* `currency_code` is a `binary` or `atom` representation of an
ISO 4217 currency code.
* `currency_or_currency_code` is a `binary` or `atom` representation
of an ISO 4217 currency code, or a `%Cldr.Currency{}` struct.

## Examples

Expand Down Expand Up @@ -288,14 +288,14 @@ defmodule Cldr.Currency.Backend do
}}

"""
@spec currency_for_code(Cldr.Currency.code, Keyword.t()) ::
{:ok, Cldr.Currency.t} | {:error, {module(), String.t()}}
@spec currency_for_code(Cldr.Currency.code() | Cldr.Currency.t(), Keyword.t()) ::
{:ok, Cldr.Currency.t()} | {:error, {module(), String.t()}}

def currency_for_code(
currency_code,
currency_or_currency_code,
options \\ [locale: unquote(backend).default_locale()]
) do
Cldr.Currency.currency_for_code(currency_code, unquote(backend), options)
Cldr.Currency.currency_for_code(currency_or_currency_code, unquote(backend), options)
end

defp get_currency_metadata(code, nil) do
Expand Down
14 changes: 8 additions & 6 deletions lib/cldr/currency.ex
Original file line number Diff line number Diff line change
Expand Up @@ -771,8 +771,8 @@ defmodule Cldr.Currency do

## Arguments

* `currency_code` is a `binary` or `atom` representation of an
ISO 4217 currency code.
* `currency_or_currency_code` is a `binary` or `atom` representation
of an ISO 4217 currency code, or a `%Cldr.Currency{}` struct.

* `backend` is any module that includes `use Cldr` and therefore
is a `Cldr` backend module
Expand Down Expand Up @@ -820,16 +820,18 @@ defmodule Cldr.Currency do

"""

@spec currency_for_code(code, Cldr.backend(), Keyword.t()) ::
{:ok, t} | {:error, {module(), String.t()}}
@spec currency_for_code(code() | t(), Cldr.backend(), Keyword.t()) ::
{:ok, t()} | {:error, {module(), String.t()}}

def currency_for_code(currency_code, backend, options \\ []) do
def currency_for_code(currency_or_currency_code, backend, options \\ [])
def currency_for_code(%__MODULE__{} = currency, _backend, _options), do: {:ok, currency}

def currency_for_code(currency_code, backend, options) do
{locale, backend} = Cldr.locale_and_backend_from(options[:locale], backend)

with {:ok, code} <- known_currency_code(currency_code),
{:ok, locale} <- Cldr.validate_locale(locale, backend),
{:ok, currencies} <- currencies_for_locale(locale, backend) do

{:ok, Map.get_lazy(currencies, code, fn -> Map.get(private_currencies(), code) end)}
end
end
Expand Down