-
Notifications
You must be signed in to change notification settings - Fork 2
/
haystack.ex
50 lines (36 loc) · 987 Bytes
/
haystack.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
defmodule Haystack do
@external_resource "README.md"
@moduledoc "README.md"
|> File.read!()
|> String.split("<!-- MDOC !-->")
|> Enum.fetch!(1)
alias Haystack.Index
# Types
@type t :: %__MODULE__{
indexes: %{atom => Index.t()}
}
@enforce_keys ~w{indexes}a
defstruct @enforce_keys
# Public
@doc """
Create a new Haystack.
## Examples
iex> Haystack.new()
"""
@spec new(Keyword.t()) :: t
def new(_opts \\ []),
do: struct(__MODULE__, indexes: %{})
@doc """
Interact with the given index.
## Examples
iex> haystack = Haystack.new()
iex> Haystack.index(haystack, :animals, &Function.identity/1)
"""
@spec index(t, atom, (Index.t() -> term)) :: t | term
def index(hs, name, f) do
case f.(Map.get(hs.indexes, name, Index.new(name))) do
%Index{} = index -> %{hs | indexes: Map.put(hs.indexes, name, index)}
response -> response
end
end
end