View Source PersistConfig (Persist Config v0.4.31)

Persists the configurations from a list of files during compilation. Also puts the current application name in a module attribute and provides macros get_env/2, get_app_env/3 and get_all_env/1 for concise configuration value retrieval.

Installation

Add persist_config to your list of dependencies in mix.exs. Also include the required configuration files in the package definition of mix.exs:

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

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 selects the files whose configurations will be persisted. Each wildcard path is relative to the root. If no matching files are found, no configurations will be persisted.

Though not required, you can still import the configurations from the above matching files in config/config.exs or friends. For example:

import Config

import_config "persist_this_config.exs"

Even when your project is used as a dependency, this package will load and persist the configurations from the specified files during compilation.

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 and despite any path configuration in the parent app). To achieve this, you may:

1. Use a module attribute as a constant:

use PersistConfig
...
@path get_env(:path)

2. Create a configuration file named, say, config/persist_path.exs:

import Config

config :words_cache, path: "#{File.cwd!()}/assets/words.txt"

3. In mix.exs, specify a package definition like this:

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

Example 1

The current application name is in @app by default:

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

Example 2

The current application name is in @my_app as an option:

use PersistConfig, app: :my_app
...
@my_attr get_app_env(@my_app, :my_attr)

Example 3

You can use macro get_env/2 to retrieve configuration values at runtime when configuration is done by config/config.exs and friends:

use PersistConfig
...
defp level, do: get_env(:level, :all)

Summary

Functions

Persists the configurations from a list of files during compilation. Also puts the current application name in a module attribute.

Returns all key-value pairs for app.

Returns the value for key in the given app's environment.

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

Functions

Link to this macro

__using__(options \\ [])

View Source (macro)

Persists the configurations from a list of files during compilation. Also puts the current application name in a module attribute.

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 selects the files whose configurations will be persisted. Each wildcard path is relative to the root. If no matching files are found, no configurations will be persisted.

Link to this macro

get_all_env(app)

View Source (since 0.4.27) (macro)

Returns all key-value pairs for app.

Link to this macro

get_app_env(app, key, default \\ nil)

View Source (since 0.4.24) (macro)

Returns the value for key in the given app's environment.

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

Link to this macro

get_env(key, default \\ nil)

View Source (since 0.4.0) (macro)

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

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