mirror of
https://github.com/sinatra/sinatra
synced 2023-03-27 23:18:01 -04:00
filter reloading
This commit is contained in:
parent
8c55335397
commit
c562abc2c9
3 changed files with 70 additions and 0 deletions
|
@ -136,6 +136,11 @@ module Sinatra
|
||||||
# +ExtensionMethods+ and defines a before filter to +perform+ the
|
# +ExtensionMethods+ and defines a before filter to +perform+ the
|
||||||
# reload of the modified file.
|
# reload of the modified file.
|
||||||
def self.registered(klass)
|
def self.registered(klass)
|
||||||
|
@reloader_loaded_in ||= {}
|
||||||
|
return if @reloader_loaded_in[klass]
|
||||||
|
|
||||||
|
@reloader_loaded_in[klass] = true
|
||||||
|
|
||||||
klass.extend BaseMethods
|
klass.extend BaseMethods
|
||||||
klass.extend ExtensionMethods
|
klass.extend ExtensionMethods
|
||||||
klass.set(:reloader) { klass.development? }
|
klass.set(:reloader) { klass.development? }
|
||||||
|
@ -205,6 +210,16 @@ module Sinatra
|
||||||
))
|
))
|
||||||
super
|
super
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def add_filter(type, path = nil, options = {}, &block)
|
||||||
|
source_location = block.respond_to?(:source_location) ?
|
||||||
|
block.source_location.first : caller_files[1]
|
||||||
|
result = super
|
||||||
|
Watcher::List.for(self).watch(source_location, Watcher::Element.new(
|
||||||
|
:"#{type}_filter", filters[type].last
|
||||||
|
))
|
||||||
|
result
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Contains the methods that the extension adds to the Sinatra
|
# Contains the methods that the extension adds to the Sinatra
|
||||||
|
@ -219,6 +234,10 @@ module Sinatra
|
||||||
(routes[verb] ||= []).delete(signature)
|
(routes[verb] ||= []).delete(signature)
|
||||||
when :middleware then
|
when :middleware then
|
||||||
@middleware.delete(element.representation)
|
@middleware.delete(element.representation)
|
||||||
|
when :before_filter then
|
||||||
|
filters[:before].delete(element.representation)
|
||||||
|
when :after_filter then
|
||||||
|
filters[:after].delete(element.representation)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -9,6 +9,10 @@ class <%= name %> < Sinatra::Base
|
||||||
use <%= middleware %>
|
use <%= middleware %>
|
||||||
<% end %>
|
<% end %>
|
||||||
|
|
||||||
|
<% filters.each do |filter| %>
|
||||||
|
<%= filter %>
|
||||||
|
<% end %>
|
||||||
|
|
||||||
<% routes.each do |route| %>
|
<% routes.each do |route| %>
|
||||||
<%= route %>
|
<%= route %>
|
||||||
<% end %>
|
<% end %>
|
||||||
|
|
|
@ -41,6 +41,7 @@ describe Sinatra::Reloader do
|
||||||
options[:routes] ||= ['get("/foo") { erb :foo }']
|
options[:routes] ||= ['get("/foo") { erb :foo }']
|
||||||
options[:inline_templates] ||= nil
|
options[:inline_templates] ||= nil
|
||||||
options[:middlewares] ||= []
|
options[:middlewares] ||= []
|
||||||
|
options[:filters] ||= []
|
||||||
options[:name] ||= app_name
|
options[:name] ||= app_name
|
||||||
|
|
||||||
update_file(app_file_path) do |f|
|
update_file(app_file_path) do |f|
|
||||||
|
@ -171,6 +172,52 @@ describe Sinatra::Reloader do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe "default filter reloading mechanism" do
|
||||||
|
it "knows when a before filter has been added" do
|
||||||
|
setup_example_app(:routes => ['get("/foo") { "foo" }'])
|
||||||
|
expect {
|
||||||
|
update_app_file(
|
||||||
|
:routes => ['get("/foo") { "foo" }'],
|
||||||
|
:filters => ['before { @hi = "hi" }']
|
||||||
|
)
|
||||||
|
get('/foo') # ...to perform the reload
|
||||||
|
}.to change { app_const.filters[:before].size }.by(1)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "knows when an after filter has been added" do
|
||||||
|
setup_example_app(:routes => ['get("/foo") { "foo" }'])
|
||||||
|
expect {
|
||||||
|
update_app_file(
|
||||||
|
:routes => ['get("/foo") { "foo" }'],
|
||||||
|
:filters => ['after { @bye = "bye" }']
|
||||||
|
)
|
||||||
|
get('/foo') # ...to perform the reload
|
||||||
|
}.to change { app_const.filters[:after].size }.by(1)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "knows when a before filter has been removed" do
|
||||||
|
setup_example_app(
|
||||||
|
:routes => ['get("/foo") { "foo" }'],
|
||||||
|
:filters => ['before { @hi = "hi" }']
|
||||||
|
)
|
||||||
|
expect {
|
||||||
|
update_app_file(:routes => ['get("/foo") { "foo" }'])
|
||||||
|
get('/foo') # ...to perform the reload
|
||||||
|
}.to change { app_const.filters[:before].size }.by(-1)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "knows when an after filter has been removed" do
|
||||||
|
setup_example_app(
|
||||||
|
:routes => ['get("/foo") { "foo" }'],
|
||||||
|
:filters => ['after { @bye = "bye" }']
|
||||||
|
)
|
||||||
|
expect {
|
||||||
|
update_app_file(:routes => ['get("/foo") { "foo" }'])
|
||||||
|
get('/foo') # ...to perform the reload
|
||||||
|
}.to change { app_const.filters[:after].size }.by(-1)
|
||||||
|
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