middleware reloading

This commit is contained in:
Gabriel Andretta 2011-06-12 14:24:43 -03:00
parent b0cb2c45a5
commit eecfe3dc97
3 changed files with 40 additions and 0 deletions

View File

@ -194,6 +194,17 @@ module Sinatra
Watcher::List.for(self).watch_inline_templates(file)
super
end
# Does everything Sinatra::Base#use does, but it also tells the
# +Watcher::List+ for the Sinatra application to watch the
# middleware beign used.
def use(middleware, *args, &block)
path = caller_files[1] || File.expand_path($0)
Watcher::List.for(self).watch(path, Watcher::Element.new(
:middleware, [middleware, args, block]
))
super
end
end
# Contains the methods that the extension adds to the Sinatra
@ -206,6 +217,8 @@ module Sinatra
verb = element.representation[:verb]
signature = element.representation[:signature]
(routes[verb] ||= []).delete(signature)
when :middleware then
@middleware.delete(element.representation)
end
end

View File

@ -4,6 +4,10 @@ class <%= name %> < Sinatra::Base
enable :inline_templates
<% end %>
<% middlewares.each do |middleware| %>
use <%= middleware %>
<% end %>
<% routes.each do |route| %>
<%= route %>
<% end %>

View File

@ -40,6 +40,7 @@ describe Sinatra::Reloader do
def write_app_file(options={})
options[:routes] ||= ['get("/foo") { erb :foo }']
options[:inline_templates] ||= nil
options[:middlewares] ||= []
options[:name] ||= app_name
update_file(app_file_path) do |f|
@ -148,6 +149,28 @@ describe Sinatra::Reloader do
end
end
describe "default middleware reloading mechanism" do
it "knows when a middleware has been added" do
setup_example_app(:routes => ['get("/foo") { "foo" }'])
update_app_file(
:routes => ['get("/foo") { "foo" }'],
:middlewares => [Rack::Head]
)
get('/foo') # ...to perform the reload
app_const.middleware.should_not be_empty
end
it "knows when a middleware has been removed" do
setup_example_app(
:routes => ['get("/foo") { "foo" }'],
:middlewares => [Rack::Head]
)
update_app_file(:routes => ['get("/foo") { "foo" }'])
get('/foo') # ...to perform the reload
app_const.middleware.should be_empty
end
end
describe ".dont_reload" do
before(:each) do
setup_example_app(