hoodwink.d enhanced

Fri Nov 24

Extract information for a given class from RDoc

require 'rubygems' require 'rake' require 'rdoc/rdoc' module RDoc # Dives into the innards of RDoc and gives you # access to things like the docs for a given # class. class CodeInfo class << self def parse(pattern = nil) @info_for_corpus = parse_files(pattern) end def for(constant) new(constant).info end def info_for_corpus @info_for_corpus or raise RuntimeError end def files_to_parse @files_to_parse ||= Rake::FileList.new end private def parse_files(pattern) files = pattern ? Rake::FileList[pattern] : files_to_parse files << '-q' # Silence rdoc options = Options.instance options.parse(files, RDoc::GENERATORS) rdoc.send(:parse_files, options) end def rdoc TopLevel.reset rdoc = RDoc.new stats = Stats.new # We don't want any output so we'll # override the print method stats.instance_eval do def print; nil end end rdoc.instance_variable_set(:@stats, stats) rdoc end end attr_reader :info def initialize(constant) @constant = constant find end private attr_reader :constant def find parts = constant.split('::') self.class.info_for_corpus.each do |i| @info = parts.inject(i) do |result, part| (result.find_module_named(part) || result.find_class_named(part)) || break end return if info end end end end if __FILE__ == $0 RDoc::CodeInfo.parse(__FILE__) info = RDoc::CodeInfo.for('RDoc::CodeInfo') puts info.comment end % ruby code_info.rb # Dives into the innards of RDoc and gives you # access to things like the rdoc for a given # class.