mirror of
https://github.com/sinatra/sinatra
synced 2023-03-27 23:18:01 -04:00
* loader is working!
This commit is contained in:
parent
939f30ace0
commit
2333cf2515
5 changed files with 54 additions and 29 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
29
lib/sinatra/loader.rb
Normal file
29
lib/sinatra/loader.rb
Normal file
|
@ -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
|
Loading…
Reference in a new issue