Fork me on GitHub

rack-flash by nakajima

flash[:notice] = "You can stop rolling your own now."

Simple flash hash implementation for Rack apps.

Try it out here: flash.patnakajima.net.

Usage

Here’s how to use it.

Vanilla Rack apps

You can access flash entries via env['rack-flash']. You can treat it either like a regular flash hash:

use Rack::Flash

# Set a flash entry
env['rack-flash']['notice'] = 'You have logged out.'

# Get a flash entry
env['rack-flash']['notice'] # => 'You have logged out.'

# Set a a flash entry for only the current request
env['rack-flash'].now['notice'] = 'You have logged out.'

Or you can pass the :accessorize option to declare your flash types. Each of these will have accessors defined on the flash object:

use Rack::Flash, :accessorize => [:notice, :error]

# Set a flash entry
env['rack-flash'].notice = 'You have logged out.'

# Get a flash entry
env['rack-flash'].notice # => 'You have logged out.'

# Set a a flash entry for only the current request
env['rack-flash'].notice! 'You have logged out.'

Sample rack app:

get = proc { |env|
  [200, {},
    env['rack-flash'].notice || 'No flash set. Try going to /set'
  ]
}

set = proc { |env|
  env['rack-flash'].notice = 'Hey, the flash was set!'
  [302, {'Location' => '/'},
    'You are being redirected.'
  ]
}

builder = Rack::Builder.new do
  use Rack::Session::Cookie
  use Rack::Flash, :accessorize => true

  map('/set') { run set }
  map('/')    { run get }
end

Rack::Handler::Mongrel.run builder, :Port => 9292

Sinatra

If you’re using Sinatra, you can use the flash hash just like in Rails:

require 'sinatra/base'
require 'rack-flash'

class MyApp < Sinatra::Base
  use Rack::Flash

  post '/set-flash' do
    # Set a flash entry
    flash[:notice] = "Thanks for signing up!"

    # Get a flash entry
    flash[:notice] # => "Thanks for signing up!"

    # Set a flash entry for only the current request
    flash.now[:notice] = "Thanks for signing up!"
  end
end

If you’ve got any ideas on how to simplify access to the flash hash for vanilla Rack apps, let me know. It still feels a bit off to me.

Sweeping stale entries

By default Rack::Flash has slightly different behavior than Rails in that it doesn’t delete entries until they are used. If you want entries to be cleared even if they are not ever accessed, you can use the :sweep option:

use Rack::Flash, :sweep => true

This will sweep stale flash entries, whether your not you actually use them.

Dependencies

rack

Install

gem install nakajima-rack-flash

License

Copyright (c) 2009 Pat Nakajima

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

Authors

Pat Nakajima (patnakajima@gmail.com)

Contact

Pat Nakajima (patnakajima@gmail.com)

Download

You can download this project in either zip or tar formats.

You can also clone the project with Git by running:

$ git clone git://github.com/nakajima/rack-flash