Skip to content

Commit 204cb0a

Browse files
committed
refactor!: remove accessible plugin, add it to guides
1 parent c191e78 commit 204cb0a

File tree

5 files changed

+78
-51
lines changed

5 files changed

+78
-51
lines changed

lib/typed_structor/plugins/accessible.ex guides/plugins/accessible.md

+46-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,21 @@
1-
defmodule TypedStructor.Plugins.Accessible do
1+
# Implement `Access` behavior
2+
3+
Sometimes, you may want to use the `get_in/2` and `update_in/3` functions
4+
with your structs. This guide demonstrates how to effortlessly implement
5+
the `Access` behavior for your structs using a plugin.
6+
7+
## Implementation
8+
9+
> #### Destructive operations {: .warning}
10+
> These operations are not allowed for the struct:
11+
> * update `:__struct__` key
12+
> * pop a key
13+
>
14+
> The functions will raise an `ArgumentError` if called.
15+
> To enable these functionalities, override the `get_and_update/3` and `pop/2` functions.
16+
17+
```elixir
18+
defmodule Guides.Plugins.Accessible do
219
@moduledoc """
320
This plugin implements the `Access` behavior for the struct
421
by delegating the `fetch/2`, `get_and_update/3`, and `pop/2`
@@ -14,13 +31,11 @@ defmodule TypedStructor.Plugins.Accessible do
1431
1532
## Usage
1633
17-
```elixir
18-
typed_structor do
19-
plugin TypedStructor.Plugins.Accessible
34+
typed_structor do
35+
plugin TypedStructor.Plugins.Accessible
2036
21-
# fields
22-
end
23-
```
37+
# fields
38+
end
2439
"""
2540

2641
use TypedStructor.Plugin
@@ -53,3 +68,27 @@ defmodule TypedStructor.Plugins.Accessible do
5368
end
5469
end
5570
end
71+
```
72+
73+
## Usage
74+
```elixir
75+
defmodule User do
76+
use TypedStructor
77+
78+
typed_structor do
79+
plugin Guides.Plugins.Accessible
80+
81+
field :name, String.t()
82+
field :age, integer()
83+
end
84+
end
85+
```
86+
87+
```elixir
88+
iex> user = %User{name: "Phil", age: 20}
89+
%User{name: "Phil", age: 20}
90+
iex> get_in(user, [:name])
91+
"Phil"
92+
iex> put_in(user, [:name], "phil")
93+
%User{name: "phil", age: 20}
94+
```

guides/plugins/introduction.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ We provide some example plugins that you can use as a reference, or copy-paste.
1212

1313
**Plugin examples:**
1414
- [Registering plugins globally](./registering_plugins_globally.md)
15-
- `TypedStructor.Plugins.Accessible`
15+
- [Implement `Access` behavior](./accessible.md)
1616
- [Type Only on Ecto Schema](./type_only_on_ecto_schema.md)
1717
- [Add primary key and timestamps types to your Ecto schema](./primary_key_and_timestamps.md)
1818
- [Derives the `Jason.Encoder` for `struct`](./derive_jason.md)

mix.exs

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ defmodule TypedStructor.MixProject do
3434
# plugins
3535
{"guides/plugins/introduction.md", [title: "Introduction"]},
3636
"guides/plugins/registering_plugins_globally.md",
37+
"guides/plugins/accessible.md",
3738
"guides/plugins/type_only_on_ecto_schema.md",
3839
"guides/plugins/primary_key_and_timestamps.md",
3940
"guides/plugins/derive_jason.md",
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
defmodule Guides.Plugins.AccessibleTest do
2+
use TypedStructor.GuideCase,
3+
async: true,
4+
guide: "accessible.md"
5+
6+
test "implements Access" do
7+
user = %User{name: "Phil", age: 20}
8+
9+
assert "Phil" === get_in(user, [:name])
10+
assert %User{name: "phil", age: 20} === put_in(user, [:name], "phil")
11+
12+
assert_raise ArgumentError, ~r/Cannot update `:__struct__` key/, fn ->
13+
put_in(user, [:__struct__], "phil")
14+
end
15+
16+
assert %{name: "phil"} = update_in(user, [:name], fn "Phil" -> "phil" end)
17+
18+
assert_raise ArgumentError, ~r/Cannot update `:__struct__` key/, fn ->
19+
update_in(user, [:__struct__], fn _ -> nil end)
20+
end
21+
22+
assert_raise ArgumentError, ~r/Cannot pop `:__struct__` key/, fn ->
23+
pop_in(user, [:__struct__])
24+
end
25+
26+
assert_raise ArgumentError, ~r/Cannot pop `:name` key/, fn ->
27+
pop_in(user, [:name])
28+
end
29+
end
30+
end

test/typed_structor/plugins/accessible_test.exs

-43
This file was deleted.

0 commit comments

Comments
 (0)