Skip to content

Commit

Permalink
Cleanup and refactoring after inheritance changes
Browse files Browse the repository at this point in the history
  • Loading branch information
rtomayko committed Jun 7, 2009
1 parent f974af4 commit f75bee5
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 28 deletions.
9 changes: 9 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
= 1.0 / unreleased

* Route handlers, before filters, templates, error mappings, and
middleware are now resolved dynamically up the inheritance hierarchy
when needed instead of duplicating the superclass's version when
a new Sinatra::Base subclass is created. This should fix a variety
of issues with extensions that need to add any of these things
to the base class.

= 0.9.2 / 2009-05-18

* This version is compatible with Rack 1.0. [Rein Henrichs]
Expand Down
53 changes: 25 additions & 28 deletions lib/sinatra/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -606,17 +606,28 @@ def clean_backtrace(trace)
}.map! { |line| line.gsub(/^\.\//, '') }
end

@routes = {}
@filters = []
@conditions = []
@templates = {}
@middleware = []
@errors = {}
@prototype = nil
@extensions = []

class << self
attr_accessor :routes, :filters, :conditions, :templates, :errors
attr_reader :routes, :filters, :templates, :errors

def reset!
@conditions = []
@routes = {}
@filters = []
@templates = {}
@errors = {}
@middleware = []
@prototype = nil
@extensions = []
end

# Extension modules registered on this class and all superclasses.
def extensions
if superclass.respond_to?(:extensions)
(@extensions + superclass.extensions).uniq
else
@extensions
end
end

# Middleware used in this class and all superclasses.
def middleware
Expand Down Expand Up @@ -793,7 +804,7 @@ def route(verb, path, options={}, &block)

invoke_hook(:route_added, verb, path, block)

(routes[verb] ||= []).
(@routes[verb] ||= []).
push([pattern, keys, conditions, block]).last
end

Expand Down Expand Up @@ -836,10 +847,6 @@ def helpers(*extensions, &block)
include(*extensions) if extensions.any?
end

def extensions
(@extensions + (superclass.extensions rescue [])).uniq
end

def register(*extensions, &block)
extensions << Module.new(&block) if block_given?
@extensions += extensions
Expand Down Expand Up @@ -898,7 +905,6 @@ def new(*args, &bk)
builder.use Rack::CommonLogger if logging?
builder.use Rack::MethodOverride if methodoverride?
builder.use ShowExceptions if show_exceptions?

middleware.each { |c,a,b| builder.use(c, *a, &b) }

builder.run super
Expand All @@ -909,17 +915,6 @@ def call(env)
synchronize { prototype.call(env) }
end

def reset!(base=superclass)
@routes = {}
@templates = {}
@conditions = []
@filters = []
@errors = {}
@middleware = []
@prototype = nil
@extensions = []
end

private
def detect_rack_handler
servers = Array(self.server)
Expand All @@ -934,7 +929,7 @@ def detect_rack_handler
end

def inherited(subclass)
subclass.reset! self
subclass.reset!
super
end

Expand Down Expand Up @@ -977,6 +972,8 @@ def caller_locations
end
end

reset!

set :raise_errors, true
set :dump_errors, false
set :clean_trace, true
Expand Down

0 comments on commit f75bee5

Please sign in to comment.