-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Hook mechanizm for route_added [help from rtomayko]
Example: module Sinatra module RouteAddedExtSample def self.route_added(verb, path) p [verb, path] end end register RouteAddedExtSample end post '/' do do_something 'ok' end Output: ["POST", "/"] move superclass logic into extensions attr reader
- Loading branch information
Showing
2 changed files
with
60 additions
and
0 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
require File.dirname(__FILE__) + '/helper' | ||
|
||
module RouteAddedTest | ||
@routes = [] | ||
def self.routes ; @routes ; end | ||
def self.route_added(verb, path) | ||
@routes << [verb, path] | ||
end | ||
end | ||
|
||
describe "route_added Hook" do | ||
|
||
before { RouteAddedTest.routes.clear } | ||
|
||
it "should be notified of an added route" do | ||
mock_app(Class.new(Sinatra::Base)) { | ||
register RouteAddedTest | ||
get('/') {} | ||
} | ||
|
||
assert_equal [["GET", "/"], ["HEAD", "/"]], | ||
RouteAddedTest.routes | ||
end | ||
|
||
it "should include hooks from superclass" do | ||
a = Class.new(Class.new(Sinatra::Base)) | ||
b = Class.new(a) | ||
|
||
a.register RouteAddedTest | ||
b.class_eval { post("/sub_app_route") {} } | ||
|
||
assert_equal [["POST", "/sub_app_route"]], | ||
RouteAddedTest.routes | ||
end | ||
|
||
it "should only run once per extension" do | ||
mock_app(Class.new(Sinatra::Base)) { | ||
register RouteAddedTest | ||
register RouteAddedTest | ||
get('/') {} | ||
} | ||
|
||
assert_equal [["GET", "/"], ["HEAD", "/"]], | ||
RouteAddedTest.routes | ||
end | ||
|
||
end |
8b8ddfe
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am curious. Where are you going with this? Doc ala jgregario? :)
http://bitworking.org/news/201/RESTify-DayTrader