1

FIRST, I would like to customize RDoc such that it automatically recognizes each key of the attrs hash foo and bar of the following code:

class SomeClass
  def initialize( args = { }) 
    attrs = { 'foo'  => nil,
              'bar'    => 'us'}
    attrs.each_key{ |key| (class << self; self; end).send( :attr_accessor, key.to_sym)}

    attrs.each_key do |key|
      attrs[ key] = args[ key] if( args.has_key?( key))
      raise "No #{key} defined" if( attrs[key] === nil))
    end

    attrs.each {|key, value| instance_variable_set( "@#{key}", value)}
  end
end

As if I had initialized them as follows:

class SomeClass
  attr_accessor :foo, :bar
  def initialize( foo = nil, bar = 'us') 
      raise "No foo defined" if( foo === nil))
      @foo = foo
      @bar = bar
  end
end

I am able to do the following and have the accessors show up properly as Attributes:

class SomeClass
  ##
  # :attr_accessor: foo

  ##
  # :attr_accessor: bar

  ##
  # this is a comment for a the initialize method

  def initialize( args = { }) 
    attrs = { 'foo'  => nil,
              'bar'    => 'us'}
    attrs.each_key{ |key| (class << self; self; end).send( :attr_accessor, key.to_sym)}

    attrs.each_key do |key|
      attrs[ key] = args[ key] if( args.has_key?( key))
      raise "No #{key} defined" if( attrs[key] === nil))
    end

    attrs.each {|key, value| instance_variable_set( "@#{key}", value)}
  end
end

But I would like to be able to have RDoc recognize these automatically!

SECOND, I would like to be able to make comment relevant to their definitions in a manor similar to the following and have the comments show up in the documentation:

class SomeClass
  def initialize( args = { }) 
    attrs = { 
              ##
              #This stores your Foo
              'foo'  => nil,
              ##
              #This stores your Bar
              'bar'    => 'us'}
    attrs.each_key{ |key| (class << self; self; end).send( :attr_accessor, key.to_sym)}

    attrs.each_key do |key|
      attrs[ key] = args[ key] if( args.has_key?( key))
      raise "No #{key} defined" if( attrs[key] === nil))
    end

    attrs.each {|key, value| instance_variable_set( "@#{key}", value)}
  end
end

LAST, I would like their default values automatically listed as well (i.e. within the documentation it would list with the attributes that by default foo = nil and bar = 'us').

I have been examining the RDoc documentation for the Ruby code parser RDoc::Parser::Ruby and I would like to build a plugin as described in the RDoc Developer Introduction but I am unsure of how to even begin.

1) Is there something out there which already accomplishes what I have described? 2) If nothing exists, could someone please point me to an example plugin accomplishing similar functionality?

rudolph9
  • 8,021
  • 9
  • 50
  • 80
  • I'm not sure you can do this. Maybe instead of trying `self.send(...)` you _might_ be able to use `attr_*(key)`, if RDoc is smart enough for that. – Jwosty Mar 22 '12 at 16:27
  • The code functions exactly as I want it to, what I need is dynamically generated documentation. I'm looking for a way to configure RDoc such that it recognizes each of my attr_*s objects in my initialize method (i.e. `attr_writers` `attr_readers` `attr_accessors`) and simply puts them into my documentation noting their default values. – rudolph9 Mar 22 '12 at 16:34
  • Hmm, I don't think RDoc has those capabilities. But it probably wouldn't be too hard to build one on top of it yourself. – Jwosty Mar 22 '12 at 17:06
  • 1
    [yard](http://yardoc.org) can do that. – Catherine Mar 22 '12 at 20:08
  • @whitequark How?? I've been reading through `YARD` content for a few hours but I'm unable to identify exactly how to go about doing this? Could you point me to an example doing something similar? – rudolph9 Mar 22 '12 at 23:16
  • @KurtRudolph, wait, you want RDoc to parse some hash in your code for you and extract method names from it? That's impossible. Doing this would require just running your code, which a documentation tool shouldn't and won't do. – Catherine Mar 23 '12 at 01:20
  • On the other hand, you can make some DSL to do the same task and then use [YARD macros](http://rubydoc.info/docs/yard/file/docs/Tags.md#Macros) to attach documentation to these definitions. What you are doing is pretty dirty anyway and would be much better done with a DSL. – Catherine Mar 23 '12 at 01:21
  • @whitequark Why is what I am doing dirty? My ultimate goal is to be able to define `attr_*`s without having to write anything twice, documentation included. How would I accomplish this in a DSL? – rudolph9 Mar 23 '12 at 01:30

1 Answers1