View Source Skogsra (Skogsrå v2.5.0)

This module defines the macros needed to use Skogsra e.g:

defmodule MyApp.Settings do
  use Skogsra

  @envdoc "My hostname"
  app_env :my_hostname, :myapp, :hostname,
    default: "localhost"
end

Summary

Functions

Imports app_env/3 and app_env/4. Additionally generates the function template( For now is just equivalent to use import Skogsra.

Creates a function to retrieve specific environment/application variables values.

Functions

Imports app_env/3 and app_env/4. Additionally generates the function template( For now is just equivalent to use import Skogsra.

Link to this macro

app_env(function_name, app_name, keys, options \\ [])

View Source (macro)

Creates a function to retrieve specific environment/application variables values.

The function created is named function_name and will get the value associated with an application called app_name and one or several parameters keys. Optionally, receives a list of options.

Available options:

OptionTypeDefaultDescription
defaultanynilSets the Default value for the variable.
typeSkogsra.Env.type():binarySets the explicit type for the variable.
os_envbinaryautogeneratedOverrides automatically generated OS environment variable name.
binding_orderSkogra.Env.bindings()[:system, :config]Sets the load order for variable binding.
binding_skipSkogra.Env.bindings()[]Which variable bindings should be skipped.
requiredbooleanfalseWhether the variable is required or not.
cachedbooleantrueWhether the variable should be cached or not.
namespacemodulenilOverrides any namespace.
env_overrideskeyword[]Overrides default and required properties for a specific environment.

e.g:

For the following declaration:

app_env :db_password, :myapp, [:mydb, :password],
  default: "password",

will generate:

  • db_password/0 and db_password/1 for getting the variable's value without or with namespace respectively. It returns :ok and :error tuples.
  • db_password!/0 and db_password!/1 for getting the variable's value without or with namespace respectively. It fails on error.
  • reload_db_password/0 and reload_db_password/1 for reloading the variable's value in the cache without or with namespace respectively.
  • put_db_password/1 and put_db_password/2 for settings a new value for the variable directly to the cache without or with namespace respectively.

A call to db_password/0 will try to get a value:

  1. From the OS environment variable $MYAPP_MYDB_PASSWORD (can be overriden by the option os_env).
  2. From the configuration file e.g:
    config :myapp,
      mydb: [password: "some password"]
  3. From the default value if it exists (In this case, it would return "password").

A call to db_password/1 with namespace Test will try to get a value:

  1. From the OS environment variable $TEST_MYAPP_MYDB_PASSWORD.
  2. From the configuration file e.g:
    config :myapp, Test,
      mydb: [password: "some test password"]
  3. From the OS environment variable $MYAPP_MYDB_PASSWORDT.
  4. From the configuraton file e.g:
    config :myapp,
      mydb: [password: "some password"]
  5. From the default value if it exists. In our example, "password".