Skip to content

Instantly share code, notes, and snippets.

@weiland
Created September 26, 2016 12:34
Show Gist options
  • Save weiland/696b6d0ef940a3727a02fe5a2ad76bb3 to your computer and use it in GitHub Desktop.
Save weiland/696b6d0ef940a3727a02fe5a2ad76bb3 to your computer and use it in GitHub Desktop.
Basic ActiveRecord in Elixir Ecto (as Macro)
defmodule BasicApp.ActiveRecord do
defmacro __using__(_) do
quote do
alias BasicApp.Repo
def all(opts \\ []) do
Repo.all __MODULE__, opts
end
def find(clauses, opts \\ []) do
Enum.filter all(opts), fn map ->
Enum.all?(clauses, fn {key, val} -> Map.get(map, key) == val end)
end
end
def get(id, opts \\ []) do
Repo.get __MODULE__, id, opts
end
def get_by(clauses, opts \\ []) do
Repo.get_by __MODULE__, clauses, opts
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment