To enable adding the intro, the tracker knows visited modules.

This commit is contained in:
Felipe Sere
2016-05-10 22:41:02 +01:00
parent c23fca5b29
commit 7b494e16df
2 changed files with 29 additions and 17 deletions

View File

@@ -4,27 +4,34 @@ defmodule Tracker do
|> Enum.flat_map(&(&1.all_koans))
|> Enum.count
Agent.start_link(fn -> {total, MapSet.new()} end, name: __MODULE__)
Agent.start_link(fn -> %{total: total,
koans: MapSet.new(),
visited_modules: MapSet.new()} end, name: __MODULE__)
modules
end
def get do
Agent.get(__MODULE__, &(&1))
end
defp get(), do: Agent.get(__MODULE__, &(&1))
def completed(koan) do
Agent.update(__MODULE__, fn({total, completed}) ->
{total, MapSet.put(completed, koan)}
def completed(module, koan) do
Agent.update(__MODULE__, fn(%{koans: completed, visited_modules: modules} = all) ->
%{ all | koans: MapSet.put(completed, koan),
visited_modules: MapSet.put(modules, module)}
end)
end
def complete? do
{total, completed} = get
total == Enum.count(completed)
%{total: total, current: completed} = summarize
total == completed
end
def summarize, do: get |> summarize
defp summarize({total, completed}) do
%{total: total, current: MapSet.size(completed)}
defp summarize(%{total: total,
koans: completed,
visited_modules: modules}) do
%{
total: total,
current: MapSet.size(completed),
visited_modules: MapSet.to_list(modules)
}
end
end

View File

@@ -5,19 +5,24 @@ defmodule TrackerTest do
test "can start" do
Tracker.start(@sample_modules)
assert Tracker.summarize == %{total: 2, current: 0}
assert Tracker.summarize == %{total: 2, current: 0, visited_modules: []}
end
test "can be notified of completed koans" do
Tracker.start(@sample_modules)
Tracker.completed(:"Hi there")
assert Tracker.summarize == %{total: 2, current: 1}
Tracker.completed(SampleKoan, :"Hi there")
assert Tracker.summarize == %{total: 2, current: 1, visited_modules: [SampleKoan]}
end
test "multiple comletions of the same koan count only once" do
Tracker.start(@sample_modules)
Tracker.completed(:"Hi there")
Tracker.completed(:"Hi there")
assert Tracker.summarize == %{total: 2, current: 1}
Tracker.completed(SampleKoan, :"Hi there")
Tracker.completed(SampleKoan, :"Hi there")
assert Tracker.summarize == %{total: 2, current: 1, visited_modules: [SampleKoan]}
end
test "knows when koans are not complete" do
Tracker.start(@sample_modules)
refute Tracker.complete?
end
end