confex v1.4.0 Confex
This is helper module that provides a nice way to read environment configuration at runtime.
Summary
Functions
Fetches a value from the config, or from the environment if {:system, “VAR”} is provided. An optional default value and it’s type can be provided if desired
Same as get/3
, but when you has map
Receives Keyword list with Confex tuples and replaces them with an environment values.
Useful when you want to store configs not in config.exs
Functions
Specs
get(atom, atom, term | nil) :: term
Fetches a value from the config, or from the environment if {:system, “VAR”} is provided. An optional default value and it’s type can be provided if desired.
Example
iex> {test_var, expected_value} = System.get_env |> Enum.take(1) |> List.first
...> Application.put_env(:myapp, :test_var, {:system, test_var})
...> ^expected_value = Elixir.Confex.get(:myapp, :test_var)
...> :ok
:ok
iex> Application.put_env(:myapp, :test_var2, 1)
...> 1 = Elixir.Confex.get(:myapp, :test_var2)
1
iex> System.delete_env("TEST_ENV")
...> Application.put_env(:myapp, :test_var2, {:system, :integer, "TEST_ENV", "default_value"})
...> "default_value" = Elixir.Confex.get(:myapp, :test_var2)
...> System.put_env("TEST_ENV", "123")
...> 123 = Elixir.Confex.get(:myapp, :test_var2)
123
iex> :default = Elixir.Confex.get(:myapp, :missing_var, :default)
:default
Specs
get_map(atom, atom, term | nil) :: Keyword.t
Same as get/3
, but when you has map.
Example
iex> {test_var, expected_value} = System.get_env |> Enum.take(1) |> List.first …> Application.put_env(:myapp, :test_var, [test: {:system, test_var}]) …> [test: ^expected_value] = Elixir.Confex.get_map(:myapp, :test_var) …> :ok :ok
iex> Application.put_env(:myapp, :test_var2, [test: 1]) …> Elixir.Confex.get_map(:myapp, :test_var2) [test: 1]
iex> :default = Elixir.Confex.get_map(:myapp, :other_missing_var, :default) :default
iex> Application.put_env(:myapp, :test_var3, [test: nil]) …> [test: nil] = Elixir.Confex.get_map(:myapp, :test_var3) [test: nil]
Receives Keyword list with Confex tuples and replaces them with an environment values.
Useful when you want to store configs not in config.exs
.
Example
iex> [test: "defaults"] = Elixir.Confex.process_env([test: {:system, "some_test_var", "defaults"}]) [test: "defaults"]