1
0
Fork 0
mirror of https://github.com/sinatra/sinatra synced 2023-03-27 23:18:01 -04:00

trigger route_added hooks for namespace-only extensions

This commit is contained in:
Konstantin Haase 2011-03-26 00:39:16 +01:00
parent 31cdac9d0e
commit fb488b9867
2 changed files with 55 additions and 4 deletions

View file

@ -54,6 +54,10 @@ module Sinatra
end
end
def invoke_hook(name, *args)
@extensions.each { |e| e.send(name, *args) if e.respond_to?(name) }
end
def errors
@errors ||= {}
end
@ -108,7 +112,14 @@ module Sinatra
def prefixed(method, pattern = nil, conditions = {}, &block)
default = '*' if method == :before or method == :after
pattern, conditions = compile pattern, conditions, default
base.send(method, pattern, conditions, &block)
result = base.send(method, pattern, conditions, &block)
invoke_hook :route_added, method.to_s.upcase, pattern, block
result
end
def method_missing(meth, *args, &block)
return super if args.any? or block or not base.respond_to? meth
base.send meth
end
end

View file

@ -496,9 +496,49 @@ describe Sinatra::Namespace do
end
describe 'extensions' do
it 'allows registering extensions for a namespace only'
it 'triggers route_added hook'
it 'prevents changing app global settings'
it 'allows read access to settings' do
value = nil
mock_app do
set :foo, 42
namespace '/foo' do
value = foo
end
end
value.should == 42
end
it 'allows registering extensions for a namespace only' do
a = b = nil
extension = Module.new { define_method(:views) { "CUSTOM!!!" } }
mock_app do
namespace '/' do
register extension
a = views
end
b = views
end
a.should == 'CUSTOM!!!'
b.should_not == 'CUSTOM!!!'
end
it 'triggers route_added hook' do
route = nil
extension = Module.new
extension.singleton_class.class_eval do
define_method(:route_added) { |*r| route = r }
end
mock_app do
namespace '/f' do
register extension
get('oo') { }
end
get('/bar') { }
end
route[1].should == '/foo'
end
it 'prevents changing app global settings' do
end
end
end
end