Skip to content

Commit 0e5a20e

Browse files
MONGOID-5415 Use @yield syntax for blocks. Also add to documentation. (#5410)
Co-authored-by: shields <[email protected]>
1 parent 5d3ac24 commit 0e5a20e

File tree

7 files changed

+82
-20
lines changed

7 files changed

+82
-20
lines changed

docs/contributing/code-documentation.txt

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ Structure
3535
class CardboardBox
3636

3737
- **Methods:** All method definitions should be preceded by a documentation comment.
38-
Use ``@param`` and ``@return`` to specify input(s) and output respectively.
38+
Use ``@param``, ``@yield``, and ``@return`` to specify inputs and output.
3939
For further details, refer to
4040
:ref:`Type Declaration <code-documentation-type-declaration>` below.
4141

@@ -295,3 +295,59 @@ Type Declaration
295295
# @option **kwargs [ String | Array<String> ] :items The items(s) as Strings to include.
296296
# @option **kwargs [ Integer ] :limit An Integer denoting the limit.
297297
def buy_groceries(**kwargs)
298+
299+
- **Blocks:** Use ``@yield`` to specify when the method yields to a block.
300+
301+
.. code-block:: ruby
302+
303+
# @yield [ Symbol, Symbol, Symbol ] Evaluate the guess of who did the crime.
304+
# Must take the person, location, and weapon used. Must return true or false.
305+
def whodunit
306+
yield(:mustard, :ballroom, :candlestick)
307+
end
308+
309+
- **Blocks:** If the method explicitly specifies a block argument, specify the block
310+
argument using ``@param`` preceded by an ampersand ``&``, and also specify ``@yield``.
311+
Note ``@yield`` should be used even when method calls ``block.call`` rather than
312+
``yield`` internally.
313+
314+
.. code-block:: ruby
315+
316+
# @param &block The block.
317+
# @yield [ Symbol, Symbol, Symbol ] Evaluate the guess of who did the crime.
318+
# Must take the person, location, and weapon used. Must return true or false.
319+
def whodunit(&block)
320+
yield(:scarlet, :library, :rope)
321+
end
322+
323+
# @param &block The block.
324+
# @yield [ Symbol, Symbol, Symbol ] Evaluate the guess of who did the crime.
325+
# Must take the person, location, and weapon used. Must return true or false.
326+
def whodunit(&block)
327+
block.call(:plum, :kitchen, :pipe)
328+
end
329+
330+
- **Blocks:** Use ``@yieldparam`` and ``@yieldreturn`` instead of ``@yield`` where
331+
beneficial for clarity.
332+
333+
.. code-block:: ruby
334+
335+
# @param &block The block.
336+
# @yieldparam [ Symbol ] The person.
337+
# @yieldparam [ Symbol ] The location.
338+
# @yieldparam [ Symbol ] The weapon used.
339+
# @yieldreturn [ true | false ] Whether the guess is correct.
340+
def whodunit(&block)
341+
yield(:peacock, :conservatory, :wrench)
342+
end
343+
344+
- **Proc Args:** Proc arguments should use ``@param`` (not ``@yield``). The
345+
inputs to the proc may be specified as subtype(s).
346+
347+
.. code-block:: ruby
348+
349+
# @param [ Proc<Integer, Integer, Integer> ] my_proc Proc argument which must
350+
# take 3 integers and must return true or false whether the guess is valid.
351+
def guess_three(my_proc)
352+
my_proc.call(42, 7, 88)
353+
end

lib/mongoid/association/embedded/embeds_many/proxy.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,8 @@ def exists?
236236
# person.addresses.find { |addr| addr.state == 'CA' }
237237
#
238238
# @param [ Object... ] *args Various arguments.
239-
# @param [ Proc ] block Optional block to pass.
239+
# @param &block Optional block to pass.
240+
# @yield [ Object ] Yields each enumerable element to the block.
240241
#
241242
# @return [ Document | Array<Document> | nil ] A document or matching documents.
242243
def find(*args, &block)
@@ -431,7 +432,7 @@ def integrate(document)
431432
#
432433
# @param [ Symbol | String ] name The name of the method.
433434
# @param [ Object... ] *args The method args.
434-
# @param [ Proc ] block Optional block to pass.
435+
# @param &block Optional block to pass.
435436
#
436437
# @return [ Criteria | Object ] A Criteria or return value from the target.
437438
ruby2_keywords def method_missing(name, *args, &block)

lib/mongoid/association/macros.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ module ClassMethods
7272
#
7373
# @param [ Symbol ] name The name of the association.
7474
# @param [ Hash ] options The association options.
75-
# @param [ Proc ] block Optional block for defining extensions.
75+
# @param &block Optional block for defining extensions.
7676
def embedded_in(name, options = {}, &block)
7777
define_association!(__method__, name, options, &block)
7878
end
@@ -95,7 +95,7 @@ def embedded_in(name, options = {}, &block)
9595
#
9696
# @param [ Symbol ] name The name of the association.
9797
# @param [ Hash ] options The association options.
98-
# @param [ Proc ] block Optional block for defining extensions.
98+
# @param &block Optional block for defining extensions.
9999
def embeds_many(name, options = {}, &block)
100100
define_association!(__method__, name, options, &block)
101101
end
@@ -118,7 +118,7 @@ def embeds_many(name, options = {}, &block)
118118
#
119119
# @param [ Symbol ] name The name of the association.
120120
# @param [ Hash ] options The association options.
121-
# @param [ Proc ] block Optional block for defining extensions.
121+
# @param &block Optional block for defining extensions.
122122
def embeds_one(name, options = {}, &block)
123123
define_association!(__method__, name, options, &block)
124124
end
@@ -140,7 +140,7 @@ def embeds_one(name, options = {}, &block)
140140
#
141141
# @param [ Symbol ] name The name of the association.
142142
# @param [ Hash ] options The association options.
143-
# @param [ Proc ] block Optional block for defining extensions.
143+
# @param &block Optional block for defining extensions.
144144
def belongs_to(name, options = {}, &block)
145145
define_association!(__method__, name, options, &block)
146146
end
@@ -162,7 +162,7 @@ def belongs_to(name, options = {}, &block)
162162
#
163163
# @param [ Symbol ] name The name of the association.
164164
# @param [ Hash ] options The association options.
165-
# @param [ Proc ] block Optional block for defining extensions.
165+
# @param &block Optional block for defining extensions.
166166
def has_many(name, options = {}, &block)
167167
define_association!(__method__, name, options, &block)
168168
end
@@ -184,7 +184,7 @@ def has_many(name, options = {}, &block)
184184
#
185185
# @param [ Symbol ] name The name of the association.
186186
# @param [ Hash ] options The association options.
187-
# @param [ Proc ] block Optional block for defining extensions.
187+
# @param &block Optional block for defining extensions.
188188
def has_and_belongs_to_many(name, options = {}, &block)
189189
define_association!(__method__, name, options, &block)
190190
end
@@ -206,7 +206,7 @@ def has_and_belongs_to_many(name, options = {}, &block)
206206
#
207207
# @param [ Symbol ] name The name of the association.
208208
# @param [ Hash ] options The association options.
209-
# @param [ Proc ] block Optional block for defining extensions.
209+
# @param &block Optional block for defining extensions.
210210
def has_one(name, options = {}, &block)
211211
define_association!(__method__, name, options, &block)
212212
end

lib/mongoid/association/referenced/has_many/proxy.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,8 @@ def exists?
200200
# later.
201201
#
202202
# @param [ [ Object | Array<Object> ]... ] *args The ids.
203-
# @param [ Proc ] block Optional block to pass.
203+
# @param &block Optional block to pass.
204+
# @yield [ Object ] Yields each enumerable element to the block.
204205
#
205206
# @return [ Document | Array<Document> | nil ] A document or matching documents.
206207
def find(*args, &block)
@@ -415,7 +416,7 @@ def cascade!(document)
415416
#
416417
# @param [ Symbol | String ] name The name of the method.
417418
# @param [ Object... ] *args The method args
418-
# @param [ Proc ] block Optional block to pass.
419+
# @param &block Optional block to pass.
419420
#
420421
# @return [ Criteria | Object ] A Criteria or return value from the target.
421422
ruby2_keywords def method_missing(name, *args, &block)

lib/mongoid/criteria.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ def ==(other)
8989
# enumerator = criteria.find(-> { "Default Band" })
9090
#
9191
# @param [ [ Object | Array<Object> ]... ] *args The id(s).
92-
# @param [ Proc ] block Optional block to pass.
92+
# @param &block Optional block to pass.
93+
# @yield [ Object ] Yields each enumerable element to the block.
9394
#
9495
# @return [ Document | Array<Document> | nil ] A document or matching documents.
9596
#

lib/mongoid/extensions/module.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ module Module
1313
# end
1414
#
1515
# @param [ String | Symbol ] name The name of the method.
16-
# @param [ Proc ] block The method body.
16+
# @param &block The method body.
1717
#
1818
# @return [ Method ] The new method.
1919
def re_define_method(name, &block)

lib/mongoid/fields.rb

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -287,8 +287,7 @@ class << self
287287
# end
288288
#
289289
# @param [ Symbol ] option_name the option name to match against
290-
# @param [ Proc ] block the handler to execute when the option is
291-
# provided.
290+
# @param &block the handler to execute when the option is provided.
292291
def option(option_name, &block)
293292
options[option_name] = block
294293
end
@@ -320,8 +319,10 @@ def options
320319
# @param [ Hash ] associations The associations to begin the search with.
321320
# @param [ Hash ] aliased_associations The alaised associations to begin
322321
# the search with.
323-
# @param [ Proc ] block The block takes in three paramaters, the current
324-
# meth, the field or the relation, and whether the second parameter is a
322+
# @param &block The block.
323+
# @yieldparam [ Symbol ] The current method.
324+
# @yieldparam [ Symbol | String ] The field or the relation.
325+
# @yieldparam [ true | false ] Whether the second yield parameter is a
325326
# field or not.
326327
#
327328
# @return [ Field ] The field found for the given key at the end of the
@@ -516,8 +517,10 @@ def using_object_ids?
516517
# given key.
517518
#
518519
# @param [ String ] key The key used to search the association tree.
519-
# @param [ Proc ] block The block takes in three paramaters, the current
520-
# meth, the field or the relation, and whether the second parameter is a
520+
# @param &block The block.
521+
# @yieldparam [ Symbol ] The current method.
522+
# @yieldparam [ Symbol | String ] The field or the relation.
523+
# @yieldparam [ true | false ] Whether the second yield parameter is a
521524
# field or not.
522525
#
523526
# @return [ Field ] The field found for the given key at the end of the

0 commit comments

Comments
 (0)