From 46bdb7dcf842afdb3bce57a6753eb4d909b1225d Mon Sep 17 00:00:00 2001 From: Konstantin Haase Date: Sat, 5 Nov 2011 11:46:14 -0200 Subject: [PATCH 1/2] only extend main object for top level dsl --- lib/sinatra/main.rb | 4 +++- test/integration/app.rb | 16 +++++++++++++++- test/integration_test.rb | 8 ++++++-- 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/lib/sinatra/main.rb b/lib/sinatra/main.rb index fc6ac70dfc..1d1d9cd38b 100644 --- a/lib/sinatra/main.rb +++ b/lib/sinatra/main.rb @@ -25,4 +25,6 @@ class Application < Base at_exit { Application.run! if $!.nil? && Application.run? } end -include Sinatra::Delegator +# include would include the module in Object +# extend only extends the `main` object +extend Sinatra::Delegator diff --git a/test/integration/app.rb b/test/integration/app.rb index b13d6e99a6..8f98c55afa 100644 --- a/test/integration/app.rb +++ b/test/integration/app.rb @@ -1,6 +1,20 @@ require 'sinatra' +configure do + set :foo, :bar +end + get '/app_file' do content_type :txt settings.app_file -end \ No newline at end of file +end + +get '/mainonly' do + object = Object.new + begin + object.send(:get, '/foo') { } + 'false' + rescue NameError + 'true' + end +end diff --git a/test/integration_test.rb b/test/integration_test.rb index fab592c779..f3de1f9869 100644 --- a/test/integration_test.rb +++ b/test/integration_test.rb @@ -44,9 +44,13 @@ def with_server Process.kill("TERM", pipe.pid) if pipe end - it 'starts a top level application' do + def assert_content(url, content) with_server do - assert_equal open("http://127.0.0.1:#{port}/app_file").read, app_file + response = open("http://127.0.0.1:#{port}#{url}") + assert_equal response.read, content end end + + it('sets the app_file') { assert_content "/app_file", app_file } + it('only extends main') { assert_content "/mainonly", "true" } end From 367cb3ca30810d2a05e166e174716eb9ed8bb64e Mon Sep 17 00:00:00 2001 From: Konstantin Haase Date: Sat, 5 Nov 2011 11:52:12 -0200 Subject: [PATCH 2/2] adjust docs: we no longer extend all objects --- README.rdoc | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/README.rdoc b/README.rdoc index 7fd5314ebe..496ed44531 100644 --- a/README.rdoc +++ b/README.rdoc @@ -1513,11 +1513,11 @@ is recommended: Defining your app at the top-level works well for micro-apps but has considerable drawbacks when building reusable components such as Rack -middleware, Rails metal, simple libraries with a server component, or -even Sinatra extensions. The top-level DSL pollutes the Object namespace -and assumes a micro-app style configuration (e.g., a single application -file, ./public and ./views directories, logging, exception -detail page, etc.). That's where Sinatra::Base comes into play: +middleware, Rails metal, simple libraries with a server component, or even +Sinatra extensions. The top-level assumes a micro-app style configuration +(e.g., a single application file, ./public and ./views +directories, logging, exception detail page, etc.). That's where +Sinatra::Base comes into play: require 'sinatra/base' @@ -1549,15 +1549,10 @@ for details on available options and their behavior. Contrary to common belief, there is nothing wrong with classic style. If it suits your application, you do not have to switch to a modular application. -There are only two downsides compared with modular style: - -* You may only have one Sinatra application per Ruby process. If you plan to - use more, switch to modular style. - -* Classic style pollutes Object with delegator methods. If you plan to ship - your application in a library/gem, switch to modular style. - -There is no reason you cannot mix modular and classic style. +The main downsides of using classic style rather than modular style is that +you may only have one Sinatra application per Ruby process. If you plan to use +more than one, switch to modular style. There is no reason you cannot mix +modular and classic style. If switching from one style to the other, you should be aware of slightly different default settings: