Genesis.Aspect behaviour (genesis v0.5.1)

View Source

Provides 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

object()

@type object() :: integer() | atom() | binary()

props()

@type props() :: Enumerable.t()

Callbacks

all()

@callback all() :: [{object(), struct()}]

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}}]

at_least(atom, integer)

@callback at_least(atom(), integer()) :: [{object(), struct()}]

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}}]

at_most(atom, integer)

@callback at_most(atom(), integer()) :: [{object(), struct()}]

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}}]

attach(object, props)

@callback attach(object(), props()) :: :ok

Attaches an aspect to an object.

between(atom, integer, integer)

@callback between(atom(), integer(), integer()) :: [{object(), struct()}]

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}}]

cast(props)

@callback cast(props()) :: map()

Casts the given properties into a map of permitted values. This function normalizes input that can be used to create an aspect.

exists?(object)

@callback exists?(object()) :: boolean()

Returns true if the aspect is attached to the given object.

get(object)

@callback get(object()) :: struct() | nil

Retrieves the aspect attached to an object. Returns the aspect struct if present or nil.

Examples

iex> Health.get(1)
%Health{current: 100}

get(object, any)

@callback get(object(), any()) :: struct() | any()

Same as get/1, but returns a default value if the aspect is not present.

handle_event(atom, object, map)

@callback handle_event(atom(), object(), map()) :: {:cont, map()} | {:halt, map()}

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.

init()

@callback init() :: {atom() | [atom()]}

Initializes the aspect ETS table. Should return an atom with the name of the table and a list of events.

match(props)

@callback match(props()) :: [{object(), struct()}]

Returns all aspects that match the given properties.

Examples

iex> Moniker.match(name: "Tripida")
[{1, %Moniker{name: "Tripida"}}]

new(props)

@callback new(props()) :: struct()

Creates a new aspect by casting the given properties. The given properties are passed to the cast/1 function.

remove(object)

@callback remove(object()) :: :ok | :noop

Removes an aspect from an object. Returns :noop if the aspect is not present.

update(object, props)

@callback update(object(), props()) :: :ok | :noop

Updates an aspect attached to an object. Will return :noop if the aspect is not present.