Created
September 26, 2016 12:34
-
-
Save weiland/696b6d0ef940a3727a02fe5a2ad76bb3 to your computer and use it in GitHub Desktop.
Basic ActiveRecord in Elixir Ecto (as Macro)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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