diff --git a/README.md b/README.md index 6c023c4..116a798 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,8 @@ # Sinatra Flash -Simple flash hash implementation for Sinatra. +Simple flash hash implementation for Sinatra. My implementation has slightly +different behavior than Rails in that it doesn't delete entries until they used. +I think it's pretty rad, but I'm happy to hear thoughts to the contrary. ## Usage diff --git a/lib/sinatra-flash.rb b/lib/sinatra-flash.rb index eb61462..2e71854 100644 --- a/lib/sinatra-flash.rb +++ b/lib/sinatra-flash.rb @@ -2,8 +2,8 @@ module Sinatra module Flash # Implements bracket accessors for storing and retrieving flash entries. class FlashHash - def initialize(env) - @session = env['rack.session'] + def initialize(session) + @session = session @session[:__FLASH__] ||= {} end @@ -41,7 +41,7 @@ def values end def flash - @flash ||= Sinatra::Flash::FlashHash.new(env) + @flash ||= Sinatra::Flash::FlashHash.new(session) end end end \ No newline at end of file diff --git a/test/helper.rb b/test/helper.rb new file mode 100644 index 0000000..38ae3c1 --- /dev/null +++ b/test/helper.rb @@ -0,0 +1,25 @@ +require 'rubygems' +require 'sinatra/base' +require 'bacon' +require 'sinatra/test' +require 'sinatra/test/bacon' +require File.join(File.dirname(__FILE__), *%w[.. lib sinatra-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 + +def mock_app(&block) + @app = Sinatra.new(&block) +end \ No newline at end of file diff --git a/test/test_flash.rb b/test/test_flash.rb new file mode 100644 index 0000000..04d968d --- /dev/null +++ b/test/test_flash.rb @@ -0,0 +1,29 @@ +require File.dirname(__FILE__) + '/helper' + +describe 'Sinatra::Flash' do + before do + mock_app { + include Sinatra::Flash + + set :sessions, false + + get '/view' do + flash[:notice] + end + + post '/set' do + flash[:notice] = params[:q] + redirect '/view' + end + } + end + + it 'is empty by default' do + err_explain do + get '/view' + body.should.be.empty + end + end + + # Testing sessions is a royal pain in the ass. +end \ No newline at end of file