Skip to content

Commit

Permalink
Remove references to Cldr.Currency genserver callback config (its dep…
Browse files Browse the repository at this point in the history
…recated)
  • Loading branch information
kipcole9 committed May 14, 2022
1 parent 18b8c41 commit e246cdc
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 50 deletions.
51 changes: 6 additions & 45 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ The package can be installed by adding `ex_cldr_currencies` to your list of depe
```elixir
def deps do
[
{:ex_cldr_currencies, "~> 2.5"}
{:ex_cldr_currencies, "~> 2.13"}
]
end
```
Expand All @@ -19,7 +19,7 @@ end

[ISO4217](https://en.wikipedia.org/wiki/ISO_4217) permits the creation of private use currencies. These are denoted by currencies that start with "X" followed by two characters. New currencies can be created with `Cldr.Currency.new/2` however in order to do so a supervisor must be started which maintains an `:ets` table that holds the custom currencies.

Since the currencies are stored in an `:ets` table they are transient and will be lost on application restart. It is the developers responsibility to define the required private use currencies on application restart. One option is to use a callback function as described below.
Since the currencies are stored in an `:ets` table they are transient and will be lost on application restart. It is the developers responsibility to define the required private use currencies on application restart.

### Starting the private use currency supervisor

Expand All @@ -43,51 +43,12 @@ defmodule MyApp do

opts = [strategy: :one_for_one, name: MoneyTest.Supervisor]
Supervisor.start_link(children, opts)

# Load your custom currencies now that the
# Cldr.Currency genserver is running
# load_my_custom_currencies()
end
end
```

### Loading private use currencies at application start
If private use currencies are required then defining them when the application starts can be accomplished by providing a callback to the `Cldr.Currency` supervisor as either a `{Module, :function, args}` tuple or as an anonymous function that takes two arguments.

The callback will be called when the custom currency supervisor is starting. The callback function is expected (but not required) to call `Cldr.Currency.new/2` to load custom currencies. Most typically this would be done to load currency definitions from a database (or other serialization) and call `Cldr.Currency.new/2` for each custom currency. In that way, any application code that depends upon the availability of custom currencies can be assured that they are available since the callback will be invoked before the application code starts.

If the callback returns `:error` or `{:error, String.t()}` then the custom currency superisor is shutdown and error is returned.

Here are some examples:
```elixir
# Using an anonymous function
defmodule MyApp.Application do
use Application

def start(_type, _args) do
# Start the service which maintains the
# :ets table that holds the private use currencies
children = [
{Cldr.Currency, [callback: fn pid, table ->
"#{inspect pid}: Starting private use currency store for #{inspect table}" end]}
]

opts = [strategy: :one_for_one, name: MyApp.Supervisor]
Supervisor.start_link(children, opts)
end
end

# Using an MFA
defmodule MyApp.Application do
use Application

def start(_type, _args) do
# In this example, the `pid` and `table` will be
# prepended to the `args`. In this example therefore
# the callback will be `MyAppModule.my_function(pid, table)`
children = [
{Cldr.Currency, [callback: {MyAppModule, :my_function, []}]}
]

opts = [strategy: :one_for_one, name: MyApp.Supervisor]
Supervisor.start_link(children, opts)
end
end

```
10 changes: 5 additions & 5 deletions lib/cldr/eternal/supervisor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ defmodule Cldr.Eternal.Supervisor do
alias Cldr.Eternal.Table

@doc """
Starts an Eternal Supervision tree which manages the two internal servers.
This returns a Tuple containing the table name, so it cannot be used inside a
Supervision tree directly. If you want to use this Supervisor, you should go
via the main Eternal module.
Starts an Eternal Supervision tree which manages the two internal servers.
This returns a Tuple containing the table name, so it cannot be used inside a
Supervision tree directly. If you want to use this Supervisor, you should go
via the main Cldr.Eternal module.
"""
# @spec start_link(name :: atom, ets_opts :: Keyword.t, opts :: Keyword.t) ::
# { :ok, pid, Table.t } | :ignore |
Expand Down

0 comments on commit e246cdc

Please sign in to comment.