Skip to content

Commit f73cba8

Browse files
authored
populate/omit -> enforce_random/exclude (#1)
1 parent 2bbec6e commit f73cba8

File tree

4 files changed

+23
-25
lines changed

4 files changed

+23
-25
lines changed

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ Post.random_params!(:create, %{author: "James"})
3737

3838
# With options
3939
Post.random_params!(:create, %{author: "James"}, %{
40-
populate: [:content],
41-
omit: [:title],
40+
enforce_random: [:content],
41+
exclude: [:title],
4242
include_defaults?: false
4343
})
4444
=> %{author: "James", content: "content-38128"}
@@ -55,13 +55,13 @@ For accepts and arguments that match the `name` or `source_attribute` of a `belo
5555
- In other actions, they are not generated at all
5656

5757
This behavior exists because:
58-
- In `create` actions, omitting a value is equivalent to setting it to `nil`
59-
- In `update` actions, omitting a value preserves the existing relationship, while explicitly setting it to `nil` removes the relationship
58+
- In `create` actions, excluding a value is equivalent to setting it to `nil`
59+
- In `update` actions, excluding a value preserves the existing relationship, while explicitly setting it to `nil` removes the relationship
6060

6161
### Options
6262

63-
- `populate`: Forces generation of random values for specified accepts/arguments, overriding the default behavior
64-
- `omit`: Prevents generation of random values for specified accepts/arguments, overriding the default behavior
63+
- `enforce_random`: Forces generation of random values for specified accepts/arguments, overriding the default behavior
64+
- `exclude`: Prevents generation of random values for specified accepts/arguments, overriding the default behavior
6565
- `include_defaults?`: When set to `true`, includes default values for accepts/arguments that have either `allow_nil?: true` or a non-nil default value. Defaults to `true`. In the example above, this would add `%{content: nil, tag: "JS"}` to the generated params.
6666

6767
### Custom Random Generator
@@ -98,7 +98,7 @@ Add `ash_random_params` to your list of dependencies in `mix.exs`:
9898
```elixir
9999
def deps do
100100
[
101-
{:ash_random_params, "~> 0.1.0"}
101+
{:ash_random_params, "~> 0.2.0"}
102102
]
103103
end
104104
```

lib/transformer.ex

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ defmodule AshRandomParams.Transformer do
1313
def add_random_params(dsl_state) do
1414
action = Builder.build_action_argument(:action, :atom, default: :create)
1515
init_params = Builder.build_action_argument(:init_params, :map, default: %{})
16-
populate = Builder.build_action_argument(:populate, {:array, :atom}, default: [])
17-
omit = Builder.build_action_argument(:omit, {:array, :atom}, default: [])
16+
enforce_random = Builder.build_action_argument(:enforce_random, {:array, :atom}, default: [])
17+
exclude = Builder.build_action_argument(:exclude, {:array, :atom}, default: [])
1818
include_defaults? = Builder.build_action_argument(:include_defaults?, :boolean, default: true)
1919

2020
dsl_state
2121
|> Builder.add_action(:action, :random_params,
2222
returns: :map,
23-
arguments: [action, init_params, omit, populate, include_defaults?],
23+
arguments: [action, init_params, exclude, enforce_random, include_defaults?],
2424
run: &__MODULE__.do_random_params/2
2525
)
2626
|> Builder.add_interface(:random_params, args: [:action, {:optional, :init_params}])
@@ -44,8 +44,8 @@ defmodule AshRandomParams.Transformer do
4444
arguments: %{
4545
action: action,
4646
init_params: init_params,
47-
omit: omit,
48-
populate: populate,
47+
exclude: exclude,
48+
enforce_random: enforce_random,
4949
include_defaults?: include_defaults?
5050
}
5151
} =
@@ -86,7 +86,7 @@ defmodule AshRandomParams.Transformer do
8686
|> Enum.reject(&(&1.name in belongs_to_attrs))
8787
end
8888
end)
89-
|> Enum.reject(&(&1.name in omit))
89+
|> Enum.reject(&(&1.name in exclude))
9090
|> Enum.reject(&(&1.name in init_keys))
9191
|> Map.new(fn %{name: name, default: default} ->
9292
value =
@@ -109,16 +109,16 @@ defmodule AshRandomParams.Transformer do
109109
{random_mod, random_opts} =
110110
random = AshRandomParams.Info.random_params_random!(input.resource)
111111

112-
fields_by_omit =
112+
fields_by_exclude =
113113
candidates
114114
|> Enum.reject(&(&1.name in rel_names))
115115
|> Enum.reject(&(&1.name in belongs_to_attrs))
116-
|> Enum.reject(&(&1.name in omit))
116+
|> Enum.reject(&(&1.name in exclude))
117117
|> Enum.reject(&(&1.allow_nil? || &1.default != nil))
118118

119-
fields_by_fill = candidates |> Enum.filter(&(&1.name in populate))
119+
fields_by_fill = candidates |> Enum.filter(&(&1.name in enforce_random))
120120

121-
(fields_by_omit ++ fields_by_fill)
121+
(fields_by_exclude ++ fields_by_fill)
122122
|> Enum.reject(&(&1.name in init_keys))
123123
|> Map.new(fn %{name: name, default: default} = attr_or_arg ->
124124
value =

mix.exs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ defmodule AshRandomParams.MixProject do
44
def project do
55
[
66
app: :ash_random_params,
7-
version: "0.1.0",
7+
version: "0.2.0",
88
elixir: "~> 1.17",
99
consolidate_protocols: Mix.env() not in [:dev, :test],
1010
start_permanent: Mix.env() == :prod,
@@ -34,8 +34,6 @@ defmodule AshRandomParams.MixProject do
3434
{:spark, ">= 0.0.0"},
3535
{:sourceror, ">= 0.0.0", only: [:dev, :test], optional: true},
3636
{:ex_doc, "~> 0.29", only: :dev, runtime: false}
37-
# {:dep_from_hexpm, "~> 0.3.0"},
38-
# {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"}
3937
]
4038
end
4139

test/ash_random_params_test.exs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ defmodule AshRandomParamsTest do
148148
|> Ash.run_action!()
149149
end
150150

151-
test "with populate" do
151+
test "with enforce_random" do
152152
assert %{
153153
author_id: nil,
154154
req_str: "req_str-" <> _,
@@ -161,13 +161,13 @@ defmodule AshRandomParamsTest do
161161
:random_params,
162162
%{
163163
action: :create,
164-
populate: [:opt_str]
164+
enforce_random: [:opt_str]
165165
}
166166
)
167167
|> Ash.run_action!()
168168
end
169169

170-
test "with omit" do
170+
test "with exclude" do
171171
assert %{
172172
author_id: nil,
173173
opt_str: nil,
@@ -180,7 +180,7 @@ defmodule AshRandomParamsTest do
180180
:random_params,
181181
%{
182182
action: :create,
183-
omit: [:req_str]
183+
exclude: [:req_str]
184184
}
185185
)
186186
|> Ash.run_action!()
@@ -239,7 +239,7 @@ defmodule AshRandomParamsTest do
239239
Post.random_params!(
240240
:create,
241241
%{opt_int: 456},
242-
%{populate: [:opt_int]}
242+
%{enforce_random: [:opt_int]}
243243
)
244244
end
245245
end

0 commit comments

Comments
 (0)