mirror of
https://github.com/sinatra/sinatra
synced 2023-03-27 23:18:01 -04:00
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
This commit is contained in:
parent
0f02bafe86
commit
8b8ddfef56
2 changed files with 60 additions and 0 deletions
|
|
@ -553,6 +553,7 @@ module Sinatra
|
||||||
@middleware = []
|
@middleware = []
|
||||||
@errors = {}
|
@errors = {}
|
||||||
@prototype = nil
|
@prototype = nil
|
||||||
|
@extensions = []
|
||||||
|
|
||||||
class << self
|
class << self
|
||||||
attr_accessor :routes, :filters, :conditions, :templates,
|
attr_accessor :routes, :filters, :conditions, :templates,
|
||||||
|
|
@ -698,10 +699,16 @@ module Sinatra
|
||||||
lambda { unbound_method.bind(self).call }
|
lambda { unbound_method.bind(self).call }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
invoke_hook(:route_added, verb, path)
|
||||||
|
|
||||||
(routes[verb] ||= []).
|
(routes[verb] ||= []).
|
||||||
push([pattern, keys, conditions, block]).last
|
push([pattern, keys, conditions, block]).last
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def invoke_hook(name, *args)
|
||||||
|
extensions.each { |e| e.send(name, *args) if e.respond_to?(name) }
|
||||||
|
end
|
||||||
|
|
||||||
def compile(path)
|
def compile(path)
|
||||||
keys = []
|
keys = []
|
||||||
if path.respond_to? :to_str
|
if path.respond_to? :to_str
|
||||||
|
|
@ -733,8 +740,13 @@ module Sinatra
|
||||||
include *extensions if extensions.any?
|
include *extensions if extensions.any?
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def extensions
|
||||||
|
(@extensions + (superclass.extensions rescue [])).uniq
|
||||||
|
end
|
||||||
|
|
||||||
def register(*extensions, &block)
|
def register(*extensions, &block)
|
||||||
extensions << Module.new(&block) if block_given?
|
extensions << Module.new(&block) if block_given?
|
||||||
|
@extensions += extensions
|
||||||
extensions.each do |extension|
|
extensions.each do |extension|
|
||||||
extend extension
|
extend extension
|
||||||
extension.registered(self) if extension.respond_to?(:registered)
|
extension.registered(self) if extension.respond_to?(:registered)
|
||||||
|
|
@ -817,6 +829,7 @@ module Sinatra
|
||||||
@errors = base.errors.dup
|
@errors = base.errors.dup
|
||||||
@middleware = base.middleware.dup
|
@middleware = base.middleware.dup
|
||||||
@prototype = nil
|
@prototype = nil
|
||||||
|
@extensions = []
|
||||||
end
|
end
|
||||||
|
|
||||||
protected
|
protected
|
||||||
|
|
|
||||||
47
test/route_added_hook_test.rb
Normal file
47
test/route_added_hook_test.rb
Normal file
|
|
@ -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
|
||||||
Loading…
Add table
Add a link
Reference in a new issue