文档菜单
文档首页
/ / /
Mongoid
/

代码文档

本页内容

  • 代码文档
  • 结构
  • 格式化
  • 类型声明

Mongoid 使用其自己的风格YARD 用于代码文档。请注意本文档中概述的约定。

  • 模块: 所有类和模块定义之前应有一个文档注释。

    # This is the documentation for the class. It's amazing
    # what they do with corrugated cardboard these days.
    class CardboardBox
  • 方法: 所有方法定义之前应有一个文档注释。使用@param@yield@return 来指定输入和输出。有关详细信息,请参阅下方的 类型声明

    # Turn a person into whatever they'd like to be.
    #
    # @param [ Person ] person The human to transmogrify.
    #
    # @return [ Tiger ] The transmogrified result.
    def transmogrify(person)
  • 错误: 使用 @raise 来解释方法特有的错误。

    # @raise [ Errors::Validations ] If validation failed.
    def validate!
  • 私有方法: 私有方法应进行文档说明,除非它们非常简短且直接,以至于其功能明显。请注意,例如,一个方法可能简短且直接,但其参数类型可能不明显,在这种情况下,参数必须适当进行文档说明。

    private
    # Documentation is optional here.
    def do_something_obvious
  • API 私有: 不打算供外部使用的类和公共方法应标记为 @api private。此宏不需要注释。

    请注意,由于 Mongoid 的模块被混合到应用程序类中,方法的 private 可见性不一定表示其作为 API 私有方法的状态。

    # This is an internal-only method.
    #
    # @api private
    def dont_call_me_from_outside
  • 注意事项和 TODO: 使用 @note 来解释注意事项、边缘情况和可能令用户惊讶的行为。使用 @todo 来记录后续工作和未来改进的建议。

    # Clear all stored data.
    #
    # @note This operation deletes data in the database.
    # @todo Refactor this method for performance.
    def erase_data!
  • 弃用: 使用 @deprecated 宏来标记已弃用的功能。此宏不需要注释。

    # This is how we did things back in the day.
    #
    # @deprecated
    def the_old_way
  • 换行:在宏的行内换行时使用双空格缩进。不要在描述中缩进行内换行。

    # This is the description of the method. Line wraps in the description
    # should not be indented.
    #
    # @return [ Symbol ] For macros, wraps must be double-space indented
    # on the second, third, etc. lines.
  • 空白:不要使用前导/尾随空行注释,或者连续多个空行注释。

    # GOOD:
    # @return [ Symbol ] The return value
    def my_method
    # BAD:
    # @return [ Symbol ] The return value
    #
    def my_method
    # BAD:
    # @param [ Symbol ] foo The input value
    #
    #
    # @return [ Symbol ] The return value
    def my_method(foo)
  • 类型联合:使用管道 | 来表示允许的类型联合。

    # @param [ Symbol | String ] name Either a Symbol or a String.
  • 嵌套类型:使用尖括号 < > 来表示类型嵌套。

    # @param [ Array<Symbol> ] array An Array of symbols.
  • 哈希:使用逗号 , 来表示键和值类型。

    # @param [ Hash<Symbol, Integer> ] hash A Hash whose keys are Symbols,
    # and whose values are Integers.
  • 数组:使用管道 | 来表示允许的类型联合。

    # @param [ Array<Symbol | String> ] array An Array whose members must
    # be either Symbols or Strings.
  • 数组:使用逗号 , 来表示元组中每个位置的类型。

    # @return [ Array<Symbol, Integer, Integer> ] A 3-member Array whose first
    # element is a Symbol, and whose second and third elements are Integers.
  • 数组:如果数组内的类型不能混合,则在顶层使用管道 |

    # @param [ Array<Symbol> | Array<Hash> ] array An Array containing only
    # Symbols, or an Array containing only Hashes. The Array may not contain
    # a mix of Symbols and Hashes.
  • 嵌套类型:为了清晰起见,当使用逗号时,使用方括号 [ ] 来表示嵌套联合。

    # @param [ Hash<Symbol, [ true | false ]> ] hash A Hash whose keys are Symbols,
    # and whose values are boolean values.
  • Ruby值:可以在类型中使用Ruby语法来表示特定值。

    # @param [ :before | :after ] timing One of the Symbol values :before or :after.
  • True, False, 和 Nil:使用 truefalsenil 而不是 TrueClassFalseClassNilClass。由于在Ruby中不存在,不要使用 Boolean 作为类型。

    # GOOD:
    # @param [ true | false | nil ] bool A boolean or nil value.
    # BAD:
    # @param [ TrueClass | FalseClass | NilClass ] bool A boolean or nil value.
    # @param [ Boolean ] bool A boolean value.
  • 返回self:当一个方法返回 self 时,指定返回值 self

    # @return [ self ] Returns the object itself.
  • 展开参数: 在类型声明中使用省略号 ... 表示展开,在参数名中使用星号 * 表示展开。

    # @param [ String... ] *items The list of items name(s) as Strings.
    def buy_groceries(*items)
  • 展开参数: 只有当每个参数实际上是一个数组时,才可以将 Array 用作类型。

    # BAD:
    # @param [ Array<String> ] *items The list of items name(s) as Strings.
    def buy_groceries(*items)
    buy_groceries("Cheese", "Crackers", "Wine")
    # OK:
    # @param [ Array<String>... ] *arrays One or more arrays containing name parts.
    def set_people_names(*arrays)
    set_people_names(["Harlan", "Sanders"], ["Jane", "K", ""Doe"], ["Jim", "Beam"])
  • 展开参数: 使用逗号 , 来表示展开中的位置。

    # @param [ Symbol..., Hash ] *args A list of names, followed by a hash
    # as the optional last arg.
    def say_hello(*args)
  • 展开参数: 使用方括号 [ ] 指定类型联合。

    # @param [ [ String | Symbol ]... ] *fields A splat of mixed Symbols and Strings.
  • 关键字参数: 依照 YARD 规范,使用 @param 来指定关键字参数,并将关键字参数名称指定为符号。

    # @param [ String ] query The search string
    # @param [ Boolean ] :exact_match Whether to do an exact match
    # @param [ Integer ] :results_per_page Number of results
    def search(query, exact_match: false, results_per_page: 10)
  • 哈希选项: 使用 @option 宏在 Hash @param 后立即定义哈希键值选项。注意 @option 的参数名称是符号。

    # @param opts [ Hash<Symbol, Object> ] The optional hash argument(s).
    # @option opts [ String | Array<String> ] :items The items(s) as Strings to include.
    # @option opts [ Integer ] :limit An Integer denoting the limit.
    def buy_groceries(opts = {})
  • 双重展开: 使用双星号 ** 在参数名中表示关键字参数展开(双重展开)。注意类型在双重展开元素上不需要声明,因为它隐式地是 <Symbol, Object>。相反,使用 @option 宏在下面定义值类型。注意 @option 的参数名称是符号。

    # @param **kwargs The optional keyword argument(s).
    # @option **kwargs [ String | Array<String> ] :items The items(s) as Strings to include.
    # @option **kwargs [ Integer ] :limit An Integer denoting the limit.
    def buy_groceries(**kwargs)
  • 块: 使用 @yield 来指定方法何时将控制权传递给块。

    # @yield [ Symbol, Symbol, Symbol ] Evaluate the guess of who did the crime.
    # Must take the person, location, and weapon used. Must return true or false.
    def whodunit
    yield(:mustard, :ballroom, :candlestick)
    end
  • 块: 如果方法明确指定了块参数,请使用前置一个与号 &@param 来指定块参数,并也指定 @yield。注意即使方法在内部调用 block.call 而不是 yield,也应该使用 @yield

    # @param &block The block.
    # @yield [ Symbol, Symbol, Symbol ] Evaluate the guess of who did the crime.
    # Must take the person, location, and weapon used. Must return true or false.
    def whodunit(&block)
    yield(:scarlet, :library, :rope)
    end
    # @param &block The block.
    # @yield [ Symbol, Symbol, Symbol ] Evaluate the guess of who did the crime.
    # Must take the person, location, and weapon used. Must return true or false.
    def whodunit(&block)
    block.call(:plum, :kitchen, :pipe)
    end
  • 块: 当有助于清晰度时,使用 @yieldparam@yieldreturn 而不是 @yield

    # @param &block The block.
    # @yieldparam [ Symbol ] The person.
    # @yieldparam [ Symbol ] The location.
    # @yieldparam [ Symbol ] The weapon used.
    # @yieldreturn [ true | false ] Whether the guess is correct.
    def whodunit(&block)
    yield(:peacock, :conservatory, :wrench)
    end
  • Proc 参数: Proc 参数应该使用 @param(而不是 @yield)。Proc 的输入可以指定为子类型。

    # @param [ Proc<Integer, Integer, Integer> ] my_proc Proc argument which must
    # take 3 integers and must return true or false whether the guess is valid.
    def guess_three(my_proc)
    my_proc.call(42, 7, 88)
    end

返回

贡献