Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added parsing support for "NOT_FOUND" error handler

  • Loading branch information...
commit 088c4afcc8d60844d000bc8d2c5c9e76179228fe 1 parent 4ec2769
Ryan Sobol authored
View
36 lib/yard/sinatra.rb
@@ -6,6 +6,10 @@ module Sinatra
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 @@ def self.routes
@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 @@ def register_route(verb, path, doc = nil)
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 @@ class RouteHandler < Ruby::Base
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
View
7 spec/example_app.rb
@@ -17,7 +17,12 @@ def settings(some_user)
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") { }
View
13 spec/yard/sinatra_spec.rb
@@ -18,4 +18,17 @@
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
Please sign in to comment.
Something went wrong with that request. Please try again.