Skip to content

Commit

Permalink
Fixed to work with Sinatra::Base applications
Browse files Browse the repository at this point in the history
  • Loading branch information
SFEley committed Apr 28, 2010
1 parent 8f32b57 commit 492e602
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 8 deletions.
20 changes: 12 additions & 8 deletions lib/sinatra/flash.rb
Expand Up @@ -6,15 +6,19 @@
module Sinatra
module Flash

# This callback rotates any flash structure we referenced, placing the 'next' hash into the session
# for the next request.
after do
set :sessions, true unless session # If you do not have a session, one will be appointed for you by the court.
@flash.each{|key, flash| session[key] = @flash[key].next}
def self.registered(app)
app.helpers Flash::Storage
app.helpers Flash::Style

# This callback rotates any flash structure we referenced, placing the 'next' hash into the session
# for the next request.
app.after do
set :sessions, true unless session # If you do not have a session, one will be appointed for you by the court.
@flash.each{|key, flash| session[key] = @flash[key].next}
end
end

helpers Storage
helpers Style

end

register Flash
end
72 changes: 72 additions & 0 deletions sinatra-flash.gemspec
@@ -0,0 +1,72 @@
# Generated by jeweler
# DO NOT EDIT THIS FILE DIRECTLY
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
# -*- encoding: utf-8 -*-

Gem::Specification.new do |s|
s.name = %q{sinatra-flash}
s.version = "0.1.0"

s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Stephen Eley"]
s.date = %q{2010-04-26}
s.description = %q{A Sinatra extension for setting and showing Rails-like flash messages. This extension improves on the Rack::Flash gem by being simpler to use, providing a full range of hash operations (including iterating through various flash keys, testing the size of the hash, etc.), and offering a 'styled_flash' view helper to render the entire flash hash with sensible CSS classes. The downside is reduced flexibility -- these methods will *only* work in Sinatra.}
s.email = %q{sfeley@gmail.com}
s.extra_rdoc_files = [
"LICENSE.markdown",
"README.markdown"
]
s.files = [
".document",
".gitignore",
"LICENSE.markdown",
"README.markdown",
"Rakefile",
"VERSION",
"lib/sinatra/flash.rb",
"lib/sinatra/flash/hash.rb",
"lib/sinatra/flash/storage.rb",
"lib/sinatra/flash/style.rb",
"spec/classic_spec.rb",
"spec/flash/hash_spec.rb",
"spec/flash/style_spec.rb",
"spec/flash_spec.rb",
"spec/spec.opts",
"spec/spec_helper.rb"
]
s.homepage = %q{http://github.com/SFEley/sinatra-flash}
s.rdoc_options = ["--charset=UTF-8"]
s.require_paths = ["lib"]
s.rubygems_version = %q{1.3.6}
s.summary = %q{Proper flash messages in Sinatra}
s.test_files = [
"spec/classic_spec.rb",
"spec/flash/hash_spec.rb",
"spec/flash/style_spec.rb",
"spec/flash_spec.rb",
"spec/spec_helper.rb"
]

if s.respond_to? :specification_version then
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
s.specification_version = 3

if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
s.add_runtime_dependency(%q<sinatra>, [">= 1.0.0"])
s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
s.add_development_dependency(%q<yard>, [">= 0"])
s.add_development_dependency(%q<sinatra-sessionography>, [">= 0"])
else
s.add_dependency(%q<sinatra>, [">= 1.0.0"])
s.add_dependency(%q<rspec>, [">= 1.2.9"])
s.add_dependency(%q<yard>, [">= 0"])
s.add_dependency(%q<sinatra-sessionography>, [">= 0"])
end
else
s.add_dependency(%q<sinatra>, [">= 1.0.0"])
s.add_dependency(%q<rspec>, [">= 1.2.9"])
s.add_dependency(%q<yard>, [">= 0"])
s.add_dependency(%q<sinatra-sessionography>, [">= 0"])
end
end

50 changes: 50 additions & 0 deletions spec/base_spec.rb
@@ -0,0 +1,50 @@
require File.dirname(__FILE__) + '/spec_helper'
require 'sinatra/base'

class BaseApp < Sinatra::Base
helpers Sinatra::Sessionography
register Sinatra::Flash


get '/flash' do
if params[:key]
flash(params[:key]).inspect
else
flash.inspect
end
end

post '/flash' do
if (key = params.delete('key'))
params.each{|k,v| flash(key)[k.to_sym] = v.to_sym}
flash(key).inspect
else
params.each{|k,v| flash[k.to_sym] = v.to_sym}
flash.inspect
end
end

end

describe "Sinatra::Flash in a Sinatra::Base application" do

def app
BaseApp
end

before(:each) do
Sinatra::Sessionography.session = nil
end

it "shows nothing when no flash has been set" do
get '/flash'
last_response.body.should == "{}"
end

it "can set and retrieve the flash" do
post '/flash', {:foo => :bar}
get '/flash'
last_response.body.should == "{:foo=>:bar}"
end

end

0 comments on commit 492e602

Please sign in to comment.