Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions lib/rack/flash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ def []=(key,val)
cache[key] = values[key] = val
end

# Mimic the each function of a normal hash for the flash values
def each(&block)
cache.merge! values
values = {}
cache.each(&block)
end

# Store a flash entry for only the current request, swept regardless of
# whether or not it was actually accessed. Useful for AJAX requests, where
# you want a flash message, even though you're response isn't redirecting.
Expand Down
21 changes: 17 additions & 4 deletions test/test_flash.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require File.dirname(__FILE__) + '/helper'

describe 'Rack::Flash' do
include Rack::Test::Methods
extend Rack::Test::Methods

def app(&block)
return Sinatra.new &block
Expand All @@ -26,6 +26,14 @@ def new_flash(entries={})
new_flash[:foo] = 'bar'
new_flash['foo'].should.equal('bar')
end

it 'has an each method' do
new_flash[:foo] = 'bar'
new_flash[:fizz] = 'buzz'
new_flash.each do |k,v|
k == :foo ? v.should.equal('bar') : v.should.equal('buzz')
end
end

it 'deletes entries from session after retrieval' do
new_flash[:foo] = 'bar'
Expand Down Expand Up @@ -74,7 +82,7 @@ def new_flash(entries={})
flash = new_flash
flash[:foo] = 'bar'
@fake_session.clear
flash['foo'].should.equal(nil)
flash[:foo].should.equal('bar')
end

describe 'accessorize option' do
Expand Down Expand Up @@ -132,16 +140,21 @@ def new_flash(entries={})
end

describe 'integration' do

def app(&block)
return Sinatra.new &block
end

it 'provides :sweep option to clear unused entries' do
app {
app do
use Rack::Flash, :sweep => true

set :sessions, true

get '/' do
'ok'
end
}
end

fake_flash = Rack::FakeFlash.new(:foo => 'bar')

Expand Down