PersistConfig (Persist Config v0.4.8) View Source

Persists, at compile time, a list of configuration files and puts the current application name in a module attribute.

Also provides a get_env macro for concise configuration value retrieval.

Usage

use PersistConfig supports the following options:

  • :app - module attribute to hold the current application name, defaults to :app
  • :files - (wildcard) paths, defaults to ["config/persist*.exs"]

Option :files lists the configuration files to be persisted. Though not needed, these can still be imported in file config/config.exs. For example: import_config "persist_this_config.exs".

Each entry represents a (wildcard) path relative to the root. If the list is or ends up being empty, no files are persisted.

When your project is used as a dependency, this package will allow the specified configuration files to be persisted.

For example, you may configure some path to read an external file and want to still read that very file when your app is a dependency (without any path configuration in the parent app).

However, such configurations must truly be global and should otherwise be avoided.

Example 1

use PersistConfig, files: ["config/persist_path.exs"]
...
@all_env Application.get_all_env(@app)
@path get_env(:path)

Example 2

use PersistConfig, app: :my_app
...
@my_attr Application.get_env(@my_app, :my_attr)

Example 3

use PersistConfig
...
defp log?, do: get_env(:log?, true)

Installation

Add persist_config to your list of dependencies in mix.exs. Also include the configuration files to be persisted in the package definition of mix.exs as shown below:

def project do
  [
    app: :your_app,
    ...
    deps: deps(),
    package: package(),
    ...
  ]
end
...
def deps do
  [
    {:persist_config, "~> 0.4", runtime: false}
  ]
end
...
defp package do
  [
    files: ["lib", "mix.exs", "README*", "config/persist*.exs"],
    maintainers: ["***"],
    licenses: ["***"],
    links: %{...}
  ]
end

Link to this section Summary

Functions

Returns the value for key in in the current application's environment.

Link to this section Functions

Link to this macro

get_env(key, default \\ nil)

View Source (macro) (since 0.4.0)

Returns the value for key in in the current application's environment.

If the configuration parameter does not exist, returns the default value.