Nebulex.Utils (Nebulex v3.0.0-rc.1)
View SourceGeneral 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
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"
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
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
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
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
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.
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
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}}
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"}