Attributes (attributes v0.1.1) View Source
Attributes offers utility functions to set and get complex attributes on modules.
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
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
Gets attribute by path.
It returns nil if path is not found.
Example
Attributes.get(MyModule, [:path])
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/3
that requires the value or the path to be defined:
- path should exist
- value should not be
nil
Example
Attributes.get!(MyModule, [:path])
Sets attribute to path.
Available at compile time only.
Example
Attributes.get(MyModule, [:path], :value)
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)