|
| 1 | +.. _mongoid-code-documentation: |
| 2 | + |
| 3 | +================== |
| 4 | +Code Documentation |
| 5 | +================== |
| 6 | + |
| 7 | +.. contents:: On this page |
| 8 | + :local: |
| 9 | + :backlinks: none |
| 10 | + :depth: 2 |
| 11 | + :class: singlecol |
| 12 | + |
| 13 | +Overview |
| 14 | +-------- |
| 15 | + |
| 16 | +In this guide, you can learn about {+odm+}'s code documentation |
| 17 | +conventions. If you have a suggestion to improve the API documentation |
| 18 | +or introduce a new feature, you can use this guide to help you format |
| 19 | +your documentation. |
| 20 | + |
| 21 | +{+odm+} uses a variation of :github:`YARD <lsegal/yard>`, a {+language+} |
| 22 | +documentation tool, for its code documentation. |
| 23 | + |
| 24 | +.. _mongoid-code-documentation-structure: |
| 25 | + |
| 26 | +Structure |
| 27 | +--------- |
| 28 | + |
| 29 | +- **Modules:** All class and module definitions must be preceded by |
| 30 | + a documentation comment. |
| 31 | + |
| 32 | + .. code-block:: ruby |
| 33 | + |
| 34 | + # This is the documentation for the class. It's amazing |
| 35 | + # what they do with corrugated cardboard these days. |
| 36 | + class CardboardBox |
| 37 | + |
| 38 | +- **Methods:** All method definitions must be preceded by a |
| 39 | + documentation comment. Use ``@param``, ``@yield``, and ``@return`` to |
| 40 | + specify inputs and output. To learn more, see the |
| 41 | + :ref:`mongoid-code-documentation-type-declaration` section. |
| 42 | + |
| 43 | + .. code-block:: ruby |
| 44 | + |
| 45 | + # Turn a person into whatever they'd like to be. |
| 46 | + # |
| 47 | + # @param [ Person ] person The human to transmogrify. |
| 48 | + # |
| 49 | + # @return [ Tiger ] The transmogrified result. |
| 50 | + def transmogrify(person) |
| 51 | + |
| 52 | +- **Errors:** Use ``@raise`` to describe errors specific to the method. |
| 53 | + |
| 54 | + .. code-block:: ruby |
| 55 | + |
| 56 | + # @raise [ Errors::Validations ] If validation failed. |
| 57 | + def validate! |
| 58 | + |
| 59 | +- **Private Methods:** Private methods must be documented unless they are |
| 60 | + so brief and straightforward that it is obvious what they do. Note that, |
| 61 | + for example, a method might be brief and straightforward but the type of |
| 62 | + its parameter may not be obvious, in which case the parameter must be |
| 63 | + appropriately documented. |
| 64 | + |
| 65 | + .. code-block:: ruby |
| 66 | + |
| 67 | + private |
| 68 | + |
| 69 | + # Documentation is optional here. |
| 70 | + def do_something_obvious |
| 71 | + |
| 72 | +- **API Private:** Classes and public methods which are not intended for |
| 73 | + external usage must be marked ``@api private``. This macro does not |
| 74 | + require a comment. |
| 75 | + |
| 76 | + Note that, because {+odm+}'s modules are mixed into application classes, |
| 77 | + ``private`` visibility of a method does not necessarily indicate its |
| 78 | + status as an API private method. |
| 79 | + |
| 80 | + .. code-block:: ruby |
| 81 | + |
| 82 | + # This is an internal-only method. |
| 83 | + # |
| 84 | + # @api private |
| 85 | + def dont_call_me_from_outside |
| 86 | + |
| 87 | +- **Notes and TODOs:** Use ``@note`` to explain caveats, edge cases, |
| 88 | + and behavior which may surprise users. Use ``@todo`` to record |
| 89 | + follow-ups and suggestions for future improvement. |
| 90 | + |
| 91 | + .. code-block:: ruby |
| 92 | + |
| 93 | + # Clear all stored data. |
| 94 | + # |
| 95 | + # @note This operation deletes data in the database. |
| 96 | + # @todo Refactor this method for performance. |
| 97 | + def erase_data! |
| 98 | + |
| 99 | +- **Deprecation:** Use the ``@deprecated`` macro to indicate deprecated |
| 100 | + functionality. This macro does not require a comment. |
| 101 | + |
| 102 | + .. code-block:: ruby |
| 103 | + |
| 104 | + # This is how we did things back in the day. |
| 105 | + # |
| 106 | + # @deprecated |
| 107 | + def the_old_way |
| 108 | + |
| 109 | +.. _mongoid-code-documentation-formatting: |
| 110 | + |
| 111 | +Formatting |
| 112 | +---------- |
| 113 | + |
| 114 | +- **Line Wrapping:** Use double-space indent when wrapping lines of macros. |
| 115 | + Do not indent line wraps in the description. |
| 116 | + |
| 117 | + .. code-block:: ruby |
| 118 | + |
| 119 | + # This is the description of the method. Line wraps in the description |
| 120 | + # must not be indented. |
| 121 | + # |
| 122 | + # @return [ Symbol ] For macros, wraps must be double-space indented |
| 123 | + # on the second, third (and so on) lines. |
| 124 | + |
| 125 | +- **Whitespace:** Do not use leading/trailing empty comment lines, |
| 126 | + or more than one consecutive empty comment line. |
| 127 | + |
| 128 | + .. code-block:: ruby |
| 129 | + |
| 130 | + # DO THIS: |
| 131 | + # @return [ Symbol ] The return value |
| 132 | + def my_method |
| 133 | + |
| 134 | + # NOT THIS: |
| 135 | + # @return [ Symbol ] The return value |
| 136 | + # |
| 137 | + def my_method |
| 138 | + |
| 139 | + # NOT THIS: |
| 140 | + # @param [ Symbol ] foo The input value |
| 141 | + # |
| 142 | + # |
| 143 | + # @return [ Symbol ] The return value |
| 144 | + def my_method(foo) |
| 145 | + |
| 146 | +.. _mongoid-code-documentation-type-declaration: |
| 147 | + |
| 148 | +Type Declaration |
| 149 | +---------------- |
| 150 | + |
| 151 | +- **Type Unions:** Use pipe ``|`` to denote a union of allowed types. |
| 152 | + |
| 153 | + .. code-block:: ruby |
| 154 | + |
| 155 | + # @param [ Symbol | String ] name Either a Symbol or a String. |
| 156 | + |
| 157 | +- **Nested Types:** Use angle brackets ``< >`` to denote type nesting. |
| 158 | + |
| 159 | + .. code-block:: ruby |
| 160 | + |
| 161 | + # @param [ Array<Symbol> ] array An Array of symbols. |
| 162 | + |
| 163 | +- **Hash:** Use comma ``,`` to denote the key and value types. |
| 164 | + |
| 165 | + .. code-block:: ruby |
| 166 | + |
| 167 | + # @param [ Hash<Symbol, Integer> ] hash A Hash whose keys are Symbols, |
| 168 | + # and whose values are Integers. |
| 169 | + |
| 170 | +- **Array:** Use pipe ``|`` to denote a union of allowed types. |
| 171 | + |
| 172 | + .. code-block:: ruby |
| 173 | + |
| 174 | + # @param [ Array<Symbol | String> ] array An Array whose members must |
| 175 | + # be either Symbols or Strings. |
| 176 | + |
| 177 | +- **Array:** Use comma ``,`` to denote the types of each position in a tuple. |
| 178 | + |
| 179 | + .. code-block:: ruby |
| 180 | + |
| 181 | + # @return [ Array<Symbol, Integer, Integer> ] A 3-member Array whose first |
| 182 | + # element is a Symbol, and whose second and third elements are Integers. |
| 183 | + |
| 184 | +- **Array:** Use pipe ``|`` on the top level if the inner types cannot be |
| 185 | + mixed within the Array. |
| 186 | + |
| 187 | + .. code-block:: ruby |
| 188 | + |
| 189 | + # @param [ Array<Symbol> | Array<Hash> ] array An Array containing only |
| 190 | + # Symbols, or an Array containing only Hashes. The Array may not contain |
| 191 | + # a mix of Symbols and Hashes. |
| 192 | + |
| 193 | +- **Nested Types:** For clarity, use square brackets ``[ ]`` to denote nested unions |
| 194 | + when commas are also used. |
| 195 | + |
| 196 | + .. code-block:: ruby |
| 197 | + |
| 198 | + # @param [ Hash<Symbol, [ true | false ]> ] hash A Hash whose keys are Symbols, |
| 199 | + # and whose values are boolean values. |
| 200 | + |
| 201 | +- **Ruby Values:** Specific values may be denoted in the type using Ruby syntax. |
| 202 | + |
| 203 | + .. code-block:: ruby |
| 204 | + |
| 205 | + # @param [ :before | :after ] timing One of the Symbol values :before or :after. |
| 206 | + |
| 207 | +- **True, False, and Nil:** Use ``true``, ``false``, and ``nil`` rather than |
| 208 | + ``TrueClass``, ``FalseClass``, and ``NilClass``. Do not use ``Boolean`` as a type |
| 209 | + since it does not exist in Ruby. |
| 210 | + |
| 211 | + .. code-block:: ruby |
| 212 | + |
| 213 | + # DO THIS: |
| 214 | + # @param [ true | false | nil ] bool A boolean or nil value. |
| 215 | + |
| 216 | + # NOT THIS: |
| 217 | + # @param [ TrueClass | FalseClass | NilClass ] bool A boolean or nil value. |
| 218 | + # @param [ Boolean ] bool A boolean value. |
| 219 | + |
| 220 | +- **Return Self:** Specify return value ``self`` where a method returns ``self``. |
| 221 | + |
| 222 | + .. code-block:: ruby |
| 223 | + |
| 224 | + # @return [ self ] Returns the object itself. |
| 225 | + |
| 226 | +- **Splat Args:** Use three-dot ellipses ``...`` in the type declaration and |
| 227 | + star ``*`` in the parameter name to denote a splat. |
| 228 | + |
| 229 | + .. code-block:: ruby |
| 230 | + |
| 231 | + # @param [ String... ] *items The list of items' names as Strings. |
| 232 | + def buy_groceries(*items) |
| 233 | + |
| 234 | +- **Splat Args:** Do not use ``Array`` as the type unless each arg is actually an Array. |
| 235 | + |
| 236 | + .. code-block:: ruby |
| 237 | + |
| 238 | + # DO NOT DO THIS: |
| 239 | + # @param [ Array<String> ] *items The list of items' names as Strings. |
| 240 | + def buy_groceries(*items) |
| 241 | + |
| 242 | + buy_groceries("Cheese", "Crackers", "Wine") |
| 243 | + |
| 244 | + # DO THIS: |
| 245 | + # @param [ Array<String>... ] *arrays One or more arrays containing name parts. |
| 246 | + def set_people_names(*arrays) |
| 247 | + |
| 248 | + set_people_names(["Harlan", "Sanders"], ["Jane", "K", ""Doe"], ["Jim", "Beam"]) |
| 249 | + |
| 250 | +- **Splat Args:** Use comma ``,`` to denote positionality in a splat. |
| 251 | + |
| 252 | + .. code-block:: ruby |
| 253 | + |
| 254 | + # @param [ Symbol..., Hash ] *args A list of names, followed by a hash |
| 255 | + # as the optional last arg. |
| 256 | + def say_hello(*args) |
| 257 | + |
| 258 | +- **Splat Args:** Specify type unions with square brackets ``[ ]``. |
| 259 | + |
| 260 | + .. code-block:: ruby |
| 261 | + |
| 262 | + # @param [ [ String | Symbol ]... ] *fields A splat of mixed Symbols and Strings. |
| 263 | + |
| 264 | +- **Keyword Arguments:** Following YARD conventions, use ``@param`` for keyword |
| 265 | + arguments, and specify keyword argument names as symbols. |
| 266 | + |
| 267 | + .. code-block:: ruby |
| 268 | + |
| 269 | + # @param [ String ] query The search string |
| 270 | + # @param [ Boolean ] :exact_match Whether to do an exact match |
| 271 | + # @param [ Integer ] :results_per_page Number of results |
| 272 | + def search(query, exact_match: false, results_per_page: 10) |
| 273 | + |
| 274 | +- **Hash Options:** Define hash key-value options with ``@option`` macro |
| 275 | + immediately following the Hash ``@param``. Note ``@option`` parameter names |
| 276 | + are symbols. |
| 277 | + |
| 278 | + .. code-block:: ruby |
| 279 | + |
| 280 | + # @param opts [ Hash<Symbol, Object> ] The optional hash argument or arguments. |
| 281 | + # @option opts [ String | Array<String> ] :items The items as Strings to include. |
| 282 | + # @option opts [ Integer ] :limit An Integer denoting the limit. |
| 283 | + def buy_groceries(opts = {}) |
| 284 | + |
| 285 | +- **Double Splats:** Use double-star ``**`` in the parameter name to denote a |
| 286 | + keyword arg splat (double splat). Note that type does not need declared on |
| 287 | + the double-splat element, as it is implicitly <Symbol, Object>. Instead, |
| 288 | + define value types with ``@option`` macro below. Note ``@option`` parameter |
| 289 | + names are symbols. |
| 290 | + |
| 291 | + .. code-block:: ruby |
| 292 | + |
| 293 | + # @param **kwargs The optional keyword argument or arguments. |
| 294 | + # @option **kwargs [ String | Array<String> ] :items The items as Strings to include. |
| 295 | + # @option **kwargs [ Integer ] :limit An Integer denoting the limit. |
| 296 | + def buy_groceries(**kwargs) |
| 297 | + |
| 298 | +- **Blocks:** Use ``@yield`` to specify when the method yields to a block. |
| 299 | + |
| 300 | + .. code-block:: ruby |
| 301 | + |
| 302 | + # @yield [ Symbol, Symbol, Symbol ] Evaluate the guess of who did the crime. |
| 303 | + # Must take the person, location, and weapon used. Must return true or false. |
| 304 | + def whodunit |
| 305 | + yield(:mustard, :ballroom, :candlestick) |
| 306 | + end |
| 307 | + |
| 308 | +- **Blocks:** If the method explicitly specifies a block argument, specify the block |
| 309 | + argument using ``@param`` preceded by an ampersand ``&``, and also specify ``@yield``. |
| 310 | + Note ``@yield`` must be used even when method calls ``block.call`` rather than |
| 311 | + ``yield`` internally. |
| 312 | + |
| 313 | + .. code-block:: ruby |
| 314 | + |
| 315 | + # @param &block The block. |
| 316 | + # @yield [ Symbol, Symbol, Symbol ] Evaluate the guess of who did the crime. |
| 317 | + # Must take the person, location, and weapon used. Must return true or false. |
| 318 | + def whodunit(&block) |
| 319 | + yield(:scarlet, :library, :rope) |
| 320 | + end |
| 321 | + |
| 322 | + # @param &block The block. |
| 323 | + # @yield [ Symbol, Symbol, Symbol ] Evaluate the guess of who did the crime. |
| 324 | + # Must take the person, location, and weapon used. Must return true or false. |
| 325 | + def whodunit(&block) |
| 326 | + block.call(:plum, :kitchen, :pipe) |
| 327 | + end |
| 328 | + |
| 329 | +- **Blocks:** Use ``@yieldparam`` and ``@yieldreturn`` instead of ``@yield`` where |
| 330 | + beneficial for clarity. |
| 331 | + |
| 332 | + .. code-block:: ruby |
| 333 | + |
| 334 | + # @param &block The block. |
| 335 | + # @yieldparam [ Symbol ] The person. |
| 336 | + # @yieldparam [ Symbol ] The location. |
| 337 | + # @yieldparam [ Symbol ] The weapon used. |
| 338 | + # @yieldreturn [ true | false ] Whether the guess is correct. |
| 339 | + def whodunit(&block) |
| 340 | + yield(:peacock, :conservatory, :wrench) |
| 341 | + end |
| 342 | + |
| 343 | +- **Proc Args:** Use ``@param`` (not ``@yield``) for proc arguments. The |
| 344 | + inputs to the proc may be specified as subtypes. |
| 345 | + |
| 346 | + .. code-block:: ruby |
| 347 | + |
| 348 | + # @param [ Proc<Integer, Integer, Integer> ] my_proc Proc argument which must |
| 349 | + # take 3 integers and must return true or false whether the guess is valid. |
| 350 | + def guess_three(my_proc) |
| 351 | + my_proc.call(42, 7, 88) |
| 352 | + end |
0 commit comments