YARD:记录自定义 getter/setter 对,如 attr_accessor
我正在使用 YARD 来记录我的项目。 创建的 YARD 文档属性
attr_accessor :some_attribute
在单独的“实例属性摘要”部分中 。现在我有另一个属性,但是使用自定义 setter 和 getter,
def some_other_attribute
# ...
end
def some_other_attribute= value
# ...
end
所以基本上我的问题是,如何让 YARD 记录这对 setter/getter,就像前一种情况中的 attr_accessor
一样,并列出 “实例属性摘要”中的“some_other_attribute
”?
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_other_attribute
within "Instance Attribute Summary"?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从 0.8 开始(目前处于预发行版),
@!attribute
指令是表示对象是属性的推荐方法。已弃用@attr_*
标签,取而代之的是该指令。您还可以(在 0.8.0+ 中):解析不一定由 Ruby 执行的代码。在 0.8 之前,您可以直接添加 attr_accessor,然后重新定义 setter/getter,如下所示:
Ruby 不应该介意,除了在 ruby -w 中它会警告方法重新定义。如果这让您感到困扰,您也可以在其中添加
undef foo, foo=
。它有点混乱(如果你关心 -w),这就是为什么我们添加了诸如@!parse
和@!attribute
之类的东西。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+):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:
Ruby shouldn't mind, except that in
ruby -w
it will warn about method redefinitions. If this bugs you, you can addundef 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
.您应该能够在类上使用 @attr 标记:
还有其他标记(例如 @attr_reader 和 @attr_writer)也很有帮助。
You should be able to use the @attr tag on the class:
There are other tags (like @attr_reader and @attr_writer) than can also be helpful.