In Programming Phoenix, chapter 3, there is a hardcoded repo example with the following code:
defmodule Rumbl.Repo do
@moduledoc """
In memory repository.
"""
def all(Rumbl.User) do
[%Rumbl.User{id: "1", name: "JoseĢ", username: "josevalim", password: "elixir"},
%Rumbl.User{id: "2", name: "Bruce", username: "redrapids", password: "7langs"},
%Rumbl.User{id: "3", name: "Chris", username: "chrismccord", password: "phx"}]
end
def all(_module), do: []
def get(module, id) do
Enum.find all(module), fn map -> map.id == id end
end
def get_by(module, params) do
Enum.find all(module), fn map ->
Enum.all?(params, fn {key, val} -> Map.get(map, key) == val end)
end
end
end
Using a pipe, I rewrote get/2
in a way that seems easier to understand:
def get(module, id) do all(module) |> Enum.find fn map -> map.id == id end end
Is there a simple way to do the same to get_by/2
?