abcd.ex 636 B

12345678910111213141516171819202122232425262728293031323334353637
  1. defmodule ABCD.Fourletters do
  2. use Agent
  3. @doc """
  4. Start a new fourletters.
  5. """
  6. def start_link(opts) do
  7. Agent.start_link(fn -> opts end)
  8. end
  9. @doc """
  10. Get the posts on this fourletters.
  11. """
  12. def get(name) do
  13. Agent.get(name, & &1)
  14. end
  15. @doc """
  16. Put a new post on these fourletters.
  17. """
  18. def put(name, value) do
  19. Agent.update(name, fn list -> append(list, value) end)
  20. end
  21. def clear(name) do
  22. Agent.update(name, fn _ -> [] end)
  23. end
  24. defp append(lines, value) when is_list(lines) and length(lines) < 100 do
  25. lines ++ [value]
  26. end
  27. defp append(lines, _values) do
  28. lines
  29. end
  30. end