To enable adding the intro, the tracker knows visited modules.
This commit is contained in:
@@ -4,27 +4,34 @@ defmodule Tracker do
|
|||||||
|> Enum.flat_map(&(&1.all_koans))
|
|> Enum.flat_map(&(&1.all_koans))
|
||||||
|> Enum.count
|
|> 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
|
modules
|
||||||
end
|
end
|
||||||
|
|
||||||
def get do
|
defp get(), do: Agent.get(__MODULE__, &(&1))
|
||||||
Agent.get(__MODULE__, &(&1))
|
|
||||||
end
|
|
||||||
|
|
||||||
def completed(koan) do
|
def completed(module, koan) do
|
||||||
Agent.update(__MODULE__, fn({total, completed}) ->
|
Agent.update(__MODULE__, fn(%{koans: completed, visited_modules: modules} = all) ->
|
||||||
{total, MapSet.put(completed, koan)}
|
%{ all | koans: MapSet.put(completed, koan),
|
||||||
|
visited_modules: MapSet.put(modules, module)}
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
def complete? do
|
def complete? do
|
||||||
{total, completed} = get
|
%{total: total, current: completed} = summarize
|
||||||
total == Enum.count(completed)
|
total == completed
|
||||||
end
|
end
|
||||||
|
|
||||||
def summarize, do: get |> summarize
|
def summarize, do: get |> summarize
|
||||||
defp summarize({total, completed}) do
|
defp summarize(%{total: total,
|
||||||
%{total: total, current: MapSet.size(completed)}
|
koans: completed,
|
||||||
|
visited_modules: modules}) do
|
||||||
|
%{
|
||||||
|
total: total,
|
||||||
|
current: MapSet.size(completed),
|
||||||
|
visited_modules: MapSet.to_list(modules)
|
||||||
|
}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -5,19 +5,24 @@ defmodule TrackerTest do
|
|||||||
|
|
||||||
test "can start" do
|
test "can start" do
|
||||||
Tracker.start(@sample_modules)
|
Tracker.start(@sample_modules)
|
||||||
assert Tracker.summarize == %{total: 2, current: 0}
|
assert Tracker.summarize == %{total: 2, current: 0, visited_modules: []}
|
||||||
end
|
end
|
||||||
|
|
||||||
test "can be notified of completed koans" do
|
test "can be notified of completed koans" do
|
||||||
Tracker.start(@sample_modules)
|
Tracker.start(@sample_modules)
|
||||||
Tracker.completed(:"Hi there")
|
Tracker.completed(SampleKoan, :"Hi there")
|
||||||
assert Tracker.summarize == %{total: 2, current: 1}
|
assert Tracker.summarize == %{total: 2, current: 1, visited_modules: [SampleKoan]}
|
||||||
end
|
end
|
||||||
|
|
||||||
test "multiple comletions of the same koan count only once" do
|
test "multiple comletions of the same koan count only once" do
|
||||||
Tracker.start(@sample_modules)
|
Tracker.start(@sample_modules)
|
||||||
Tracker.completed(:"Hi there")
|
Tracker.completed(SampleKoan, :"Hi there")
|
||||||
Tracker.completed(:"Hi there")
|
Tracker.completed(SampleKoan, :"Hi there")
|
||||||
assert Tracker.summarize == %{total: 2, current: 1}
|
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
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user