env_helper v0.2.1 EnvHelper

Helpers for enviroment and application variables.

Link to this section Summary

Functions

defines a method name/0 which returns either the application variable for appname[key] or the provided alt value

creates a method name/0 which returns either alt or the environment variable set for the upcase version name

creates a method name/0 which returns either alt or the environment variable set for the upcase version name, cast to an integer

Link to this section Functions

Link to this macro app_env(name, list, alt) (macro)

defines a method name/0 which returns either the application variable for appname[key] or the provided alt value.

Works best in simple cases, such as config :appname, :key, value

Example (simple app variable)

defmodule AppEnvs do
  import EnvHelper
  app_env(:port, [:appname, :port], 1234)
end

> AppEnvs.port
1234
> Application.put_env(:appname, :port, 5678)
:ok
> AppEnvs.port
5678

Example (nested app variable)

defmodule AppEnvs do
  import EnvHelper

  defmodule Section do
    app_env(:secret, [:appname, :section, :secret], "default secret")
  end
end

> AppEnvs.Section.secret
"default secret"
> Application.puts_env(:appname, :section, [secret: "my super secret"])
:ok
> AppEnvs.Section.secret
"my super secret"
Link to this macro system_env(name, alt) (macro)

creates a method name/0 which returns either alt or the environment variable set for the upcase version name.

Paramenters

  • name :: atom

    The name of a system environment variable, downcase, as an atom

  • alt :: any

    The value used if the system variable is not set.

Example

defmodule EnvSets do
  import EnvHelper
  system_env(:app_url, "localhost:2000")
end

> EnvSets.app_url
"localhost:2000"

> System.put_env("APP_URL", "app.io")
:ok

> EnvSets.app_url
"app.io"
Link to this macro system_env(name, alt, atom) (macro)
system_env(atom(), any(), :string_to_list) :: list()
system_env(atom(), any(), :string_to_boolean) :: boolean()
system_env(atom(), any(), :string_to_integer) :: integer()

creates a method name/0 which returns either alt or the environment variable set for the upcase version name, cast to an integer.

Paramenters

  • name :: atom

    The name of a system environment variable, downcase, as an atom

  • alt :: any

    The value used if the system variable is not set.

  • :string_to_integer

    forces the returned value to be an integer.

Example

defmodule EnvSets do
  import EnvHelper
  system_env(:app_port, 2000, :string_to_integer)
end
> EnvSets.app_port
2000
> System.put_env("APP_PORT", "2340")
:ok
> EnvSets.app_port
2340
> System.put_env("APP_PORT", 2360)
:ok
> EnvSets.app_url
2360

creates a method name/0 which returns either alt or the environment variable set for the upcase version name, cast to a boolean.

Paramenters

  • name :: atom

    The name of a system environment variable, downcase, as an atom

  • alt :: any

    The value used if the system variable is not set.

  • :string_to_boolean

    forces the returned value to be a boolean.

Example

defmodule EnvSets do
  import EnvHelper
  system_env(:auto_connect, false, :string_to_boolean)
end
> EnvSets.auto_connect
false
> System.put_env("AUTO_CONNECT", "true")
:ok
> EnvSets.auto_connect
true
> System.put_env("AUTO_CONNECT", "false")
:ok
> EnvSets.auto_connect
false

creates a method name/0 which returns either alt or the environment variable set for the upcase version name, cast to a List.

Paramenters

  • name :: atom

    The name of a system environment variable, downcase, as an atom

  • alt :: any

    The value used if the system variable is not set.

  • :string_to_list

    forces the returned value to be a list.

Example

defmodule EnvSets do
  import EnvHelper
  system_env(:destination_urls, "url_one,url_two", :string_to_list)
end
> EnvSets.destination_urls
["url_one", "url_two"]
> System.put_env("DESTINATION_URLS", "url_three,url_four")
:ok
> EnvSets.destination_urls
["url_three", "url_four"]
> System.put_env("AUTO_CONNECT", "url_five")
:ok
> EnvSets.destination_urls
["url_five"]