diff --git a/examples/hello/test.rb b/examples/hello/test.rb index bbc0c461..a0802ba3 100644 --- a/examples/hello/test.rb +++ b/examples/hello/test.rb @@ -1,13 +1,13 @@ $LOAD_PATH.unshift File.dirname(__FILE__) + '/../../lib/' require 'sinatra' -# after_attend :log_fun_stuff -# -# helpers do -# def log_fun_stuff -# logger.debug "THIS IS COOL!" -# end -# end +after_attend :log_fun_stuff + +helpers do + def log_fun_stuff + logger.debug "THIS IS COOL!" + end +end get '/' do body <<-HTML diff --git a/lib/sinatra.rb b/lib/sinatra.rb index cb7e514e..b26e0b40 100644 --- a/lib/sinatra.rb +++ b/lib/sinatra.rb @@ -6,14 +6,13 @@ end end -require File.dirname(__FILE__) + '/sinatra/core_ext/class' -require File.dirname(__FILE__) + '/sinatra/core_ext/hash' +SINATRA_ROOT = File.dirname(__FILE__) + '/..' -require File.dirname(__FILE__) + '/sinatra/logger' -require File.dirname(__FILE__) + '/sinatra/event' -require File.dirname(__FILE__) + '/sinatra/dispatcher' -require File.dirname(__FILE__) + '/sinatra/server' -require File.dirname(__FILE__) + '/sinatra/dsl' +require File.dirname(__FILE__) + '/sinatra/loader' + +Sinatra::Loader.load_files Dir.glob(SINATRA_ROOT + '/lib/sinatra/core_ext/*.rb') +Sinatra::Loader.load_files Dir.glob(SINATRA_ROOT + '/lib/sinatra/*.rb') +Sinatra::Loader.load_files Dir.glob(SINATRA_ROOT + '/vendor/*/init.rb') SINATRA_LOGGER = Sinatra::Logger.new(STDOUT) @@ -25,12 +24,6 @@ end set_logger -SINATRA_ROOT = File.dirname(__FILE__) + '/..' - -Dir.glob(SINATRA_ROOT + '/vendor/*/init.rb').each do |plugin| - require plugin -end - at_exit do Sinatra::Server.new.start unless Sinatra::Server.running end diff --git a/lib/sinatra/dispatcher.rb b/lib/sinatra/dispatcher.rb index 3f9e996b..f8c0e8b2 100644 --- a/lib/sinatra/dispatcher.rb +++ b/lib/sinatra/dispatcher.rb @@ -1,16 +1,16 @@ module Sinatra - - DEFAULT_HEADERS = { 'Content-Type' => 'text/html' } - + class Dispatcher cattr_accessor :logger - def headers - DEFAULT_HEADERS + def default_headers + { 'Content-Type' => 'text/html' } end def call(env) + Loader.reload! + @request = Rack::Request.new(env) event = EventManager.determine_event( @@ -19,7 +19,7 @@ module Sinatra ) result = event.attend(@request) - [result.status, headers.merge(result.headers), result.body] + [result.status, default_headers.merge(result.headers), result.body] rescue => e logger.exception e end diff --git a/lib/sinatra/event.rb b/lib/sinatra/event.rb index 27b8dd35..81eebdd3 100644 --- a/lib/sinatra/event.rb +++ b/lib/sinatra/event.rb @@ -1,9 +1,12 @@ module Sinatra module EventManager - extend self + def reset! + @events.clear + end + def events @events || [] end @@ -110,10 +113,9 @@ module Sinatra end def attend(request) + context = EventContext.new(request) begin - context = EventContext.new(request) context.instance_eval(&@block) if @block - context rescue => e context.error e end @@ -127,6 +129,7 @@ module Sinatra def run_through_after_filters(context) after_filters.each { |filter| context.send(filter) } end + end end diff --git a/lib/sinatra/loader.rb b/lib/sinatra/loader.rb new file mode 100644 index 00000000..e7f65381 --- /dev/null +++ b/lib/sinatra/loader.rb @@ -0,0 +1,29 @@ +require 'set' + +module Sinatra + module Loader + extend self + + def reload! + EventManager.reset! + load_files loaded_files + load $0 + end + + def load_files(*files) + files = files.flatten + files = files.first if files.first.is_a? Set + + files.each do |file| + file = File.expand_path(file) + load file + loaded_files << file + end + end + alias_method :load_file, :load_files + + def loaded_files + @loaded_files ||= Set.new + end + end +end