envio v0.4.4 Envio.Utils View Source

Multipurpose utils, required for the Envío proper functioning, like:

  • fq_name to construct fully-qualified names for channels to prevent clashes

Link to this section Summary

Functions

Returns the anonymous function that either loads the system environment variable or simply returns the value statically loaded from config file

Produces a fully-qualified name out of namespace in a form of a module name atom and a method name (atom or binary.)

Fetches the value from the map by given key and returns both the value and the map without this key

Safely converts any term to binary

Link to this section Functions

Link to this function

config_value(var) View Source
config_value(input :: binary() | {:system, binary()}) :: term()

Returns the anonymous function that either loads the system environment variable or simply returns the value statically loaded from config file.

Examples

iex> # config :envio, :binary_value, "FOO"
iex> Envio.Utils.config_value(Application.get_env(:envio, :binary_value)).()
"FOO"

iex> # config :envio, :env_value, {:system, "FOO"}
iex> System.put_env("FOO", "42")
iex> Envio.Utils.config_value(Application.get_env(:envio, :env_value)).()
"42"
Link to this function

fq_name(namespace_or_name, name_or_nil \\ nil) View Source
fq_name(
  binary() | atom() | {atom(), atom() | binary()},
  nil | atom() | binary()
) :: binary()

Produces a fully-qualified name out of namespace in a form of a module name atom and a method name (atom or binary.)

Examples

iex> Envio.Utils.fq_name("foo")
"foo"
iex> Envio.Utils.fq_name({Foo.Bar, "baz"})
"foo/bar.baz"
iex> Envio.Utils.fq_name(Foo.Bar, "baz")
"foo/bar.baz"
iex> Envio.Utils.fq_name(Foo.Bar, :baz)
"foo/bar.baz"
iex> Envio.Utils.fq_name("Foo.Bar", "baz")
"Foo.Bar.baz"
Link to this function

get_delete(input, key, default \\ nil) View Source
get_delete(input :: map(), key :: atom(), default :: term()) :: term()

Fetches the value from the map by given key and returns both the value and the map without this key.

Examples

iex> Envio.Utils.get_delete(%{foo: :bar, baz: 42}, :foo)
{:bar, %{baz: 42}}
iex> Envio.Utils.get_delete(%{baz: 42}, :foo)
{nil, %{baz: 42}}
iex> Envio.Utils.get_delete(%{baz: 42}, :foo, :bar)
{:bar, %{baz: 42}}
Link to this function

smart_to_binary(list) View Source
smart_to_binary(input :: term()) :: binary()

Safely converts any term to binary.

Examples

iex> Envio.Utils.smart_to_binary("foo")
"foo"
iex> Envio.Utils.smart_to_binary(42)
"42"
iex> Envio.Utils.smart_to_binary(%{foo: :bar, baz: 42})
"%{baz: 42, foo: :bar}"
iex> Envio.Utils.smart_to_binary([foo: :bar, baz: 42])
"[foo: :bar, baz: 42]"