Skogsrå v1.3.2 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
.
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 isnil
will use the default name. This option is ignored if the optionskip_system
istrue
(default isfalse
).namespace
- Namespace of the variable.skip_system
- Iftrue
, doesn't look for the variable value in the system. Defaults tofalse
.skip_config
- Iftrue
, doesn't look for the variable value in the configuration. Defaults tofalse
.required
- Errors when the value isnil
. Defaults tofalse
.cached
- Caches the value on the first read. Defaults totrue
.
e.g:
For the following declaration:
app_env :db_password, :myapp, [:mydb, :password],
default: "password",
will generate:
db_password/0
anddb_password/1
for getting the variable's value without or with namespace respectively. It returns:ok
and:error
tuples.db_password!/0
anddb_password!/1
for getting the variable's value without or with namespace respectively. It fails on error.reload_db_password/0
andreload_db_password/1
for reloading the variable's value in the cache without or with namespace respectively.put_db_password/1
andput_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:
- Look for the value of
$MYAPP_MYDB_PASSWORD
OS environment variable. If it'snil
, then it will try 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.
- Return the value of the default value or
nil
.
A call to db_password/1
with namespace Test
will try to:
- Look for the value of
$TEST_MYAPP_MYDB_PASSWORD
OS environment variable. If it'snil
, then it will try 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.
- Return the value of the default value or
nil
.