Created
October 28, 2018 17:39
-
-
Save toranb/b10e1d41a40330cc27233e2ffe981982 to your computer and use it in GitHub Desktop.
Simple GenServer callback to create a map w/ {key, value} pairs by looking at the filesystem
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
@impl GenServer | |
def handle_call({:all}, _timeout, _state) do | |
{:ok, files} = :file.list_dir(@database) | |
state = | |
Enum.map(files, fn (key) -> | |
value = | |
case File.read(file_name(@database, key)) do | |
{:ok, contents} -> :erlang.binary_to_term(contents) | |
_ -> nil | |
end | |
{key, value} | |
end) | |
|> Enum.into(%{}) | |
{:reply, state, state} | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment