Skip to content

Commit

Permalink
stab one at automatically finding the rack root app and adding flash …
Browse files Browse the repository at this point in the history
…to it
  • Loading branch information
knzconnor committed Apr 17, 2009
1 parent 3c1b93b commit caa922b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion example/base_app.ru
Expand Up @@ -17,6 +17,6 @@ class Base
end
end
use Rack::Session::Cookie
use Rack::Flash, :flash_app_class => Base
use Rack::Flash#, :flash_app_class => Base
use Rack::ShowExceptions
run Base.new
22 changes: 22 additions & 0 deletions lib/rack/flash.rb
@@ -1,3 +1,20 @@
module Rack
class Builder
attr :ins
def use(middleware, *args, &block)
middleware.instance_variable_set "@rack_builder", self
@ins << lambda { |app|
middleware.new(app, *args, &block)
}
end

def run(app)
app.class.instance_variable_set "@rack_builder", self
@ins << app #lambda { |nothing| app }
end
end
end

module Rack
class Flash
# Raised when the session passed to FlashHash initialize is nil. This
Expand Down Expand Up @@ -105,6 +122,11 @@ def initialize(app, opts={})
app_class.class_eval do
def flash; env['rack-flash'] end
end
else
rack_root_app = self.class.instance_variable_get("@rack_builder").ins.last
def rack_root_app.flash
env['rack-flash']
end
end
@app, @opts = app, opts
end
Expand Down

0 comments on commit caa922b

Please sign in to comment.