|
| 1 | +.. _code-documentation: |
| 2 | + |
| 3 | +****************** |
| 4 | +Code Documentation |
| 5 | +****************** |
| 6 | + |
| 7 | +.. default-domain:: mongodb |
| 8 | + |
| 9 | +.. contents:: On this page |
| 10 | + :local: |
| 11 | + :backlinks: none |
| 12 | + :depth: 2 |
| 13 | + :class: singlecol |
| 14 | + |
| 15 | + |
| 16 | +Code Documentation |
| 17 | +================== |
| 18 | + |
| 19 | +Mongoid uses its own flavor of `YARD <https://github.com/lsegal/yard>`_ |
| 20 | +for code documentation. Please note the conventions outlined in this document. |
| 21 | + |
| 22 | + |
| 23 | +.. _code-documentation-structure: |
| 24 | + |
| 25 | +Structure |
| 26 | +--------- |
| 27 | + |
| 28 | +- **Modules:** All class and module definitions should be preceded by |
| 29 | + a documentation comment. |
| 30 | + |
| 31 | + .. code-block:: ruby |
| 32 | + |
| 33 | + # This is the documentation for the class. It's amazing |
| 34 | + # what they do with corrugated cardboard these days. |
| 35 | + class CardboardBox |
| 36 | + |
| 37 | +- **Methods:** All method definitions should be preceded by a documentation comment. |
| 38 | + Use ``@param`` and ``@return`` to specify input(s) and output respectively. |
| 39 | + For further details, refer to |
| 40 | + :ref:`Type Declaration <code-documentation-type-declaration>` below. |
| 41 | + |
| 42 | + .. code-block:: ruby |
| 43 | + |
| 44 | + # Turn a person into whatever they'd like to be. |
| 45 | + # |
| 46 | + # @param [ Person ] person The human to transmogrify. |
| 47 | + # |
| 48 | + # @return [ Tiger ] The transmogrified result. |
| 49 | + def transmogrify(person) |
| 50 | + |
| 51 | +- **Private Methods:** Private methods should be documented unless they are |
| 52 | + so brief and straightforward that it is obvious what they do. Note that, |
| 53 | + for example, a method may be brief and straightforward but the type of |
| 54 | + its parameter may not be obvious, in which case the parameter needs to |
| 55 | + be appropriately documented. |
| 56 | + |
| 57 | + .. code-block:: ruby |
| 58 | + |
| 59 | + private |
| 60 | + |
| 61 | + # Documentation is optional here. |
| 62 | + def my_internal_method |
| 63 | + |
| 64 | +- **Notes:** Use the ``@note`` macro to explain caveats, edge cases, |
| 65 | + and behavior which may surprise users. |
| 66 | + |
| 67 | + .. code-block:: ruby |
| 68 | + |
| 69 | + # Clear all stored data. |
| 70 | + # |
| 71 | + # @note This operation deletes data in the database. |
| 72 | + def erase_data! |
| 73 | + |
| 74 | +- **API Private:** Classes and public methods which are not intended for |
| 75 | + external usage should be marked ``@api private``. This macro does not |
| 76 | + require a comment. |
| 77 | + |
| 78 | + Note that, because Mongoid's modules are mixed into application classes, |
| 79 | + ``private`` visibility of a method does not necessarily indicate its |
| 80 | + status as an API private method. |
| 81 | + |
| 82 | + .. code-block:: ruby |
| 83 | + |
| 84 | + # This is an internal-only method. |
| 85 | + # |
| 86 | + # @api private |
| 87 | + def dont_call_me_from_outside |
| 88 | + |
| 89 | +- **Deprecation:** Use the ``@deprecated`` macro to indicate deprecated |
| 90 | + functionality. This macro does not require a comment. |
| 91 | + |
| 92 | + .. code-block:: ruby |
| 93 | + |
| 94 | + # This is how we did things back in the day. |
| 95 | + # |
| 96 | + # @deprecated |
| 97 | + def the_old_way |
| 98 | + |
| 99 | + |
| 100 | +.. _code-documentation-formatting: |
| 101 | + |
| 102 | +Formatting |
| 103 | +---------- |
| 104 | + |
| 105 | +- **Line Wrapping:** Use double-space indent when wrapping lines of macros. |
| 106 | + Do not indent line wraps in the description. |
| 107 | + |
| 108 | + .. code-block:: ruby |
| 109 | + |
| 110 | + # This is the description of the method. Line wraps in the description |
| 111 | + # should not be indented. |
| 112 | + # |
| 113 | + # @return [ Symbol ] For macros, wraps must be double-space indented |
| 114 | + # on the second, third, etc. lines. |
| 115 | + |
| 116 | +- **Whitespace:** Do not use leading/trailing empty comment lines, |
| 117 | + or more than one consecutive empty comment line. |
| 118 | + |
| 119 | + .. code-block:: ruby |
| 120 | + |
| 121 | + # GOOD: |
| 122 | + # @return [ Symbol ] The return value |
| 123 | + def my_method |
| 124 | + |
| 125 | + # BAD: |
| 126 | + # @return [ Symbol ] The return value |
| 127 | + # |
| 128 | + def my_method |
| 129 | + |
| 130 | + # BAD: |
| 131 | + # @param [ Symbol ] foo The input value |
| 132 | + # |
| 133 | + # |
| 134 | + # @return [ Symbol ] The return value |
| 135 | + def my_method(foo) |
| 136 | + |
| 137 | + |
| 138 | +.. _code-documentation-type-declaration: |
| 139 | + |
| 140 | +Type Declaration |
| 141 | +---------------- |
| 142 | + |
| 143 | +- **Type Unions:** Use pipe ``|`` to denote a union of allowed types. |
| 144 | + |
| 145 | + .. code-block:: ruby |
| 146 | + |
| 147 | + # @param [ Symbol | String ] name Either a Symbol or a String. |
| 148 | + |
| 149 | +- **Nested Types:** Use angle brackets ``< >`` to denote type nesting. |
| 150 | + |
| 151 | + .. code-block:: ruby |
| 152 | + |
| 153 | + # @param [ Array<Symbol> ] array An Array of symbols. |
| 154 | + |
| 155 | +- **Hash:** Use comma ``,`` to denote the key and value types. |
| 156 | + |
| 157 | + .. code-block:: ruby |
| 158 | + |
| 159 | + # @param [ Hash<Symbol, Integer> ] hash A Hash whose keys are Symbols, |
| 160 | + # and whose values are Integers. |
| 161 | + |
| 162 | +- **Array:** Use pipe ``|`` to denote a union of allowed types. |
| 163 | + |
| 164 | + .. code-block:: ruby |
| 165 | + |
| 166 | + # @param [ Array<Symbol | String> ] array An Array whose members must |
| 167 | + # be either Symbols or Strings. |
| 168 | + |
| 169 | +- **Array:** Use comma ``,`` to denote the types of each position in a tuple. |
| 170 | + |
| 171 | + .. code-block:: ruby |
| 172 | + |
| 173 | + # @return [ Array<Symbol, Integer, Integer> ] A 3-member Array whose first |
| 174 | + # element is a Symbol, and whose second and third elements are Integers. |
| 175 | + |
| 176 | +- **Array:** Use pipe ``|`` on the top level if the inner types cannot be |
| 177 | + mixed within the Array. |
| 178 | + |
| 179 | + .. code-block:: ruby |
| 180 | + |
| 181 | + # @param [ Array<Symbol> | Array<Hash> ] array An Array containing only |
| 182 | + # Symbols, or an Array containing only Hashes. The Array may not contain |
| 183 | + # a mix of Symbols and Hashes. |
| 184 | + |
| 185 | +- **Nested Types:** For clarity, use square brackets ``[ ]`` to denote nested unions |
| 186 | + when commas are also used. |
| 187 | + |
| 188 | + .. code-block:: ruby |
| 189 | + |
| 190 | + # @param [ Hash<Symbol, [ true | false ]> ] hash A Hash whose keys are Symbols, |
| 191 | + # and whose values are boolean values. |
| 192 | + |
| 193 | +- **Ruby Values:** Specific values may be denoted in the type using Ruby syntax. |
| 194 | + |
| 195 | + .. code-block:: ruby |
| 196 | + |
| 197 | + # @param [ :before | :after ] timing One of the Symbol values :before or :after. |
| 198 | + |
| 199 | +- **True, False, and Nil:** Use ``true``, ``false``, and ``nil`` rather than |
| 200 | + ``TrueClass``, ``FalseClass``, and ``NilClass``. Do not use ``Boolean`` as a type |
| 201 | + since it does not exist in Ruby. |
| 202 | + |
| 203 | + .. code-block:: ruby |
| 204 | + |
| 205 | + # GOOD: |
| 206 | + # @param [ true | false | nil ] bool A boolean or nil value. |
| 207 | + |
| 208 | + # BAD: |
| 209 | + # @param [ TrueClass | FalseClass | NilClass ] bool A boolean or nil value. |
| 210 | + # @param [ Boolean ] bool A boolean value. |
| 211 | + |
| 212 | +- **Return Self:** Specify return value ``self`` where a method returns ``self``. |
| 213 | + |
| 214 | + .. code-block:: ruby |
| 215 | + |
| 216 | + # @return [ self ] Returns the object itself. |
| 217 | + |
| 218 | +- **Splat Args:** Use three-dot ellipses ``...`` in the type declaration and |
| 219 | + star ``*`` in the parameter name to denote a splat. |
| 220 | + |
| 221 | + .. code-block:: ruby |
| 222 | + |
| 223 | + # @param [ String... ] *items The list of items name(s) as Strings. |
| 224 | + def buy_groceries(*items) |
| 225 | + |
| 226 | +- **Splat Args:** Do not use ``Array`` as the type unless each arg is actually an Array. |
| 227 | + |
| 228 | + .. code-block:: ruby |
| 229 | + |
| 230 | + # BAD: |
| 231 | + # @param [ Array<String> ] *items The list of items name(s) as Strings. |
| 232 | + def buy_groceries(*items) |
| 233 | + |
| 234 | + buy_groceries("Cheese", "Crackers", "Wine") |
| 235 | + |
| 236 | + # OK: |
| 237 | + # @param [ Array<String>... ] *arrays One or more arrays containing name parts. |
| 238 | + def set_people_names(*arrays) |
| 239 | + |
| 240 | + set_people_names(["Harlan", "Sanders"], ["Jane", "K", ""Doe"], ["Jim", "Beam"]) |
| 241 | + |
| 242 | +- **Splat Args:** Use comma ``,`` to denote positionality in a splat. |
| 243 | + |
| 244 | + .. code-block:: ruby |
| 245 | + |
| 246 | + # @param [ Symbol..., Hash ] *args A list of names, followed by a hash |
| 247 | + # as the optional last arg. |
| 248 | + def say_hello(*args) |
| 249 | + |
| 250 | +- **Splat Args:** Specify type unions with square brackets ``[ ]``. |
| 251 | + |
| 252 | + .. code-block:: ruby |
| 253 | + |
| 254 | + # @param [ [ String | Symbol ]... ] *fields A splat of mixed Symbols and Strings. |
| 255 | + |
| 256 | +- **Keyword Arguments:** Following YARD conventions, use ``@param`` for keyword |
| 257 | + arguments, and specify keyword argument names as symbols. |
| 258 | + |
| 259 | + .. code-block:: ruby |
| 260 | + |
| 261 | + # @param [ String ] query The search string |
| 262 | + # @param [ Boolean ] :exact_match Whether to do an exact match |
| 263 | + # @param [ Integer ] :results_per_page Number of results |
| 264 | + def search(query, exact_match: false, results_per_page: 10) |
| 265 | + |
| 266 | +- **Hash Options:** Define hash key-value options with ``@option`` macro |
| 267 | + immediately following the Hash ``@param``. Note ``@option`` parameter names |
| 268 | + are symbols. |
| 269 | + |
| 270 | + .. code-block:: ruby |
| 271 | + |
| 272 | + # @param opts [ Hash<Symbol, Object> ] The optional hash argument(s). |
| 273 | + # @option opts [ String | Array<String> ] :items The items(s) as Strings to include. |
| 274 | + # @option opts [ Integer ] :limit An Integer denoting the limit. |
| 275 | + def buy_groceries(opts = {}) |
| 276 | + |
| 277 | +- **Double Splats:** Use double-star ``**`` in the parameter name to denote a |
| 278 | + keyword arg splat (double splat). Note that type does not need declared on |
| 279 | + the double-splat element, as it is implicitly <Symbol, Object>. Instead, |
| 280 | + define value types with ``@option`` macro below. Note ``@option`` parameter |
| 281 | + names are symbols. |
| 282 | + |
| 283 | + .. code-block:: ruby |
| 284 | + |
| 285 | + # @param **kwargs The optional keyword argument(s). |
| 286 | + # @option **kwargs [ String | Array<String> ] :items The items(s) as Strings to include. |
| 287 | + # @option **kwargs [ Integer ] :limit An Integer denoting the limit. |
| 288 | + def buy_groceries(**kwargs) |
0 commit comments