Created
May 30, 2009 08:08
-
-
Save lsegal/120429 to your computer and use it in GitHub Desktop.
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
<div class="section specs"> | |
<h3>Specifications</h3> | |
<ul> | |
<% for spec in object[:specifications] %> | |
<li> | |
<%= spec[:name] %> | |
<span>(<a class="source_link" href="#">View source</a>)</span> | |
<div class="source_code"> | |
<table> | |
<tr> | |
<td> | |
<pre class="lines"> | |
<%= h format_lines(object) %></pre> | |
</td> | |
<td> | |
<pre class="code"><span class="info file"># File '<%= h spec[:file] %>', line <%= spec[:line] %></span> | |
<%= html_syntax_highlight spec[:source] %></pre> | |
</td> | |
</tr> | |
</table> | |
</div> | |
</li> | |
<% end %> | |
</ul> | |
</div> |
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 RSpecDescribeHandler < YARD::Handlers::Ruby::Base | |
handles method_call(:describe) | |
def process | |
name = statement.parameters.first.source | |
if statement.parameters[1] | |
src = statement.parameters[1].jump(:string_content).source | |
name += (src[0] == "#" ? "" : "::") + src | |
end | |
object = P(name) | |
ensure_loaded! object | |
object[:specifications] ||= [] | |
parse_block(statement.last.last, owner: object) | |
rescue YARD::Handlers::NamespaceMissingError | |
end | |
end | |
class RSpecItHandler < YARD::Handlers::Ruby::Base | |
handles method_call(:it) | |
def process | |
owner[:specifications] << {}.tap do |o| | |
source = statement.source.chomp | |
indent = source.split(/\r?\n/).last[/^([ \t]*)/, 1].length | |
o[:source] = source.gsub(/^[ \t]{#{indent}}/, '') | |
o[:name] = statement.parameters.first.jump(:string_content).source | |
o[:file] = statement.file | |
o[:line] = statement.line | |
end | |
end | |
end | |
module YARD | |
module Generators | |
class MethodGenerator | |
before_section :specs, :has_specs? | |
def sections_for(object) | |
[:header, [:title, [G(MethodSignatureGenerator), :aliases], | |
G(DeprecatedGenerator), :specs, G(DocstringGenerator), | |
G(TagsGenerator), G(SourceGenerator)]] | |
end | |
def has_specs?(object) | |
object[:specifications] && object[:specifications].size > 0 ? true : false | |
end | |
end | |
end | |
end | |
YARD::Generators::Base.register_template_path File.dirname(__FILE__) + '/../templates/' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment