Delegator (delegator v0.2.0)

Copy Markdown View Source

Delegates functions and macros from one module to another.

Delegator extends Elixir's built-in delegation mechanism by providing powerful macros for delegating multiple functions and macros at once, with fine-grained control over which items are delegated and how they are named.

Shared options

  • :as - Rename delegated definitions. See Delegator.AliasesMap.new/1.
  • :except - Exclude specific defintions. See Delegator.AritiesMap.new/1.
  • :only - Include only specific defintions. See Delegator.AritiesMap.new/1.
  • :prefix - Add prefix to delegated names. May be an atom or a string.
  • :suffix - Add suffix to delegated names. May be an atom or a string.
  • :to - Required. Which module to delegate to.

Passing nil to any option causes it to be ignored.

Overrides

Functions delegated with defdelegateall are automatically marked as overridable, allowing you to override them in your module.

Macros are never overridable.

Summary

Functions

Defines delegating functions for all functions in a module.

Defines delegating macros for all macros in a module.

Define delegating functions and macros for everything in a module.

Defines a macro that delegates to another module.

Functions

defdelegateall(opts \\ [])

(macro)

Defines delegating functions for all functions in a module.

See the "Shared options" section at the module documentation for more options.

defdelegateallmacros(opts \\ [])

(macro)

Defines delegating macros for all macros in a module.

See the "Shared options" section at the module documentation for more options.

defdelegateeverything(opts \\ [])

(macro)

Define delegating functions and macros for everything in a module.

See the "Shared options" section at the module documentation for more options.

defdelegatemacro(arg, opts \\ [])

(macro)

Defines a macro that delegates to another module.

Similar to defdelegate/2 but for macros.

Options

  • :to - the module to dispatch to. Required.
  • :as - the macro to call on the target given in :to. Optional. Defaults to the name of the macro being delegated.

delegate_name(arg, aliases, prefix, suffix)

Build a delegation name.

Takes the original definition as a name-arity tuple. It then looks for any aliases matching that definition. If no alias is found, applies a prefix and/or suffix when provided.

Prefix and suffix are not applied when an alias is provided, so the alias is honored as provided.

When the definition name ends with ! or ?, the suffix is applied right before these characters.

When the definition starts with _, no separator is placed after the prefix. When it ends with _, no separator is placed before the suffix.

Examples

iex> Delegator.delegate_name({:foo, 1}, Delegator.AliasesMap.new(bar: :foo), :pre, :post)
"bar"

iex> Delegator.delegate_name({:foo, 1}, Delegator.AliasesMap.new(%{{:bar, 1} => :foo}), :pre, :post)
"bar"

iex> Delegator.delegate_name({:foo, 1}, Delegator.AliasesMap.new(), :pre, :post)
"pre_foo_post"

iex> Delegator.delegate_name({:foo!, 1}, Delegator.AliasesMap.new(), :pre, :post)
"pre_foo_post!"

iex> Delegator.delegate_name({:__foo__, 1}, Delegator.AliasesMap.new(), :pre, :post)
"pre__foo__post"