Nebulex.Utils (Nebulex v3.0.0-rc.1)

View Source

General purpose utilities.

Summary

Functions

Concatenates a list of "camelized" aliases and returns a new alias.

A wrapper for Keyword.get/3 but validates the returned value invoking the function valid?.

Convenience guard to determine whether the given argument e is a Nebulex exception or not.

Convenience guard to determine whether the given argument e is a Nebulex wrappable exception.

Custom guard to validate whether the given value is a timeout.

Returns the implemented behaviours for the given module.

Convenience macro for unwrapping a function call result and deciding whether to raise an exception or return the unwrapped value.

Convenience macro for wrapping the given exception into a tuple in the shape of {:error, exception}.

Convenience macro for wrapping the given call result into a tuple in the shape of {:ok, result}.

Functions

camelize_and_concat(list)

@spec camelize_and_concat([atom() | binary() | number()]) :: atom()

Concatenates a list of "camelized" aliases and returns a new alias.

It handles binaries, atoms, and numbers.

Examples

iex> Nebulex.Utils.camelize_and_concat([Foo, :bar])
Foo.Bar

iex> Nebulex.Utils.camelize_and_concat([Foo, "bar"])
Foo.Bar

iex> Nebulex.Utils.camelize_and_concat([Foo, "Bar", 1])
:"Elixir.Foo.Bar.1"

get_option(opts, key, expected, valid?, default \\ nil)

@spec get_option(keyword(), atom(), binary(), (any() -> boolean()), fun() | any()) ::
  any()

A wrapper for Keyword.get/3 but validates the returned value invoking the function valid?.

Raises an ArgumentError in case the validation fails.

Examples

iex> Nebulex.Utils.get_option(
...>   [keys: [1, 2, 3]],
...>   :keys,
...>   "a list with at least one element",
...>   &((is_list(&1) and length(&1) > 0) or is_nil(&1))
...> )
[1, 2, 3]

iex> Nebulex.Utils.get_option(
...>   [],
...>   :keys,
...>   "a list with at least one element",
...>   &((is_list(&1) and length(&1) > 0) or is_nil(&1))
...> )
nil

iex> Nebulex.Utils.get_option(
...>   [],
...>   :keys,
...>   "a list with at least one element",
...>   &((is_list(&1) and length(&1) > 0) or is_nil(&1)),
...>   fn -> "default" end
...> )
"default"

iex> Nebulex.Utils.get_option(
...>   [keys: 123],
...>   :keys,
...>   "non empty list",
...>   &((is_list(&1) and length(&1) > 0) or is_nil(&1))
...> )
** (ArgumentError) invalid value for :keys option: expected non empty list, got: 123

is_nebulex_exception(e)

(macro)

Convenience guard to determine whether the given argument e is a Nebulex exception or not.

Example

iex> import Nebulex.Utils, only: [is_nebulex_exception: 1]
iex> is_nebulex_exception(%Nebulex.Error{reason: :error})
true
iex> is_nebulex_exception(%{})
false

is_nebulex_wrappable_exception(e)

(macro)

Convenience guard to determine whether the given argument e is a Nebulex wrappable exception.

Example

iex> import Nebulex.Utils, only: [is_nebulex_wrappable_exception: 1]
iex> is_nebulex_wrappable_exception(%Nebulex.Error{reason: :error})
true
iex> is_nebulex_wrappable_exception(%Nebulex.KeyError{reason: :error})
true
iex> is_nebulex_wrappable_exception(%Nebulex.CacheNotFoundError{})
false
iex> is_nebulex_wrappable_exception(%Nebulex.QueryError{})
false
iex> is_nebulex_wrappable_exception(%{})
false

is_timeout(value)

(macro)

Custom guard to validate whether the given value is a timeout.

Examples

iex> import Nebulex.Utils, only: [is_timeout: 1]
iex> is_timeout(1)
true
iex> is_timeout(:infinity)
true
iex> is_timeout(-1)
false
iex> is_timeout(1.0)
false
iex> is_timeout("")
false
iex> is_timeout(nil)
false

module_behaviours(module)

@spec module_behaviours(module()) :: [module()]

Returns the implemented behaviours for the given module.

unwrap_or_raise(call)

(macro)

Convenience macro for unwrapping a function call result and deciding whether to raise an exception or return the unwrapped value.

Example

iex> import Nebulex.Utils
iex> unwrap_or_raise {:ok, "ok"}
"ok"
iex> unwrap_or_raise {:error, %Nebulex.Error{reason: :error}}
** (Nebulex.Error) command failed with reason: :error
iex> unwrap_or_raise :other
:other

wrap_error(exception, opts)

(macro)

Convenience macro for wrapping the given exception into a tuple in the shape of {:error, exception}.

Example

iex> import Nebulex.Utils
iex> wrap_error Nebulex.Error, reason: :error
{:error, %Nebulex.Error{reason: :error}}

wrap_ok(call)

(macro)

Convenience macro for wrapping the given call result into a tuple in the shape of {:ok, result}.

Example

iex> import Nebulex.Utils
iex> wrap_ok "hello"
{:ok, "hello"}