ExUtils.Configs (ExUtils v1.0.1)

Copy Markdown View Source

Simplified helpers to work with Application configs in Elixir.

Before using this module, you must add the following to your config.exs file:

config :ex_utils, otp_app: :your_app

Replace :your_app with the actual OTP app atom.

Summary

Functions

Build the absolute path of a file in the priv/ directory, given its relative path.

Gets a config value for the given access path.

Reads the application configs of the OTP app.

Same as get/3 but raises when the value is nil or not found and no default is provided.

Read a file stored in the priv/ directory.

Functions

from_priv!(path)

@spec from_priv!(binary()) :: binary()

Build the absolute path of a file in the priv/ directory, given its relative path.

This is especially useful in compiled elixir releases.

Examples

During development, the returned path might look like:

iex> ExUtils.Configs.from_priv!("templates/email.html")
"priv/templates/email.html"

In a compiled release, it might look like:

iex> ExUtils.Configs.from_priv!("templates/email.html")
"/opt/my_app/lib/my_app-1.0.0/priv/templates/email.html"

get(context, keys, default \\ nil)

@spec get(atom(), atom() | list(), any()) :: any()

Gets a config value for the given access path.

Returns the given default when the value is nil or does not exist.

Examples

Given config :your_app, :features, uploads: [enabled: true]:

iex> ExUtils.Configs.get(:features, [:uploads, :enabled])
true
iex> ExUtils.Configs.get(:features, [:uploads, :max_size], :unlimited)
:unlimited

get!(context)

@spec get!(atom()) :: any()

Reads the application configs of the OTP app.

Raises if the value is nil or does not exist.

Example

Given the following application config:

config :your_app, :mailer, adapter: MyApp.LocalAdapter

It can be retrieved with:

iex> ExUtils.Configs.get!(:mailer)
[adapter: MyApp.LocalAdapter]

get!(context, keys, default \\ nil)

@spec get!(atom(), atom() | list(), any()) :: any()

Same as get/3 but raises when the value is nil or not found and no default is provided.

read_from_priv!(path)

@spec read_from_priv!(binary()) :: binary()

Read a file stored in the priv/ directory.

Accepts a relative path of file inside priv.

Example

Given a priv/messages/welcome.txt file containing Welcome!:

iex> ExUtils.Configs.read_from_priv!("messages/welcome.txt")
"Welcome!"