Skip to content
Closed
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
8 changes: 8 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
source 'https://rubygems.org'

gemspec

group :test do
gem 'bacon'
gem 'jeweler2'
end
23 changes: 23 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
PATH
remote: .
specs:
rack-flash3 (1.0.1)
rack
rack

GEM
remote: https://rubygems.org/
specs:
bacon (1.0.0)
git (1.2.9.1)
jeweler2 (2.0.9)
git (>= 1.2.5)
rack (1.6.0)

PLATFORMS
ruby

DEPENDENCIES
bacon
jeweler2
rack-flash3!
14 changes: 14 additions & 0 deletions lib/rack/flash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class SessionUnavailable < StandardError; end

# Implements bracket accessors for storing and retrieving flash entries.
class FlashHash
include Enumerable
attr_reader :flagged

def initialize(store, opts={})
Expand All @@ -21,6 +22,14 @@ def initialize(store, opts={})
end
end

def each(&block)
if values.empty?
cache.each(&block)
else
values.each(&block)
end
end

# Remove an entry from the session and return its value. Cache result in
# the instance cache.
def [](key)
Expand Down Expand Up @@ -59,6 +68,11 @@ def sweep!
flagged.clear
end

# Clear the internal cache
def clear_cache!
@cache.clear
end

# Hide the underlying :__FLASH__ session key and only expose values stored
# in the flash.
def inspect
Expand Down
2 changes: 1 addition & 1 deletion test/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,4 @@ def swept?
@sweeped
end
end
end
end
21 changes: 15 additions & 6 deletions test/test_flash.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
require File.dirname(__FILE__) + '/helper'
require_relative './helper'
include Rack::Test::Methods

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

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

before do
Expand All @@ -22,11 +22,18 @@ def new_flash(entries={})
new_flash[:foo].should.equal('bar')
end

it 'accepts strings or hashes' do
it 'accepts strings or symbols' do
new_flash[:foo] = 'bar'
new_flash['foo'].should.equal('bar')
end

it 'can be iterated over' do
flash = new_flash(:foo => 'bar', :fizz => 'buzz')
keys = flash.map { |key, value| key }
keys.should.include(:foo)
keys.should.include(:fizz)
end

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

Expand Down Expand Up @@ -143,7 +151,8 @@ def new_flash(entries={})
end
}

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

get '/', :env=>{ 'x-rack.flash' => fake_flash }

Expand All @@ -154,4 +163,4 @@ def new_flash(entries={})
end

# Testing sessions is a royal pain in the ass.
end
end