Skogsrå v2.0.3 Skogsra View Source

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

Link to this section Summary

Functions

For now is just equivalent to use import Skogsra.

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

Link to this section Functions

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.

Options:

  • default - Default value for the variable in case is not present.
  • type - Type of the variable. Used for casting the value. By default, casts the value to the same type of the default value. If the default value is not present, defaults to :binary. The available values are: :binary, :integer, :float, :boolean, :atom. Additionally, you can provide {module, function} for custom types. The function must receive the binary and return the custom type.
  • os_env - Alias for the variable in the OS. If the alias is nil will use the default name. This option is ignored if the option skip_system is true (default is false).
  • namespace - Namespace of the variable.
  • skip_system - If true, doesn't look for the variable value in the system. Defaults to false.
  • skip_config - If true, doesn't look for the variable value in the configuration. Defaults to false.
  • required - Errors when the value is nil. Defaults to false.
  • cached - Caches the value on the first read. Defaults to true.

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:

  1. Look for the value of $MYAPP_MYDB_PASSWORD OS environment variable. If it's nil, then it will try 2.
  2. Look for the value in the configuration e.g:
   config :myapp,
     mydb: [password: "some password"]

If it's nil, then it will try 3.

  1. Return the value of the default value or nil.

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

  1. Look for the value of $TEST_MYAPP_MYDB_PASSWORD OS environment variable. If it's nil, then it will try 2.
  2. Look for the value in the configuration e.g:
   config :myapp, Test,
     mydb: [password: "some password"]

If it's nil, then it will try 3.

  1. Return the value of the default value or nil.