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

Add .after_reload to Sinatra::Reloader

This commit is contained in:
stjhimy 2016-07-25 10:05:19 -03:00
parent 7f013c9160
commit 26a7ba68d4
2 changed files with 40 additions and 1 deletions

View file

@ -55,7 +55,8 @@ module Sinatra
#
# You can refine the reloading policy with +also_reload+ and
# +dont_reload+, to customize which files should, and should not, be
# reloaded, respectively.
# reloaded, respectively. You can also use +after_reload+ to execute a
# block after any file being reloaded.
#
# === Classic Application
#
@ -66,6 +67,9 @@ module Sinatra
#
# also_reload '/path/to/some/file'
# dont_reload '/path/to/other/file'
# after_reload do
# puts 'reloaded'
# end
#
# # Your classic application code goes here...
#
@ -81,6 +85,9 @@ module Sinatra
# register Sinatra::Reloader
# also_reload '/path/to/some/file'
# dont_reload '/path/to/other/file'
# after_reload do
# puts 'reloaded'
# end
# end
#
# # Your modular application code goes here...
@ -205,6 +212,12 @@ module Sinatra
MUTEX_FOR_PERFORM = Mutex.new
# Allow a block to be executed after any file being reloaded
@@after_reload = []
def after_reload(&block)
@@after_reload << block
end
# When the extension is registered it extends the Sinatra application
# +klass+ with the modules +BaseMethods+ and +ExtensionMethods+ and
# defines a before filter to +perform+ the reload of the modified files.
@ -236,6 +249,7 @@ module Sinatra
require watcher.path
watcher.update
end
@@after_reload.each(&:call)
end
# Contains the methods defined in Sinatra::Base that are overridden.

View file

@ -416,6 +416,31 @@ describe Sinatra::Reloader do
end
end
describe ".after_reload" do
before(:each) do
setup_example_app(:routes => ['get("/foo") { Foo.foo }'])
@foo_path = File.join(tmp_dir, 'foo.rb')
update_file(@foo_path) do |f|
f.write 'class Foo; def self.foo() "foo" end end'
end
$LOADED_FEATURES.delete @foo_path
require @foo_path
app_const.also_reload @foo_path
end
it "allows block execution after reloading files" do
app_const.after_reload do
$reloaded = true
end
expect($reloaded).to eq(nil)
expect(get('/foo').body.strip).to eq('foo')
update_file(@foo_path) do |f|
f.write 'class Foo; def self.foo() "bar" end end'
end
expect($reloaded).to eq(true)
end
end
it "automatically registers the reloader in the subclasses" do
class ::Parent < Sinatra::Base
register Sinatra::Reloader