Added parsing support for "NOT_FOUND" error handler

This commit is contained in:
Ryan Sobol 2010-06-24 17:49:54 -05:00
Родитель 4ec276995f
Коммит 088c4afcc8
3 изменённых файлов: 52 добавлений и 4 удалений

Просмотреть файл

@ -6,6 +6,10 @@ module YARD
def self.routes
YARD::Handlers::Sinatra::AbstractRouteHandler.routes
end
def self.error_handlers
YARD::Handlers::Sinatra::AbstractRouteHandler.error_handlers
end
end
module CodeObjects
@ -37,10 +41,19 @@ module YARD
@routes ||= []
end
def self.error_handlers
@error_handlers ||= []
end
def process
path = http_path
path = $1 if path =~ /^"(.*)"$/
register_route(http_verb, path)
case http_verb
when 'NOT_FOUND'
register_error_handler(http_verb)
else
path = http_path
path = $1 if path =~ /^"(.*)"$/
register_route(http_verb, path)
end
end
def register_route(verb, path, doc = nil)
@ -62,6 +75,22 @@ module YARD
AbstractRouteHandler.routes << route
yield(route) if block_given?
end
def register_error_handler(verb, doc = nil)
error_handler = register CodeObjects::RouteObject.new(namespace, verb, :instance) do |o|
o.visibility = "public"
o.source = statement.source
o.signature = verb
o.explicit = true
o.scope = scope
o.docstring = statement.comments
o.http_verb = verb
o.real_name = verb
o.add_file(parser.file, statement.line)
end
AbstractRouteHandler.error_handlers << error_handler
yield(error_handler) if block_given?
end
end
# Route handler for YARD's source parser.
@ -73,6 +102,7 @@ module YARD
handles method_call(:put)
handles method_call(:delete)
handles method_call(:head)
handles method_call(:not_found)
def http_verb
statement.method_name(true).to_s.upcase

Просмотреть файл

@ -17,7 +17,12 @@ class ExampleApp < Sinatra::Base
get "/settings" do
haml :settings, {}, :settings => settings(current_user)
end
# Error 404 Page Not Found
not_found do
haml :'404'
end
put("/settings") { }
delete("/settings") { }
post("/settings") { }

Просмотреть файл

@ -18,4 +18,17 @@ describe YARD::Sinatra do
route.docstring.should =~ /Displays a settings page for the current user/ if route.http_verb == "GET"
end
end
it "reads sinatra error handlers" do
YARD::Sinatra.error_handlers.size.should == 1
end
it "sets error handlers correctly" do
YARD::Sinatra.error_handlers.each do |error_handler|
%w[NOT_FOUND].should include(error_handler.http_verb)
error_handler.file.should =~ /example_app\.rb$/
error_handler.docstring.should =~ /Error 404 Page Not Found/ if error_handler.http_verb == "NOT_FOUND"
end
end
end