Created
December 20, 2010 04:55
-
-
Save lsegal/748033 to your computer and use it in GitHub Desktop.
Method class extensions to introspect YARD docs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'yard' | |
class Method | |
def docstring | |
@yard_cache ||= {} | |
file = source_location[0] | |
unless @yard_cache[file] | |
YARD.parse(file) | |
@yard_cache[file] = true | |
end | |
if owner.name # instance method | |
klassinst, klass = '#', owner.name | |
else # class method | |
klassinst, klass = '.', owner.to_s[/#<.+?:(.+?)>/, 1] | |
end | |
if obj = YARD::Registry.at([klass, name].join(klassinst)) | |
obj.docstring | |
end | |
end | |
def examples | |
docstring.tags(:example).map {|t| t.text } | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'yard/method_ext' | |
module A | |
class Foo | |
# Do class method stuff | |
def self.bar | |
puts "CLASS METHOD" | |
end | |
# Do instance method stuff | |
# | |
# @example | |
# Foo.new.bar | |
# | |
# @return [void] | |
def bar | |
10.times do |i| | |
print i | |
end | |
end | |
end | |
end | |
p A::Foo.method(:bar).docstring | |
foo = A::Foo.new | |
p foo.method(:bar).docstring | |
p foo.method(:bar).examples | |
__END__ | |
"Do class method stuff" | |
"Do instance method stuff" | |
["Foo.new.bar"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment