From db761b8b07fbb9beac545e2779f90d254197deb4 Mon Sep 17 00:00:00 2001 From: Konstantin Haase Date: Tue, 3 May 2011 20:17:02 +0200 Subject: [PATCH] let app_file default to the file subclassing Sinatra::Base in modular apps --- CHANGES | 3 +++ README.rdoc | 2 +- lib/sinatra/base.rb | 2 ++ test/settings_test.rb | 14 +++++++++----- 4 files changed, 15 insertions(+), 6 deletions(-) diff --git a/CHANGES b/CHANGES index 7735fc63..e076a320 100644 --- a/CHANGES +++ b/CHANGES @@ -10,6 +10,9 @@ explicitly to prevent that behavior. (Magnus Holm, Ryan Tomayko, Konstantin Haase) + * `settings.app_file` now defaults to the file subclassing `Sinatra::Base` in + modular applications. (Konstantin Haase) + * Set up `Rack::Logger` or `Rack::NullLogger` depending on whether logging was enabled or not. Also, expose that logger with the `logger` helper method. (Konstantin Haase) diff --git a/README.rdoc b/README.rdoc index 0b72138a..caa2e665 100644 --- a/README.rdoc +++ b/README.rdoc @@ -1382,7 +1382,7 @@ different default settings: Setting Classic Modular - app_file file loading sinatra nil + app_file file loading sinatra file subclassing Sinatra::Base run $0 == app_file false logging true false method_override true false diff --git a/lib/sinatra/base.rb b/lib/sinatra/base.rb index 95c140e2..408fd814 100644 --- a/lib/sinatra/base.rb +++ b/lib/sinatra/base.rb @@ -1310,6 +1310,7 @@ module Sinatra def inherited(subclass) subclass.reset! + subclass.set :app_file, caller_files.first unless subclass.app_file? super end @@ -1471,6 +1472,7 @@ module Sinatra set :method_override, true set :run, Proc.new { ! test? } set :session_secret, Proc.new { super() unless development? } + set :app_file, nil def self.register(*extensions, &block) #:nodoc: added_methods = extensions.map {|m| m.public_instance_methods }.flatten diff --git a/test/settings_test.rb b/test/settings_test.rb index 71ceb849..32daf79a 100644 --- a/test/settings_test.rb +++ b/test/settings_test.rb @@ -3,10 +3,10 @@ require File.dirname(__FILE__) + '/helper' class SettingsTest < Test::Unit::TestCase setup do @base = Sinatra.new(Sinatra::Base) - @base.set :environment, :foo + @base.set :environment => :foo, :app_file => nil @application = Sinatra.new(Sinatra::Application) - @application.set :environment, :foo + @application.set :environment => :foo, :app_file => nil end it 'sets settings to literal values' do @@ -358,9 +358,13 @@ class SettingsTest < Test::Unit::TestCase end describe 'app_file' do - it 'is nil' do - assert_nil @base.app_file - assert_nil @application.app_file + it 'is nil for base classes' do + assert_nil Sinatra::Base.app_file + assert_nil Sinatra::Application.app_file + end + + it 'defaults to the file subclassing' do + assert_equal __FILE__, Sinatra.new.app_file end end