Add an agent to keep track of completed koans

This commit is contained in:
Felipe Sere
2016-04-24 12:59:55 +01:00
parent 143040c0a0
commit c9ee4dbae0
2 changed files with 49 additions and 0 deletions

24
lib/tracker.ex Normal file
View File

@@ -0,0 +1,24 @@
defmodule Tracker do
def start(modules) do
total = modules
|> Enum.flat_map(&(&1.all_koans))
|> Enum.count
Agent.start_link(fn -> {total, MapSet.new()} end, name: __MODULE__)
end
def get do
Agent.get(__MODULE__, &(&1))
|> summirize
end
def completed(koan) do
Agent.update(__MODULE__, fn({total, completed}) ->
{total, MapSet.put(completed, koan)}
end)
end
def summirize({total, completed}) do
%{total: total, current: MapSet.size(completed)}
end
end