Document model attributes with YARD

此生再无相见时 提交于 2019-12-02 22:57:32

After quite a while searching around, I punted and manually added the documentation for the attributes to the model files. This is certainly not ideal, but hopefully the model structure won't change a whole lot.

I created a .yardopts file for the project and used the yard command-line options to create two new tags for marking these up:

--type-name-tag 'attribute:Attributes' --type-name-tag 'association:Associations'

These provide me with specific tags for marking up the attributes and associations; they will show up grouped under the "Attributes" and "Associations" headings in the documentation. I can add this:

# @attribute name [String] The name of the object
# @association relatedObjs [Array<AnotherClass>] Objects needed to perform a certain function

Maybe someone will write a plugin for YARD that will parse out the annotate-models output.

It seems that YARD now has its own @!attribute (notice the exclamation mark) tag for this purpose:

http://rubydoc.info/docs/yard/file/docs/Tags.md#attribute

Example:

class Task < ActiveRecord::Base
  # @!attribute name
  #   @return [String] The name of the task.

  # @!attribute description
  #   @return [String] The description of the task.

  # @!attribute active
  #   @return [Boolean] Marks whether the task is active or not.
end

This will result in nice documentation of your attributes. The only thing to watch out is that you always keep your documentation up to date because nobody will check whether you remove an attribute from your documentation when you deleted it from the database, etc.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!