diff --git a/lib/tracker.ex b/lib/tracker.ex new file mode 100644 index 0000000..df14ba2 --- /dev/null +++ b/lib/tracker.ex @@ -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 diff --git a/test/tracker_test.exs b/test/tracker_test.exs new file mode 100644 index 0000000..28b05e7 --- /dev/null +++ b/test/tracker_test.exs @@ -0,0 +1,25 @@ +defmodule TrackerTest do + use ExUnit.Case + + @sample_modules [SampleKoan, PassingKoan] + + test "can start" do + Tracker.start(@sample_modules) + assert Tracker.get == %{total: 2, current: 0} + end + + test "can be notified of completed koans" do + Tracker.start(@sample_modules) + Tracker.completed(:"Hi there") + assert Tracker.get == %{total: 2, current: 1} + end + + test "multiple comletions of the same koan count only once" do + Tracker.start(@sample_modules) + Tracker.completed(:"Hi there") + Tracker.completed(:"Hi there") + Tracker.completed(:"Hi there") + Tracker.completed(:"Hi there") + assert Tracker.get == %{total: 2, current: 1} + end +end