Attributes (attributes v0.4.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, update, get or delete attributes' tree.

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 all attributes.

Deletes attribute in path.

Deletes attribute in path and raises if not found.

Gets all attributes.

Gets attribute in path.

Gets attribute in path with default.

Gets attribute in path and raises if not found.

Sets attribute in path.

Sets attribute in path and raise error if already defined.

Updates attribute in path.

Updates attribute in path with default lambda input value.

Updates attribute in path and raise error if not found.

Link to this section Functions

Deletes all attributes.

Available at compile time only.

Example

Attributes.delete(MyModule)

Deletes attribute in path.

Available at compile time only. It does not raise if the path is not found.

Example

Attributes.delete(MyModule, [:path])

Deletes attribute in path and raises if not found.

Available at compile time only. 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 all attributes.

Example

Attributes.get(MyModule)

Gets attribute in 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 in path with default.

It returns default if path is not found or the value is nil.

Example

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

Gets attribute in 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 in path.

Available at compile time only.

Example

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

set!(module, path, value)

View Source

Sets attribute in 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)
Link to this function

update(module, path, lambda)

View Source

Updates attribute in path.

Available at compile time only.

Example

Attributes.update(MyModule, [:path], & &1 + 1)
Link to this function

update(module, path, default, lambda)

View Source

Updates attribute in path with default lambda input value.

Available at compile time only.

Example

Attributes.update(MyModule, [:path], 41, & &1 + 1)
Link to this function

update!(module, path, lambda)

View Source

Updates attribute in path and raise error if not found.

Available at compile time only. It is the extension of update/3 that requires the value and the path to be defined:

  • path should exist
  • value should not be nil

Example

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