-
Notifications
You must be signed in to change notification settings - Fork 8
/
entity.ex
47 lines (36 loc) · 1.62 KB
/
entity.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
defmodule Ecspanse.Entity do
@moduledoc """
Entities are only identifiers. An entity exists only if it holds at least one component.
The entities per se are not persisted.
Entities are represented as a struct with `id` as the only field.
## Examples
```elixir
%Ecspanse.Entity{id: "cfa1ad89-44b6-4d1f-8590-186354be9158"}
```
"""
alias __MODULE__
@typedoc "The entity struct."
@type t :: %Entity{
id: id()
}
@type id :: binary()
@typedoc """
An `entity_spec` is the definition required to create an entity.
## Options
- `:id` - a custom unique ID for the entity (binary). If not provided, a random UUID will be generated.
- `:components` - a list of `t:Ecspanse.Component.component_spec/0` to be added to the entity.
- `:children` A list of `t:Ecspanse.Entity.t/0` to be added as children to the entity. Children entities should already exist.
- `:parents` A list of `t:Ecspanse.Entity.t/0` to be added as parents to the entity. Parent entities should already exist.
> #### Note {: .info}
> At least one of the `:components`, `:children` or `:parents` options must be provided,
> otherwise the entity cannot be persisted.
> #### Entity ID {: .warning}
> The entity IDs must be unique. Attention when providing the `:id` option as part of the `entity_spec`.
> If the provided ID is not unique, spwaning entities will raise an error.
"""
@type entity_spec :: {Entity, opts :: keyword()}
@enforce_keys [:id]
defstruct [:id]
@spec fetch(Ecspanse.Entity.id()) :: {:ok, t()} | {:error, :not_found}
defdelegate fetch(id), to: Ecspanse.Query, as: :fetch_entity
end