lib/sinatra.rb in sinatra-0.3.2 vs lib/sinatra.rb in sinatra-0.3.3

- old
+ new

@@ -9,10 +9,14 @@ elsif ENV['EVENT'] require 'swiftcore/evented_mongrel' puts "Using Evented Mongrel" end +unless defined?(Rack::File::MIME_TYPES) + class Rack::File; MIME_TYPES = Rack::Mime::MIME_TYPES; end +end + module Rack #:nodoc: class Request #:nodoc: # Set of request method names allowed via the _method parameter hack. By @@ -53,11 +57,11 @@ module Sinatra extend self - VERSION = '0.3.2' + VERSION = '0.3.3' class NotFound < RuntimeError def self.code ; 404 ; end end class ServerError < RuntimeError @@ -921,22 +925,23 @@ # created after the changes are made. For this reason, modifications to # the default_options Hash typically occur at the very beginning of a # file, before any DSL related functions are invoked. def self.default_options return @default_options unless @default_options.nil? - root = File.expand_path(File.dirname($0)) + app_file = locate_app_file + root = File.expand_path(File.dirname(app_file)) @default_options = { :run => true, :port => 4567, :host => '0.0.0.0', - :env => :development, + :env => (ENV['RACK_ENV'] || :development).to_sym, :root => root, :views => root + '/views', :public => root + '/public', :sessions => false, :logging => true, - :app_file => $0, + :app_file => app_file, :raise_errors => false } load_default_options_from_command_line! @default_options end @@ -955,10 +960,20 @@ op.on('-x') { default_options[:mutex] = true } op.on('-s server') { |server| default_options[:server] = server } end.parse!(ARGV.dup.select { |o| o !~ /--name/ }) end + # Use heuristics to locate the application file. + def self.locate_app_file + caller[1..-1].reverse.each do |path| + path = path.split(':', 2)[0] + next if path =~ /sinatra\.rb$/ || path == '(__DSL__)' + return path + end + $0 + end + # Determine whether the application is in the process of being # reloaded. def reloading? @reloading == true end @@ -1253,12 +1268,17 @@ context.reset! [:complete, context.instance_eval(&result.block)] end body = returned.to_result(context) rescue => e + msg = "#{e.class.name} - #{e.message}:" + msg << "\n #{e.backtrace.join("\n ")}" + request.env['rack.errors'] << msg + request.env['sinatra.error'] = e - context.status(500) + status = e.class.code rescue 500 + context.status(status) raise if options.raise_errors && e.class != NotFound result = (errors[e.class] || errors[ServerError]).invoke(request) returned = catch(:halt) do [:complete, context.instance_eval(&result.block)] @@ -1363,14 +1383,14 @@ def helpers(&b) Sinatra::EventContext.class_eval(&b) end def use_in_file_templates! - require 'stringio' - templates = IO.read(caller.first.split(':').first).split('__FILE__').last - data = StringIO.new(templates) + file = caller.first.sub(/:\d+$/, '') + data = IO.read(file).split('__END__').last + data.gsub! /\r\n/, "\n" current_template = nil - data.each do |line| + data.each_line do |line| if line =~ /^@@\s?(.*)/ current_template = $1.to_sym Sinatra.application.templates[current_template] = '' elsif current_template Sinatra.application.templates[current_template] << line