-
Notifications
You must be signed in to change notification settings - Fork 574
/
unfetched.ex
38 lines (27 loc) · 933 Bytes
/
unfetched.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
defmodule Plug.Conn.Unfetched do
@moduledoc """
A struct used as default on unfetched fields.
The `:aspect` key of the struct specifies what field is still unfetched.
## Examples
unfetched = %Plug.Conn.Unfetched{aspect: :cookies}
"""
defstruct [:aspect]
@type t :: %__MODULE__{aspect: atom()}
@behaviour Access
def fetch(%{aspect: aspect}, key) do
raise_unfetched(__ENV__.function, aspect, key)
end
def get(%{aspect: aspect}, key, _value) do
raise_unfetched(__ENV__.function, aspect, key)
end
def get_and_update(%{aspect: aspect}, key, _fun) do
raise_unfetched(__ENV__.function, aspect, key)
end
def pop(%{aspect: aspect}, key) do
raise_unfetched(__ENV__.function, aspect, key)
end
defp raise_unfetched({access, _}, aspect, key) do
raise ArgumentError,
"cannot #{access} key #{inspect(key)} from conn.#{aspect} because they were not fetched"
end
end