rack-flash3-1.0.5/0000755000004100000410000000000012211340030013625 5ustar www-datawww-datarack-flash3-1.0.5/test/0000755000004100000410000000000012211340030014604 5ustar www-datawww-datarack-flash3-1.0.5/test/helper.rb0000644000004100000410000000147312211340030016415 0ustar www-datawww-datarequire '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 endrack-flash3-1.0.5/test/test_flash.rb0000644000004100000410000000702212211340030017266 0ustar www-datawww-datarequire 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. endrack-flash3-1.0.5/LICENSE0000644000004100000410000000210612211340030014631 0ustar www-datawww-dataThe 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.md0000644000004100000410000000554512211340030015115 0ustar www-datawww-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/0000755000004100000410000000000012211340030015260 5ustar www-datawww-datarack-flash3-1.0.5/example/sinatra_app.rb0000644000004100000410000000121112211340030020101 0ustar www-datawww-datarequire '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/0000755000004100000410000000000012211340030016415 5ustar www-datawww-datarack-flash3-1.0.5/example/views/show.erb0000644000004100000410000000054212211340030020070 0ustar www-datawww-data
<% if flash.has?(params[:name]) %>

<%= flash[params[:name]] %>

- from flash[:<%= params[:name] %>] <% else %>

No flash message for flash[:<%= params[:name] %>]

<% end %>
Go Back Home
rack-flash3-1.0.5/example/views/layout.erb0000644000004100000410000000443612211340030020433 0ustar www-datawww-data Flash Examples

Sinatra::Flash

<%= yield %>
rack-flash3-1.0.5/example/views/index.erb0000644000004100000410000000136612211340030020224 0ustar www-datawww-data<% if flash.has?(:err) %>

<%= flash[:err] %>

<% end %> <% if flash.has?(:ok) %>

<%= flash[:ok] %>

<% end %> <% [:notice, :error, :success, :whatevz, :bliggety].each do |name| %>
<% end %>rack-flash3-1.0.5/example/base_app.ru0000644000004100000410000000075212211340030017406 0ustar www-datawww-datarequire 'rack/request' require 'rack/response' require 'rack/showexceptions' require 'rack/session/cookie' require File.dirname(__FILE__) + '/../lib/rack-flash' class Base attr_accessor :env def call(env) @env = env flash['err'] = "IT'S ALIVE" res = Rack::Response.new res.write "Flashy" res.write "#{flash['err']}" res.finish end end use Rack::Session::Cookie use Rack::Flash#, :flash_app_class => Base use Rack::ShowExceptions run Base.newrack-flash3-1.0.5/Rakefile0000644000004100000410000000000012211340030015260 0ustar www-datawww-datarack-flash3-1.0.5/rack-flash3.gemspec0000644000004100000410000000151112211340030017266 0ustar www-datawww-datarequire File.expand_path('../lib/rack/flash/version', __FILE__) Gem::Specification.new do |gem| gem.authors = ["Pat Nakajima", "Travis Reeder"] gem.email = ["treeder@gmail.com"] gem.description = "Flash hash implementation for Rack apps." gem.summary = "Flash hash implementation for Rack apps." gem.homepage = "https://github.com/treeder/rack-flash" gem.files = `git ls-files`.split($\) gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) } gem.test_files = gem.files.grep(%r{^(test|spec|features)/}) gem.name = "rack-flash3" gem.require_paths = ["lib"] gem.version = RackFlash::VERSION gem.required_rubygems_version = ">= 1.3.6" gem.required_ruby_version = Gem::Requirement.new(">= 1.8") gem.add_runtime_dependency "rack", ">= 0" end rack-flash3-1.0.5/checksums.yaml.gz0000444000004100000410000000041612211340030017114 0ustar www-datawww-data7%Re=nAD9\`V@8s'6ĚȧWU/_?3|wsn݌iGYVAf)Ёm̓\fߺ~y3hc Coۃ6T!=' - !ruby/object:Gem::Version version: '0' type: :runtime prerelease: false version_requirements: !ruby/object:Gem::Requirement requirements: - - '>=' - !ruby/object:Gem::Version version: '0' description: Flash hash implementation for Rack apps. email: - treeder@gmail.com executables: [] extensions: [] extra_rdoc_files: [] files: - .gitignore - LICENSE - README.md - Rakefile - VERSION.yml - example/base_app.ru - example/sinatra_app.rb - example/views/index.erb - example/views/layout.erb - example/views/show.erb - lib/rack-flash.rb - lib/rack/flash.rb - lib/rack/flash/test.rb - rack-flash3.gemspec - test/helper.rb - test/test_flash.rb homepage: https://github.com/treeder/rack-flash licenses: [] metadata: {} post_install_message: rdoc_options: [] require_paths: - lib required_ruby_version: !ruby/object:Gem::Requirement requirements: - - '>=' - !ruby/object:Gem::Version version: '1.8' required_rubygems_version: !ruby/object:Gem::Requirement requirements: - - '>=' - !ruby/object:Gem::Version version: 1.3.6 requirements: [] rubyforge_project: rubygems_version: 2.0.3 signing_key: specification_version: 4 summary: Flash hash implementation for Rack apps. test_files: - test/helper.rb - test/test_flash.rb rack-flash3-1.0.5/VERSION.yml0000644000004100000410000000005312211340030015473 0ustar www-datawww-data--- :major: 1 :minor: 0 :patch: 4 :build: rack-flash3-1.0.5/.gitignore0000644000004100000410000000001312211340030015607 0ustar www-datawww-data.idea pkg/ rack-flash3-1.0.5/lib/0000755000004100000410000000000012211340030014373 5ustar www-datawww-datarack-flash3-1.0.5/lib/rack-flash.rb0000644000004100000410000000007212211340030016732 0ustar www-datawww-datarequire File.join(File.dirname(__FILE__), *%w[rack flash])rack-flash3-1.0.5/lib/rack/0000755000004100000410000000000012211340030015313 5ustar www-datawww-datarack-flash3-1.0.5/lib/rack/flash/0000755000004100000410000000000012211340030016410 5ustar www-datawww-datarack-flash3-1.0.5/lib/rack/flash/test.rb0000644000004100000410000000042212211340030017712 0ustar www-datawww-datamodule Rack class Flash def self.fake_session @fake_session ||= {} end alias_method :old_call, :call def new_call(env) env['rack.session'] ||= Rack::Flash.fake_session old_call(env) end alias_method :call, :new_call end endrack-flash3-1.0.5/lib/rack/flash.rb0000644000004100000410000000764112211340030016745 0ustar www-datawww-datamodule Rack class Flash # Raised when the session passed to FlashHash initialize is nil. This # is usually an indicator that session middleware is not in use. class SessionUnavailable < StandardError; end # Implements bracket accessors for storing and retrieving flash entries. class FlashHash attr_reader :flagged def initialize(store, opts={}) raise Rack::Flash::SessionUnavailable \ .new('Rack::Flash depends on session middleware.') unless store @opts = opts @store = store if accessors = @opts[:accessorize] accessors.each { |opt| def_accessor(opt) } end end # Remove an entry from the session and return its value. Cache result in # the instance cache. def [](key) key = key.to_sym cache[key] ||= values.delete(key) end # Store the entry in the session, updating the instance cache as well. def []=(key,val) key = key.to_sym cache[key] = values[key] = val 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. def now cache end # Checks for the presence of a flash entry without retrieving or removing # it from the cache or store. def has?(key) [cache, values].any? { |store| store.keys.include?(key.to_sym) } end alias_method :include?, :has? def keys cache.keys | values.keys end # Mark existing entries to allow for sweeping. def flag! @flagged = values.keys end # Remove flagged entries from flash session, clear flagged list. def sweep! Array(flagged).each { |key| values.delete(key) } flagged.clear end # Hide the underlying :__FLASH__ session key and only expose values stored # in the flash. def inspect '#' % [values.inspect, cache.inspect] end # Human readable for logging. def to_s values.inspect end private # Maintain an instance-level cache of retrieved flash entries. These # entries will have been removed from the session, but are still available # through the cache. def cache @cache ||= {} end # Helper to access flash entries from :__FLASH__ session value. This key # is used to prevent collisions with other user-defined session values. def values @store[:__FLASH__] ||= {} end # Generate accessor methods for the given entry key if :accessorize is true. def def_accessor(key) raise ArgumentError.new('Invalid entry type: %s' % key) if respond_to?(key) class << self; self end.class_eval do define_method(key) { |*args| val = args.first; val ? (self[key]=val) : self[key] } define_method("#{key}=") { |val| self[key] = val } define_method("#{key}!") { |val| cache[key] = val } end end end # ------------------------------------------------------------------------- # - Rack Middleware implementation def initialize(app, opts={}) if klass = app_class(app, opts) klass.class_eval do def flash; env['x-rack.flash'] end end end @app, @opts = app, opts end def call(env) env['x-rack.flash'] ||= Rack::Flash::FlashHash.new(env['rack.session'], @opts) if @opts[:sweep] env['x-rack.flash'].flag! end res = @app.call(env) if @opts[:sweep] env['x-rack.flash'].sweep! end res end private def app_class(app, opts) return nil if opts.has_key?(:helper) and not opts[:helper] opts[:flash_app_class] || defined?(Sinatra::Base) && Sinatra::Base end end end