Search code examples
rubydocumentationyard

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_other_attribute within "Instance Attribute Summary"?


Solution

  • 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.