Skip to content

Instantly share code, notes, and snippets.

@Papipo
Created June 5, 2015 23:11
Show Gist options
  • Select an option

  • Save Papipo/83c4c9eae71e8591ef7f to your computer and use it in GitHub Desktop.

Select an option

Save Papipo/83c4c9eae71e8591ef7f to your computer and use it in GitHub Desktop.
defmodule EventSourcing.Aggregate do
defmacro __using__(_) do
quote do
use GenServer
alias EventSourcing.Event
def start_link, do: start_link([])
def start_link(events) do
GenServer.start_link(__MODULE__, events)
end
def init(events) do
{:ok, apply_events(events)}
end
defp apply_events(events) do
Enum.reduce(events, %{}, &apply_event/2)
end
defp apply_event(%Event{name: name, data: data}, state) do
apply_event(name, data, state)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment