Skip to content

Commit c2fc43f

Browse files
authored
Tweak formatting and language in Multi (#4645)
1 parent 0e32538 commit c2fc43f

File tree

1 file changed

+78
-78
lines changed

1 file changed

+78
-78
lines changed

lib/ecto/multi.ex

Lines changed: 78 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ defmodule Ecto.Multi do
33
`Ecto.Multi` is a data structure for grouping multiple Repo operations.
44
55
`Ecto.Multi` makes it possible to pack operations that should be
6-
performed in a single database transaction and gives a way to introspect
6+
performed in a single database transaction and provides a way to introspect
77
the queued operations without actually performing them. Each operation
88
is given a name that is unique and will identify its result in case of
9-
success or failure.
9+
either success or failure.
1010
11-
If a multi is valid (i.e. all the changesets in it are valid),
11+
If a Multi is valid (i.e. all the changesets in it are valid),
1212
all operations will be executed in the order they were added.
1313
1414
The `Ecto.Multi` structure should be considered opaque. You can use
@@ -19,41 +19,41 @@ defmodule Ecto.Multi do
1919
structure that can be used for introspection.
2020
2121
> #### When to use Ecto.Multi? {: .info}
22-
22+
>
2323
> `Ecto.Multi` is particularly useful when the set of operations to perform
24-
> is dynamic. For most use cases, however, using regular control flow within
24+
> is dynamic. For most other use cases, using regular control flow within
2525
> [`Repo.transact(fun)`](`c:Ecto.Repo.transact/2`) and returning
2626
> `{:ok, result}` or `{:error, reason}` is more straightforward.
2727
2828
## Changesets
2929
30-
If multi contains operations that accept changesets (like `insert/4`,
31-
`update/4` or `delete/4`) they will be checked before starting the
32-
transaction. If any changeset has errors, the transaction won't even
33-
be started and the error will be immediately returned.
30+
If a Multi contains operations that accept changesets (like `insert/4`,
31+
`update/4` or `delete/4`), they will be checked before starting the
32+
transaction. If any changeset has errors, the transaction will not be
33+
started and the error will immediately be returned.
3434
35-
Note: `insert/4`, `update/4`, `insert_or_update/4`, and `delete/4`
35+
Note: `insert/4`, `update/4`, `insert_or_update/4` and `delete/4`
3636
variants that accept a function do not perform these checks since
3737
the functions are executed after the transaction has started.
3838
3939
## Run
4040
41-
Multi allows you to run arbitrary functions as part of your transaction
41+
`Multi` allows you to run arbitrary functions as part of your transaction
4242
via `run/3` and `run/5`. This is especially useful when an operation
4343
depends on the value of a previous operation. For this reason, the
4444
function given as a callback to `run/3` and `run/5` will receive the repo
45-
as the first argument, and all changes performed by the multi so far as a
46-
map for the second argument.
45+
as the first argument, and all changes performed by the Multi so far as a
46+
map as the second argument.
4747
4848
The function given to `run` must return `{:ok, value}` or `{:error, value}`
4949
as its result. Returning an error will abort any further operations
50-
and make the whole multi fail.
50+
and make the Multi fail.
5151
5252
## Example
5353
54-
Let's look at an example definition and usage. The use case we'll be
55-
looking into is resetting a password. We need to update the account
56-
with proper information, log the request and remove all current sessions:
54+
Let's look at an example definition and usage: resetting a password. We need
55+
to update the account with proper information, log the request and remove
56+
all current sessions:
5757
5858
defmodule PasswordManager do
5959
alias Ecto.Multi
@@ -70,24 +70,24 @@ defmodule Ecto.Multi do
7070
7171
Repo.transact(PasswordManager.reset(account, params))
7272
73-
By pattern matching on the result we can differentiate different conditions:
73+
By pattern matching on the result we can differentiate various conditions:
7474
7575
case result do
7676
{:ok, %{account: account, log: log, sessions: sessions}} ->
77-
# Operation was successful, we can access results (exactly the same
78-
# we would get from running corresponding Repo functions) under keys
79-
# we used for naming the operations.
77+
# The Multi was successful. We can access results , which are as
78+
# we would get from running the corresponding Repo functions, under
79+
# keys we used for naming the operations.
8080
{:error, failed_operation, failed_value, changes_so_far} ->
8181
# One of the operations failed. We can access the operation's failure
82-
# value (like changeset for operations on changesets) to prepare a
82+
# value (such as a changeset for operations on changesets) to prepare a
8383
# proper response. We also get access to the results of any operations
84-
# that succeeded before the indicated operation failed. However, any
85-
# successful operations would have been rolled back.
84+
# that succeeded before the indicated operation failed. (However,
85+
# successful operations were rolled back.)
8686
end
8787
8888
We can also easily unit test our transaction without actually running it.
89-
Since changesets can use in-memory-data, we can use an account that is
90-
constructed in memory as well (without persisting it to the database):
89+
Since changesets can use in-memory data, we can use an account that is
90+
constructed in memory as well, without persisting it to the database:
9191
9292
test "dry run password reset" do
9393
account = %Account{password: "letmein"}
@@ -180,9 +180,9 @@ defmodule Ecto.Multi do
180180
end
181181

182182
@doc """
183-
Appends the second multi to the first one.
183+
Appends the second Multi to the first.
184184
185-
All names must be unique between both structures.
185+
All names must be unique within both structures.
186186
187187
## Example
188188
@@ -198,9 +198,9 @@ defmodule Ecto.Multi do
198198
end
199199

200200
@doc """
201-
Prepends the second multi to the first one.
201+
Prepends the second Multi to the first.
202202
203-
All names must be unique between both structures.
203+
All names must be unique within both structures.
204204
205205
## Example
206206
@@ -237,14 +237,14 @@ defmodule Ecto.Multi do
237237
end
238238

239239
@doc """
240-
Merges a multi returned dynamically by an anonymous function.
240+
Merges a Multi returned dynamically by an anonymous function.
241241
242-
This function is useful when the multi to be merged requires information
243-
from the original multi. Hence the second argument is an anonymous function
244-
that receives the multi changes so far. The anonymous function must return
245-
another multi.
242+
This function is useful when the Multi to be merged requires information
243+
from the original Multi. The second argument is an anonymous function
244+
that receives the Multi changes so far. The anonymous function must return
245+
another Multi.
246246
247-
If you would prefer to simply merge two multis together, see `append/2` or
247+
If you would prefer to simply merge two Multis together, see `append/2` or
248248
`prepend/2`.
249249
250250
Duplicated operations are not allowed.
@@ -268,10 +268,10 @@ defmodule Ecto.Multi do
268268
end
269269

270270
@doc """
271-
Merges a multi returned dynamically by calling `module` and `function` with `args`.
271+
Merges a Multi returned dynamically by calling `module` and `function` with `args`.
272272
273-
Similar to `merge/2`, but allows to pass module name, function and arguments.
274-
The function should return an `Ecto.Multi`, and receives changes so far
273+
Similar to `merge/2` but allows passing of module name, function and
274+
arguments. The function should return an `Ecto.Multi`, and receives changes so far
275275
as the first argument (prepended to those passed in the call to the function).
276276
277277
Duplicated operations are not allowed.
@@ -283,9 +283,9 @@ defmodule Ecto.Multi do
283283
end
284284

285285
@doc """
286-
Adds an insert operation to the multi.
286+
Adds an insert operation to the Multi.
287287
288-
The `name` must be unique from other statements in the multi.
288+
The `name` must be unique within the Multi.
289289
290290
The remaining arguments and options are the same as in `c:Ecto.Repo.insert/2`.
291291
@@ -324,9 +324,9 @@ defmodule Ecto.Multi do
324324
end
325325

326326
@doc """
327-
Adds an update operation to the multi.
327+
Adds an update operation to the Multi.
328328
329-
The `name` must be unique from other statements in the multi.
329+
The `name` must be unique within the Multi.
330330
331331
The remaining arguments and options are the same as in `c:Ecto.Repo.update/2`.
332332
@@ -358,9 +358,9 @@ defmodule Ecto.Multi do
358358
end
359359

360360
@doc """
361-
Inserts or updates a changeset depending on whether the changeset was persisted or not.
361+
Inserts or updates a changeset depending on whether or not the changeset was persisted.
362362
363-
The `name` must be unique from other statements in the multi.
363+
The `name` must be unique within the Multi.
364364
365365
The remaining arguments and options are the same as in `c:Ecto.Repo.insert_or_update/2`.
366366
@@ -402,9 +402,9 @@ defmodule Ecto.Multi do
402402
end
403403

404404
@doc """
405-
Adds a delete operation to the multi.
405+
Adds a delete operation to the Multi.
406406
407-
The `name` must be unique from other statements in the multi.
407+
The `name` must be unique within the Multi.
408408
409409
The remaining arguments and options are the same as in `c:Ecto.Repo.delete/2`.
410410
@@ -450,9 +450,9 @@ defmodule Ecto.Multi do
450450
end
451451

452452
@doc """
453-
Runs a query expecting one result and stores it in the multi.
453+
Runs a query expecting one result and stores the result in the Multi.
454454
455-
The `name` must be unique from other statements in the multi.
455+
The `name` must be unique within the Multi.
456456
457457
The remaining arguments and options are the same as in `c:Ecto.Repo.one/2`.
458458
@@ -482,11 +482,11 @@ defmodule Ecto.Multi do
482482
end
483483

484484
@doc """
485-
Runs a query and stores all entries in the multi.
485+
Runs a query and stores all results in the Multi.
486486
487-
The `name` must be unique from other statements in the multi.
487+
The `name` must be unique within the Multi.
488488
489-
The remaining arguments and options are the same as in `c:Ecto.Repo.all/2` does.
489+
The remaining arguments and options are the same as in `c:Ecto.Repo.all/2`.
490490
491491
## Example
492492
@@ -515,9 +515,9 @@ defmodule Ecto.Multi do
515515
end
516516

517517
@doc """
518-
Checks if there exists an entry matching the given query and stores a boolean in the multi.
518+
Checks if an entry matching the given query exists and stores a boolean in the Multi.
519519
520-
The `name` must be unique from other statements in the multi.
520+
The `name` must be unique within the Multi.
521521
522522
The remaining arguments and options are the same as in `c:Ecto.Repo.exists?/2`.
523523
@@ -566,10 +566,10 @@ defmodule Ecto.Multi do
566566
end
567567

568568
@doc """
569-
Causes the multi to fail with the given value.
569+
Causes the Multi to fail with the given value.
570570
571-
Running the multi in a transaction will execute
572-
no previous steps and returns the value of the first
571+
Running the Multi in a transaction will execute
572+
no previous steps and return the value of the first
573573
error added.
574574
"""
575575
@spec error(t, name, error :: term) :: t
@@ -578,10 +578,10 @@ defmodule Ecto.Multi do
578578
end
579579

580580
@doc """
581-
Adds a function to run as part of the multi.
581+
Adds a function to run as part of the Multi.
582582
583583
The function should return either `{:ok, value}` or `{:error, value}`,
584-
and receives the repo as the first argument, and the changes so far
584+
and receives the repo as the first argument and the changes so far
585585
as the second argument.
586586
587587
## Example
@@ -598,11 +598,11 @@ defmodule Ecto.Multi do
598598
end
599599

600600
@doc """
601-
Adds a function to run as part of the multi.
601+
Adds a function to run as part of the Multi.
602602
603-
Similar to `run/3`, but allows to pass module name, function and arguments.
603+
Similar to `run/3`, but allows passing of module name, function and arguments.
604604
The function should return either `{:ok, value}` or `{:error, value}`, and
605-
receives the repo as the first argument, and the changes so far as the
605+
receives the repo as the first argument and the changes so far as the
606606
second argument (prepended to those passed in the call to the function).
607607
"""
608608
@spec run(t, name, module, function, args) :: t when function: atom, args: [any]
@@ -612,9 +612,9 @@ defmodule Ecto.Multi do
612612
end
613613

614614
@doc """
615-
Adds an insert_all operation to the multi.
615+
Adds an `insert_all` operation to the Multi.
616616
617-
Accepts the same arguments and options as `c:Ecto.Repo.insert_all/3` does.
617+
Accepts the same arguments and options as `c:Ecto.Repo.insert_all/3`.
618618
619619
## Example
620620
@@ -661,9 +661,9 @@ defmodule Ecto.Multi do
661661
end
662662

663663
@doc """
664-
Adds an update_all operation to the multi.
664+
Adds an `update_all` operation to the Multi.
665665
666-
Accepts the same arguments and options as `c:Ecto.Repo.update_all/3` does.
666+
Accepts the same arguments and options as `c:Ecto.Repo.update_all/3`.
667667
668668
## Example
669669
@@ -705,9 +705,9 @@ defmodule Ecto.Multi do
705705
end
706706

707707
@doc """
708-
Adds a delete_all operation to the multi.
708+
Adds a `delete_all` operation to the Multi.
709709
710-
Accepts the same arguments and options as `c:Ecto.Repo.delete_all/2` does.
710+
Accepts the same arguments and options as `c:Ecto.Repo.delete_all/2`.
711711
712712
## Example
713713
@@ -754,7 +754,7 @@ defmodule Ecto.Multi do
754754
end
755755

756756
@doc """
757-
Returns the list of operations stored in `multi`.
757+
Returns the list of operations stored in the Multi.
758758
759759
Always use this function when you need to access the operations you
760760
have defined in `Ecto.Multi`. Inspecting the `Ecto.Multi` struct internals
@@ -776,26 +776,26 @@ defmodule Ecto.Multi do
776776
@doc """
777777
Adds a value to the changes so far under the given name.
778778
779-
The given `value` is added to the multi before the transaction starts.
779+
The given `value` is added to the Multi before the transaction starts.
780780
If you would like to run arbitrary functions as part of your transaction,
781781
see `run/3` or `run/5`.
782782
783783
## Example
784784
785785
Imagine there is an existing company schema that you retrieved from
786-
the database. You can insert it as a change in the multi using `put/3`:
786+
the database. You can insert it as a change in the Multi using `put/3`:
787787
788788
Ecto.Multi.new()
789789
|> Ecto.Multi.put(:company, company)
790790
|> Ecto.Multi.insert(:user, fn changes -> User.changeset(changes.company) end)
791791
|> Ecto.Multi.insert(:person, fn changes -> Person.changeset(changes.user, changes.company) end)
792792
|> MyApp.Repo.transact()
793793
794-
In the example above there isn't a large benefit in putting the
795-
`company` in the multi, because you could also access the
794+
In the example above, there isn't a significant benefit in putting
795+
the `company` in the Multi because you could also access the
796796
`company` variable directly inside the anonymous function.
797797
798-
However, the benefit of `put/3` is when composing `Ecto.Multi`s.
798+
However, the benefit of `put/3` is seen when composing `Ecto.Multi`s.
799799
If the insert operations above were defined in another module,
800800
you could use `put(:company, company)` to inject changes that
801801
will be accessed by other functions down the chain, removing
@@ -807,14 +807,14 @@ defmodule Ecto.Multi do
807807
end
808808

809809
@doc """
810-
Inspects results from a Multi
810+
Inspects results from a Multi.
811811
812-
By default, the name is shown as a label to the inspect, custom labels are
812+
By default, the name is shown as a label to the inspect. Custom labels are
813813
supported through the `IO.inspect/2` `label` option.
814814
815815
## Options
816816
817-
All options for IO.inspect/2 are supported, it also support the following ones:
817+
All options for IO.inspect/2 are supported, as well as:
818818
819819
* `:only` - A field or a list of fields to inspect, will print the entire
820820
map by default.
@@ -949,7 +949,7 @@ defmodule Ecto.Multi do
949949
{Map.merge(changes, new_changes), MapSet.union(names, new_names)}
950950

951951
common ->
952-
raise "cannot merge multi, the following operations were found in " <>
952+
raise "cannot merge Multi; the following operations were found in " <>
953953
"both Ecto.Multi: #{Kernel.inspect(common)}"
954954
end
955955
end

0 commit comments

Comments
 (0)