mirror of
https://github.com/sinatra/sinatra
synced 2023-03-27 23:18:01 -04:00
middleware reloading
This commit is contained in:
parent
b0cb2c45a5
commit
eecfe3dc97
3 changed files with 40 additions and 0 deletions
|
@ -194,6 +194,17 @@ module Sinatra
|
||||||
Watcher::List.for(self).watch_inline_templates(file)
|
Watcher::List.for(self).watch_inline_templates(file)
|
||||||
super
|
super
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
# Contains the methods that the extension adds to the Sinatra
|
# Contains the methods that the extension adds to the Sinatra
|
||||||
|
@ -206,6 +217,8 @@ module Sinatra
|
||||||
verb = element.representation[:verb]
|
verb = element.representation[:verb]
|
||||||
signature = element.representation[:signature]
|
signature = element.representation[:signature]
|
||||||
(routes[verb] ||= []).delete(signature)
|
(routes[verb] ||= []).delete(signature)
|
||||||
|
when :middleware then
|
||||||
|
@middleware.delete(element.representation)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -4,6 +4,10 @@ class <%= name %> < Sinatra::Base
|
||||||
enable :inline_templates
|
enable :inline_templates
|
||||||
<% end %>
|
<% end %>
|
||||||
|
|
||||||
|
<% middlewares.each do |middleware| %>
|
||||||
|
use <%= middleware %>
|
||||||
|
<% end %>
|
||||||
|
|
||||||
<% routes.each do |route| %>
|
<% routes.each do |route| %>
|
||||||
<%= route %>
|
<%= route %>
|
||||||
<% end %>
|
<% end %>
|
||||||
|
|
|
@ -40,6 +40,7 @@ describe Sinatra::Reloader do
|
||||||
def write_app_file(options={})
|
def write_app_file(options={})
|
||||||
options[:routes] ||= ['get("/foo") { erb :foo }']
|
options[:routes] ||= ['get("/foo") { erb :foo }']
|
||||||
options[:inline_templates] ||= nil
|
options[:inline_templates] ||= nil
|
||||||
|
options[:middlewares] ||= []
|
||||||
options[:name] ||= app_name
|
options[:name] ||= app_name
|
||||||
|
|
||||||
update_file(app_file_path) do |f|
|
update_file(app_file_path) do |f|
|
||||||
|
@ -148,6 +149,28 @@ describe Sinatra::Reloader do
|
||||||
end
|
end
|
||||||
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
|
describe ".dont_reload" do
|
||||||
before(:each) do
|
before(:each) do
|
||||||
setup_example_app(
|
setup_example_app(
|
||||||
|
|
Loading…
Reference in a new issue