Attributes (attributes v0.2.0) View Source

Attributes offers utility functions to manipulate complex attributes on modules.

A typical usage could be inside macros that need to enrich modules before their compilation. You can set, get or delete attributes' tree using partial or full path.

Example

defmodule MyModule do
  Attributes.set(__MODULE__, [:path, :to, :attr], :value)
end

Attributes supports nested maps and keyword. The previous assignment could be rewritten as follow:

Example

Attributes.set(MODULE, [:path], [to: [attr: :value]])

Example

Attributes.set(MODULE, [:path], %{to: %{attr: :value}})

After defining an attribute, you can obtain its value using get/2, get/3 or get!/2 methods.

Example

iex> Attributes.get(MyModule, [:path, :to, :attr])
iex> :value

Example

iex> Attributes.get(MyModule, [:path, :to])
iex> [attr: :value]

Link to this section Summary

Functions

Deletes attribute by path.

Deletes attribute by path and raises if not found.

Gets attribute by path.

Gets attribute by path with default.

Gets attribute by path and raises if not found.

Sets attribute to path.

Sets attribute to path and raise error if already defined.

Link to this section Functions

Deletes attribute by path.

It does not raise if the path is not found.

Example

Attributes.delete(MyModule, [:path])

Deletes attribute by path and raises if not found.

It is the extension of delete/2 that requires the value and the path to be defined:

  • path should exist
  • value should not be nil

Example

Attributes.delete!(MyModule, [:path])

Gets attribute by path.

It returns nil if path is not found.

Example

Attributes.get(MyModule, [:path])
Link to this function

get(module, path, default)

View Source

Gets attribute by path with default.

It returns default if path is not found.

Example

Attributes.get(MyModule, [:path], :default)

Gets attribute by path and raises if not found.

It is the extension of get/2 that requires the value and the path to be defined:

  • path should exist
  • value should not be nil

Example

Attributes.get!(MyModule, [:path])
Link to this function

set(module, path, value)

View Source

Sets attribute to path.

Available at compile time only.

Example

Attributes.get(MyModule, [:path], :value)
Link to this function

set!(module, path, value)

View Source

Sets attribute to path and raise error if already defined.

Available at compile time only. It is the extension of set/3 that requires the path to don't have a value.

Example

Attributes.set!(MyModule, [:path], :value)