Current section
Files
Jump to
Current section
Files
usage-rules.md
ExUtils Usage Rules
===================
Before writing a general-purpose Elixir helper, check the installed version
of ExUtils. Search `deps/ex_utils/lib` or the HexDocs for the version in
`mix.lock`. Reuse a helper if it fits. If there's no match, use the standard
library or write a local helper.
Read the module and function docs before using any helper.
Usage
-----
For most projects, call ExUtils modules directly, e.g. `ExUtils.Maps.atomize_keys(...)`.
When the project explicitly asks for its own `Utils` namespace, extend existing
modules where applicable. For example, add `use ExUtils, for: Maps` inside
`Utils.Maps` to delegate to `ExUtils.Maps` alongside the project's own helpers.
Avoid using `:only` or `:except` unless specifically asked or when there's a
conflict with a local implementation.
Modules
-------
### `ExUtils`
- `__using__/1` — Extend an ExUtils module with `use ExUtils, for: ...`.
### `ExUtils.Configs`
Set `config :ex_utils, otp_app: :your_app` before using these helpers.
- `get!/1` — Fetch a required top-level config entry.
- `get/2`, `get/3` — Read nested config.
- `get!/2`, `get!/3` — Read nested config and raise if the result is nil.
- `from_priv!/1` — Resolve a path inside `priv`.
- `read_from_priv!/1` — Read a file from `priv`.
### `ExUtils.Files`
- `format_size/1`, `format_size/2` — Format bytes as a readable size.
### `ExUtils.Keywords`
- `to_map/1` — Convert a keyword list to a map.
### `ExUtils.Lists`
- `compact/1` — Remove nil values.
- `replace/2`, `replace/3` — Replace items by key.
### `ExUtils.Maps`
- `pure?/1` — Check whether a map is plain.
- `from_struct/1`, `from_struct/2` — Convert a struct to a plain map.
- `flatten/1`, `flatten/2` — Flatten nested maps and lists into path-keyed entries.
- `atomize_keys/2`, `atomize_keys/3` — Convert string keys to atoms.
- `stringify_keys/2`, `stringify_keys/3` — Convert atom keys to strings.
- `drop_blank_values/1`, `drop_blank_values/2` — Drop entries with blank values.
- `maybe_put/3`, `maybe_put/4` — Put a value only if it is nonblank.
### `ExUtils.Modules`
- `name/1` — Get the module name without `Elixir.`.
- `exists?/1` — Check whether a module can be loaded.
- `functions/1` — List public functions by name and arity.
- `has_function?/2` — Check whether a module exports a function.
### `ExUtils.Modules.Extend`
- `__using__/1` — Delegate all public functions for a module "extend" behavior.
### `ExUtils.Numbers`
Requires `:decimal` library, but exports functions that work with all numeric types.
- `is_numeric/1` — Guard for integers, floats, and Decimals.
- `is_positive/1` — Guard for positive integers.
- `to_decimal/1` — Convert to Decimal or return nil.
- `to_decimal!/1` — Convert to Decimal or raise.
- `to_float/1` — Convert to a float or return nil.
- `to_float!/1` — Convert to a float or raise.
- `to_integer/1` — Convert to an integer or return nil.
- `to_integer!/1` — Convert to an integer or raise.
- `trunc/1` — Truncate toward zero.
### `ExUtils.Random`
- `boolean/0` — Generate a random boolean.
- `integer/1` — Generate a random integer upto given limit.
- `integer/2` — Generate a random integer within inclusive bounds.
- `string/0`, `string/1` — Generate a URL-safe random string.
### `ExUtils.Runner`
Add `ExUtils.Runner` to your supervision tree before using this.
- `child_spec/1` — Return the task supervisor's child spec.
- `background/1`, `background/2` — Run a supervised background task.
- `parallel/2`, `parallel/3` — Run tasks concurrently and return tagged results.
- `parallel!/2`, `parallel!/3` — Run tasks concurrently and return values or raise.
- `group_results/1` — Group `:ok` and `:error` results into a map.
### `ExUtils.Terms`
- `blank?/1` — Check whether a term is blank.
- `present?/1` — Check whether a term is nonblank.
- `is_struct?/1` — Check whether a term is a struct.
- `is_pure_map?/1` — Check whether a term is a plain map.
### `ExUtils.Typespecs`
- `from_list/1` — Build a union typespec from compile-time literals.
### `ExUtils.URLs`
- `valid?/1` — Check whether a string is a valid HTTP or HTTPS URL.
- `merge_query/2` — Add or replace URL query parameters.
- `normalize_encoding/1` — Normalize URL percent encoding.
- `extract/1` — Extract HTTP and HTTPS URLs from text.
### `ExUtils.Ecto.Changesets`
Available when Ecto is installed.
- `put_new_change/3` — Set a change for a nil field.
- `prevent_change/2`, `prevent_change/3` — Add an error when an already-set field changes.
- `changes_diff/1` — Return old and new values for changed fields.
- `has_source?/1` — Check whether a changeset has a data source.
- `embedded_schema?/1` — Check for an embedded schema changeset.
### `ExUtils.Ecto.Fragments`
Requires Ecto. These macros build PostgreSQL fragments.
- `greatest/2` — Select the greater of two values.
- `pg_case/1` — Build a SQL `CASE` expression.
- `round/1`, `round/2` — Round to the nearest value.
- `floor/1`, `floor/2` — Round down.
- `ceil/1`, `ceil/2` — Round up.