开发者

YARD: document custom getter/setter pair like attr_accessor

I'm using YARD to document my project. YARD document attributes created with

attr_accessor :some_attribute

in a separate section "Instance Attribute Summary". Now I have another attribute, but with custom setter and getter

def some_other_attribute
  # ...
end

def some_other_attribute= value
  # ...
end

so basically my question is, how can I get YARD to document this pair of setter/getter just like attr_accessor in the previous case, and list some_othe开发者_开发技巧r_attribute within "Instance Attribute Summary"?


As of 0.8 (which is in pre-release right now), the @!attribute directive is the recommended way to denote that an object is an attribute. The @attr_* tags are deprecated in favour of this directive. You could also do (in 0.8.0+):

# @!parse attr_accessor :some_attribute

To parse code that isn't necessarily executed by Ruby. Prior to 0.8, you could just add the attr_accessor directly and then redefine the setter/getter as follows:

class MyClass
  attr_accessor :foo
  def foo; something_else end
  def foo=(v) something_else(v) end
end

Ruby shouldn't mind, except that in ruby -w it will warn about method redefinitions. If this bugs you, you can add undef foo, foo= in there too. It's a little messy (if you care about -w), which is why we added things like @!parse and @!attribute.


You should be able to use the @attr tag on the class:

# @attr [String] name The name of this object. 
class MyClass
  def name
  end
  def name=
  end
end

There are other tags (like @attr_reader and @attr_writer) than can also be helpful.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜