From 7f013c91607cc867806b29b316f576e2bc6be75c Mon Sep 17 00:00:00 2001 From: stjhimy Date: Mon, 25 Jul 2016 09:54:31 -0300 Subject: [PATCH 1/2] Use rack-protection from master on sinatra-contrib --- sinatra-contrib/Gemfile | 1 + 1 file changed, 1 insertion(+) diff --git a/sinatra-contrib/Gemfile b/sinatra-contrib/Gemfile index 27e6ba2d..11aa52d0 100644 --- a/sinatra-contrib/Gemfile +++ b/sinatra-contrib/Gemfile @@ -42,3 +42,4 @@ repos = { 'tilt' => 'rtomayko/tilt', 'rack' => 'rack/rack' } gem lib, dep if dep end +gem "rack-protection", github: "sinatra/rack-protection" From 26a7ba68d4c45b7d0ba8d2fa382d237a876bb8ce Mon Sep 17 00:00:00 2001 From: stjhimy Date: Mon, 25 Jul 2016 10:05:19 -0300 Subject: [PATCH 2/2] Add .after_reload to Sinatra::Reloader --- sinatra-contrib/lib/sinatra/reloader.rb | 16 +++++++++++++++- sinatra-contrib/spec/reloader_spec.rb | 25 +++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/sinatra-contrib/lib/sinatra/reloader.rb b/sinatra-contrib/lib/sinatra/reloader.rb index 7b84e786..9b12c6c9 100755 --- a/sinatra-contrib/lib/sinatra/reloader.rb +++ b/sinatra-contrib/lib/sinatra/reloader.rb @@ -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. diff --git a/sinatra-contrib/spec/reloader_spec.rb b/sinatra-contrib/spec/reloader_spec.rb index b296e2f2..4b5e2035 100644 --- a/sinatra-contrib/spec/reloader_spec.rb +++ b/sinatra-contrib/spec/reloader_spec.rb @@ -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