Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do we need behaviours? #16

Open
SimonLab opened this issue Nov 21, 2018 · 1 comment
Open

Do we need behaviours? #16

SimonLab opened this issue Nov 21, 2018 · 1 comment
Labels
discuss question Further information is requested

Comments

@SimonLab
Copy link
Member

The example is using behaviours to define the structure of the API interface:

@callback insert(struct) :: {:ok, Ecto.Schema.t()} | {:error, Ecto.Changeset.t()}
@callback get(integer) :: Ecto.Schema.t() | nil | no_return()
@callback all() :: [Ecto.Schema.t()]
@callback update(Ecto.Schema.t(), struct) ::
{:ok, Ecto.Schema.t()} | {:error, Ecto.Changeset.t()}
@callback get_history(Ecto.Schema.t()) :: [Ecto.Schema.t()] | no_return()

The implementation of the API is then done in the before_compile macro:

defmacro __before_compile__(_env) do
quote do
import Ecto.Query
def insert(attrs) do
%__MODULE__{}
|> __MODULE__.changeset(attrs)
|> Repo.insert()
end
def get(entry_id) do
sub =
from(
m in __MODULE__,
where: m.entry_id == ^entry_id,
order_by: [desc: :inserted_at],
limit: 1,
select: m
)
query = from(m in subquery(sub), where: not m.deleted, select: m)
Repo.one(query)
end

...
However I'm not sure behaviours are needed for the append only example.
From my understanding it makes sense to use behaviours when multiple modules need to provide the same functions but with a different logic of implemention. In our example only the AppendOnlyLog module is implementing the behaviours.

For a learning purpose it makes sense to see how behaviours works 👍 but I'm not sure if they provide a lot of values for this repository?

Should we still keep them to demonstrate how they can be used?

ref: https://elixir-lang.org/getting-started/typespecs-and-behaviours.html#behaviours

@SimonLab SimonLab added question Further information is requested discuss labels Nov 21, 2018
@nelsonic
Copy link
Member

Related to: #8

@SimonLab SimonLab changed the title Do we need bahaviours? Do we need behaviours? Nov 21, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discuss question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants