From 50e0eeefbdc9201810cef1666762e283b4b890ea Mon Sep 17 00:00:00 2001 From: Zamith Date: Tue, 3 Feb 2015 12:09:13 +0000 Subject: [PATCH] Makes FlashHash Enumerable This allows the iteration over the flash messages in a view, like Rails does. Added a method to clear the hash cache, so that by clearing the session and the cache we can effectivly empty the flash messages hash. Also added a Gemfile with the test dependencies and corrected the tests (except the integration test). This will make it easier for the next person trying to contribute. --- Gemfile | 8 ++++++++ Gemfile.lock | 23 +++++++++++++++++++++++ lib/rack/flash.rb | 14 ++++++++++++++ test/helper.rb | 2 +- test/test_flash.rb | 21 +++++++++++++++------ 5 files changed, 61 insertions(+), 7 deletions(-) create mode 100644 Gemfile create mode 100644 Gemfile.lock diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..916785a --- /dev/null +++ b/Gemfile @@ -0,0 +1,8 @@ +source 'https://rubygems.org' + +gemspec + +group :test do + gem 'bacon' + gem 'jeweler2' +end diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..9b56390 --- /dev/null +++ b/Gemfile.lock @@ -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! diff --git a/lib/rack/flash.rb b/lib/rack/flash.rb index 6b2935a..4e301bd 100644 --- a/lib/rack/flash.rb +++ b/lib/rack/flash.rb @@ -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={}) @@ -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) @@ -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 diff --git a/test/helper.rb b/test/helper.rb index 5fc901b..0931fae 100644 --- a/test/helper.rb +++ b/test/helper.rb @@ -48,4 +48,4 @@ def swept? @sweeped end end -end \ No newline at end of file +end diff --git a/test/test_flash.rb b/test/test_flash.rb index a670940..7ec6b31 100644 --- a/test/test_flash.rb +++ b/test/test_flash.rb @@ -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 @@ -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] @@ -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 @@ -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 } @@ -154,4 +163,4 @@ def new_flash(entries={}) end # Testing sessions is a royal pain in the ass. -end \ No newline at end of file +end