-
-
Notifications
You must be signed in to change notification settings - Fork 398
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
#setter= documentation is hidden if used with attr_accessor or attr_reader #516
Comments
We're going to be better documenting this usage in 0.8. The overload would looks like: class Test
# @overload foo
# @return the foo object
# @overload foo=(value)
# Sets the foo
# @param [value] the new foo
attr_accessor :foo
# redefine the getter/setter here as you please
end This only applies when you have actual documentation though-- if you have no accompanying docstring, you don't need an overload. When you say "documented", do you mean "recognized"? Because your example doesn't have any docs. Either way, the issue is that once you define a method pair as an attribute, the individual methods are no longer meaningful on their own as they are now shadowed by their "attributiness", if you will. The default template shows an attribute as a single readonly, readwrite or writeonly element-- it does not list the individual methods that make up the attribute separately, because that would defeat the purpose of the attribute abstraction. In other words, once you've declared something an attribute, all of your docs about the attribute should be attached to the attribute declaration. You should think of the attribute as its own independent abstraction from the methods in terms of your API. The individual getter/setter methods are now just implementation details for your attribute. It's possible that we could automatically check the docstrings on the getter/setter methods and merge them for you, but most of the time this isn't necessary (and given that yard auto-generates default docstrings, it will generate extra unnecessary boiler plate documentation), which is why we recommend |
Yeah, with "documented" I actually meant "showing up in the generated docs", sorry. Thank you very much for the example and the explanation. However, I found another example
This will show My problem with the current behavior is that comments that might provide useful I don't know how difficult this would be to implement, but I'd give it a try and |
I tried to simplify the explanation a little, so I did end up cutting some corners in what actually happens. If you want the full rundown (it will be important to explain why merging won't work that well), it is this: When the default template displays an attribute, it looks for one of the two attribute methods in the pair-- either foo or foo= (in that order). It picks the first matching method and lists that docstring. It always only picks one of the two. The "attribute" concept is an abstraction that exists only in the template layer, not in the parser (white lie, we do have some things to keep track of attribute information, but there's no "AttributeObject" in the registry), so it's picking a method to represent the attribute in the template. Now, as you can imagine, this means that if "foo" exists as the first method in the method pair, the template will use the docs for that method as the attribute's docstring. What you're effectively doing in your example when redocumenting "def foo" after an attr_accessor, is attaching two docstrings to the same "foo" method. That's why you're seeing the docs for "foo", and not your attr declaration. In YARD, the last docstring always wins out, so your code is equivalent to overriding a regular method definition twice, and changing the docstring while doing it: # Comment on accessor
def foo; @foo end
# Comment on getter
def foo; something_else end You can think of documenting attr_accessors as attaching a docstring to both the foo and foo= methods, since that is what YARD does. In that sense, you shouldn't be documenting both the attr_* declaration and a subsequent method override, because they are the same methods. In essence, YARD is treating an attr_accessor as if it were expanded out to its true form of two method declarations and applying the docstring on those objects. You shouldn't think of an attr_* as a special entity in your domain, it's simply shorthand for defining two methods. Again, with that behaviour, documenting both declarations doesn't make sense. As for merging, there are problems:
class Test
# Documentation for writer, but template won't display this
attr_writer :foo
# Documentation for reader, YARD templates will pick this
# as the method to represent the "attribute" pair.
# IOW, you should put all docs relating to the "attribute" here.
def foo; something_with(@foo) end
end But after all this explanation, I should end with saying I think this is all beside the point-- because:
If you're just looking to have the writer be "recognized" and you're not looking for any extra docs, it already is recognized by virtue of being a readwrite attribute in the template. If there was no writer, YARD would add a "readonly" label to the attr. YARD will never show docs for the |
Closing. New docs now include recommendations on how to document attributes. More improvements to the docs are still appreciated! |
The problem is any comment that is one-line above attr_reader & attr_accessor will be considered as their document (via experiment). So `# class Benchmark::IPS::Job` & `# End of Entry` need to be removed. Or intentionlly put one more blank line. I think removal these is better. Currently there is no good way to document attr_accessor as I know of. On documenting attr_accessor please see: 1) http://stackoverflow.com/questions/7583166/yard-document-custom-getter-setter-pair-like-attr-accessor 2) lsegal/yard#516 So I just use `@attr` for now. Thanks for the help by @lsegal from lsegal/yard#787.
The problem is any comment that is one-line above attr_reader & attr_accessor will be considered as their document (via experiment). So `# class Benchmark::IPS::Job` & `# End of Entry` need to be removed. Or intentionlly put one more blank line. I think removal these is better. Currently there is no good way to document attr_accessor as I know of. On documenting attr_accessor please see: 1) http://stackoverflow.com/questions/7583166/yard-document-custom-getter-setter-pair-like-attr-accessor 2) lsegal/yard#516 So I just use `@attr` for now. Thanks for the help by @lsegal from lsegal/yard#787.
This is somehow a duplicate of the closed issue #158, but I feel like this should by fixed, as it is, IMO, pretty surprising, especially given the fact that the accessor is documented, but the setter is not, e.g.:
The following will result in both
foo=
andfoo
being documented:This will result in only
foo
being documented:As will this:
The creator of the closed issue #158 says to use
@overload
to workaround this behavior, but I haven't been able to do so (probably I'm using@overload
the wrong way).However, I believe that this behavior is pretty confusing and, as such, a caveat that could be removed.
The text was updated successfully, but these errors were encountered: