Genesis.Aspect behaviour (genesis v0.5.1)
View SourceProvides common behavior and callbacks for aspects. Aspects are modular pieces of state or behavior that can be attached to objects.
Summary
Callbacks
Returns all aspects of the given type. Returns a list of tuples containing the object and the aspect struct.
Returns all aspects that have the given property with a value greater than or equal to the given minimum.
Returns all aspects that have the given property with a value less than or equal to the given maximum.
Attaches an aspect to an object.
Returns all aspects that have the given property with a value between the given minimum and maximum.
Casts the given properties into a map of permitted values. This function normalizes input that can be used to create an aspect.
Returns true if the aspect is attached to the given object.
Retrieves the aspect attached to an object.
Returns the aspect struct if present or nil
.
Same as get/1
, but returns a default value if the aspect is not present.
Handles events dispatched to this aspect via its parent object. Receives the event name, the object and a map of arguments for the event.
Initializes the aspect ETS table. Should return an atom with the name of the table and a list of events.
Returns all aspects that match the given properties.
Creates a new aspect by casting the given properties.
The given properties are passed to the cast/1
function.
Removes an aspect from an object.
Returns :noop
if the aspect is not present.
Updates an aspect attached to an object.
Will return :noop
if the aspect is not present.
Types
@type props() :: Enumerable.t()
Callbacks
Returns all aspects of the given type. Returns a list of tuples containing the object and the aspect struct.
Examples
iex> Health.all()
[{1, %Health{current: 100}}, {2, %Health{current: 50}}]
Returns all aspects that have the given property with a value greater than or equal to the given minimum.
Examples
iex> Health.at_least(:current, 50)
[{1, %Health{current: 75}}]
Returns all aspects that have the given property with a value less than or equal to the given maximum.
Examples
iex> Health.at_most(:current, 50)
[{1, %Health{current: 25}}]
Attaches an aspect to an object.
Returns all aspects that have the given property with a value between the given minimum and maximum.
Examples
iex> Health.between(:current, 50, 100)
[{1, %Health{current: 75}}]
Casts the given properties into a map of permitted values. This function normalizes input that can be used to create an aspect.
Returns true if the aspect is attached to the given object.
Retrieves the aspect attached to an object.
Returns the aspect struct if present or nil
.
Examples
iex> Health.get(1)
%Health{current: 100}
Same as get/1
, but returns a default value if the aspect is not present.
Handles events dispatched to this aspect via its parent object. Receives the event name, the object and a map of arguments for the event.
Given that the same event is dispatched to all aspects within an object, this
function should return a tuple with :cont
or :halt
to either keep processing
the event or stop propagating the event to the remaining aspects in the pipeline.
Initializes the aspect ETS table. Should return an atom with the name of the table and a list of events.
Returns all aspects that match the given properties.
Examples
iex> Moniker.match(name: "Tripida")
[{1, %Moniker{name: "Tripida"}}]
Creates a new aspect by casting the given properties.
The given properties are passed to the cast/1
function.
@callback remove(object()) :: :ok | :noop
Removes an aspect from an object.
Returns :noop
if the aspect is not present.
Updates an aspect attached to an object.
Will return :noop
if the aspect is not present.