Skip to content

Instantly share code, notes, and snippets.

@toranb
Created October 28, 2018 17:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save toranb/b10e1d41a40330cc27233e2ffe981982 to your computer and use it in GitHub Desktop.
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
@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