rack-flash3-1.0.5/ 0000755 0000041 0000041 00000000000 12211340030 013625 5 ustar www-data www-data rack-flash3-1.0.5/test/ 0000755 0000041 0000041 00000000000 12211340030 014604 5 ustar www-data www-data rack-flash3-1.0.5/test/helper.rb 0000644 0000041 0000041 00000001473 12211340030 016415 0 ustar www-data www-data require 'rubygems' gem 'sinatra', '<=1.3.2' require 'sinatra/base' require 'bacon' require 'rack/test' require File.join(File.dirname(__FILE__), *%w[.. lib rack-flash]) class String [:green, :yellow, :red].each { |c| define_method(c) { self } } end if ENV['TM_RUBY'] # bacon swallows errors alive def err_explain begin yield rescue => e puts e.inspect puts e.backtrace raise e end end module Rack class FakeFlash < Rack::Flash::FlashHash attr_reader :flagged, :sweeped, :store def initialize(*args) @flagged, @sweeped = false, false @store = {} super(@store) end def flag! @flagged = true super end def sweep! @sweeped = true super end def flagged? @flagged end def swept? @sweeped end end end rack-flash3-1.0.5/test/test_flash.rb 0000644 0000041 0000041 00000007022 12211340030 017266 0 ustar www-data www-data require File.dirname(__FILE__) + '/helper' describe 'Rack::Flash' do include Rack::Test::Methods def app(&block) return Sinatra.new &block end before do @fake_session = {} end def new_flash(entries={}) flash = Rack::Flash::FlashHash.new(@fake_session) entries.each { |key,val| flash[key] = val } flash end it 'stores entries' do new_flash[:foo] = 'bar' new_flash[:foo].should.equal('bar') end it 'accepts strings or hashes' do new_flash[:foo] = 'bar' new_flash['foo'].should.equal('bar') end it 'deletes entries from session after retrieval' do new_flash[:foo] = 'bar' new_flash[:foo] new_flash[:foo].should.be.nil end it 'caches retrieved entries in instance' do flash = new_flash(:foo => 'bar') flash[:foo].should.equal('bar') flash[:foo].should.equal('bar') end it 'does not step on session keys' do @fake_session[:foo] = true new_flash[:foo] = false @fake_session[:foo].should.be.true end it 'can flag existing entries' do flash = new_flash(:foo => 'bar', :fizz => 'buzz') flash.flag! flash.flagged.should.include(:foo) flash.flagged.should.include(:fizz) end it 'can sweep flagged entries' do err_explain do flash = new_flash(:foo => 'bar', :fizz => 'buzz') flash.flag! flash.sweep! flash.flagged.should.be.empty new_flash[:foo].should.be.nil new_flash[:fizz].should.be.nil end end it 'allows setters with Flash.now semantics' do flash = new_flash flash.now[:foo] = 'bar' flash[:foo].should.equal('bar') new_flash[:foo].should.be.nil end it 'does not raise an error when session is cleared' do flash = new_flash flash[:foo] = 'bar' @fake_session.clear flash['foo'].should.equal(nil) end describe 'accessorize option' do def new_flash(entries={}) flash = Rack::Flash::FlashHash.new(@fake_session, :accessorize => [:foo, :fizz]) entries.each { |key,val| flash[key] = val } flash end it 'allows getters' do flash = new_flash(:foo => 'bar') flash.foo.should.equal('bar') end it 'allows setters' do flash = new_flash flash.fizz = 'buzz' flash.fizz.should.equal('buzz') end it 'allows declarative setters' do flash = new_flash flash.fizz 'buzz' flash.fizz.should.equal('buzz') end it 'allows setters with Flash.now semantics' do flash = new_flash flash.foo! 'bar' flash.foo.should.equal('bar') new_flash[:foo].should.be.nil end it 'only defines accessors for passed entry types' do err_explain do flash = new_flash proc { flash.bliggety = 'blam' }.should.raise(NoMethodError) end end end it 'does not provide getters by default' do proc { new_flash(:foo => 'bar').foo }.should.raise(NoMethodError) end it 'does not provide setters by default' do proc { flash = new_flash flash.fizz = 'buzz' }.should.raise(NoMethodError) end describe 'integration' do it 'provides :sweep option to clear unused entries' do app { use Rack::Flash, :sweep => true set :sessions, true get '/' do 'ok' end } fake_flash = Rack::FakeFlash.new(:foo => 'bar') get '/', :env=>{ 'x-rack.flash' => fake_flash } fake_flash.should.be.flagged fake_flash.should.be.swept fake_flash.store[:foo].should.be.nil end end # Testing sessions is a royal pain in the ass. end rack-flash3-1.0.5/LICENSE 0000644 0000041 0000041 00000002106 12211340030 014631 0 ustar www-data www-data The MIT License (MIT) Copyright (c) 2013 Travis Reeder, 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. rack-flash3-1.0.5/README.md 0000644 0000041 0000041 00000005545 12211340030 015115 0 ustar www-data www-data # Patch the Call to rack_builder Treeder forked the original Rack::Flash and patched its issues, see https://github.com/treeder/rack-flash However, when *not* using Sinatra, the code still won't work. This patches that in the Patch_rack_builder_call branch. [View the RDoc](http://gitrdoc.com/nakajima/rack-flash/tree/master). Below is the original README.md ---- # Rack Flash flash[:notice] = "You can stop rolling your own now." Simple flash hash implementation for Rack apps. [View the RDoc](http://gitrdoc.com/nakajima/rack-flash/tree/master). ## Usage Here's how to use it. ### Install / add to Gemfile gem 'rack-flash3' ### Vanilla Rack apps You can access flash entries via `env['x-rack.flash']`. You can treat it either like a regular flash hash: env['x-rack.flash'][: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['x-rack.flash'].notice = 'You have logged out.' # Get a flash entry env['x-rack.flash'].notice # => 'You have logged out.' # Set a a flash entry for only the current request env['x-rack.flash'].notice! 'You have logged out.' Sample rack app: get = proc { |env| [200, {}, env['x-rack.flash'].notice || 'No flash set. Try going to /set' ] } set = proc { |env| env['x-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 enable :sessions 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 or not you actually use them. rack-flash3-1.0.5/example/ 0000755 0000041 0000041 00000000000 12211340030 015260 5 ustar www-data www-data rack-flash3-1.0.5/example/sinatra_app.rb 0000644 0000041 0000041 00000001211 12211340030 020101 0 ustar www-data www-data require 'rubygems' require 'sinatra/base' require File.dirname(__FILE__) + '/../lib/rack-flash' class MyApp < Sinatra::Base use Rack::Flash set :root, File.dirname(__FILE__) set :layout, true set :logging, true set :sessions, true get '/' do erb :index end # View the value of any given flash get '/:name' do erb :show end post '/:name' do if params[:message].strip.empty? flash["err"] = "You must enter a message." flash["err_on_#{params[:name]}"] = 1 redirect('/') end flash[:ok] = "Set flash entry!" flash[params[:name]] = params[:message] redirect '/' end run! end rack-flash3-1.0.5/example/views/ 0000755 0000041 0000041 00000000000 12211340030 016415 5 ustar www-data www-data rack-flash3-1.0.5/example/views/show.erb 0000644 0000041 0000041 00000000542 12211340030 020070 0 ustar www-data www-data
flash[:<%= params[:name] %>]
<% else %>
No flash message for flash[:<%= params[:name] %>]
Sinatra::Flash