get_conf v0.2.0 GetConf
A simple configuration manager for namespaced modules.
Usage
Define your configuration in your config/{env}.exs
:
use Mix.Config
config :app_name, AppName, key: "value"
And access it in your modules. It will go further up the namespace, to find a matching key in the configuration of each module.
GetConf.get_conf(:app_name, AppName, :key)
# => "value"
GetConf.get_conf(:app_name, AppName.Module, :key)
# => "value"
Macro
You can also use the GetConf module inside your module, to implement get_conf/1
and set_conf/2
.
defmodule TestModule
use GetConf, otp_app: :app_name
end
"bar" = TestModule.get_conf(:foo)
Link to this section Summary
Functions
Get a value out of a configured keyword list from the given application/module.
Set a value on the keyword list of the given application/module.
Link to this section Functions
Link to this function
get_conf(otp_app, module, key, default \\ nil)
Get a value out of a configured keyword list from the given application/module.
otp_app
: The name of the otp applicationmodule
: The modulekey
: The key from the Keyword list ot fetch
Returns nil
or the value from the configuration.
Examples
iex> Application.put_env(:get_conf, TestModule, [foo: "bar"])
iex> GetConf.get_conf(:get_conf, TestModule, :foo)
"bar"
Link to this function
set_conf(otp_app, module, key, value)
Set a value on the keyword list of the given application/module.
otp_app
: The name of the otp applicationmodule
: The modulekey
: The key from the Keyword list ot fetchvalue
: The value to set
Raises a RuntimeError
if the configuration is not a keyword list.
Examples
iex> GetConf.set_conf(:get_conf, TestModule, :foo, "value")
iex> Application.get_env(:get_conf, TestModule)
[foo: "value"]