Skip to content

Commit

Permalink
on_boot hooks for applications
Browse files Browse the repository at this point in the history
  • Loading branch information
slivu committed May 24, 2013
1 parent 4f28360 commit 543e032
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
30 changes: 19 additions & 11 deletions lib/e-builder/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,24 +80,23 @@ def global_setup &proc
# displays URLs the app will respond to,
# with controller and action that serving each URL.
def url_map opts = {}
to_app!
map = {}
sorted_routes.each do |r|
@routes[r].each_pair { |rm, as| (map[r] ||= {})[rm] = as.dup }
mount_controllers!
map = sorted_routes.inject({}) do |m,r|
@routes[r].each_pair {|rm,rs| (m[r] ||= {})[rm] = rs.dup}; m
end

def map.to_s
out = []
out = ''
self.each_pair do |route, request_methods|
next if route.source.size == 0
out << "%s\n" % route.source
request_methods.each_pair do |request_method, route_setup|
out << " %s%s" % [request_method, ' ' * (10 - request_method.to_s.size)]
out << "%s#%s\n" % [route_setup[:controller], route_setup[:action]]
request_methods.each_pair do |rm,rs|
out << " %s%s" % [rm, ' ' * (10 - rm.to_s.size)]
out << "%s\n" % (rs[:app] || [rs[:controller], rs[:action]]*'#')
end
out << "\n"
end
out.join
out
end
map
end
Expand All @@ -121,6 +120,8 @@ def environment
# @option opts [String] :host (0.0.0.0)
#
def run opts = {}
boot!

handler = opts.delete(:server)
(handler && Rack::Handler.const_defined?(handler)) || (handler = HTTP__DEFAULT_SERVER)

Expand Down Expand Up @@ -151,7 +152,9 @@ def call env

def app
@app ||= begin
on_boot!
mount_controllers!
@sorted_routes = sorted_routes.freeze
@routes.freeze
middleware.reverse.inject(lambda {|env| call!(env)}) {|a,e| e[a]}
end
Expand All @@ -168,7 +171,7 @@ def to_app
def call! env
path = env[ENV__PATH_INFO]
script_name = env[ENV__SCRIPT_NAME]
sorted_routes.each do |route|
@sorted_routes.each do |route|
if matches = route.match(path)

if route_setup = @routes[route][env[ENV__REQUEST_METHOD]] || @routes[route][:*]
Expand Down Expand Up @@ -219,7 +222,7 @@ def call! env
end

def sorted_routes
@sorted_routes ||= @routes.keys.sort {|a,b| b.source.size <=> a.source.size}.freeze
@routes.keys.sort {|a,b| b.source.size <=> a.source.size}
end

def valid_host? accepted_hosts, env
Expand Down Expand Up @@ -305,6 +308,11 @@ def mount_applications applications, root = nil, opts = {}
end
alias mount_application mount_applications

# execute blocks defined via `on_boot`
def on_boot!
(@on_boot || []).each {|b| b.call}
end

# Some Rack handlers (Thin, Rainbows!) implement an extended body object protocol, however,
# some middleware (namely Rack::Lint) will break it by not mirroring the methods in question.
# This middleware will detect an extended body object and will make sure it reaches the
Expand Down
5 changes: 5 additions & 0 deletions lib/e-builder/setup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -230,4 +230,9 @@ def streaming_backend
end
end

# block(s) to run just before application starts
def on_boot &proc
(@on_boot ||= []).push(proc)
end

end

0 comments on commit 543e032

Please sign in to comment.