Skip to content

Commit

Permalink
Removed dependence on Sinatra. Start renaming.
Browse files Browse the repository at this point in the history
  • Loading branch information
nakajima committed Feb 27, 2009
1 parent 4e08c2a commit dca1d97
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 16 deletions.
5 changes: 3 additions & 2 deletions example/app.rb
Expand Up @@ -3,15 +3,16 @@
require File.join(File.dirname(__FILE__), *%w[.. lib sinatra-flash.rb])

class MyApp < Sinatra::Base
include Sinatra::Flash
use Rack::Flash

set :root, File.dirname(__FILE__)
set :layout, true
set :logging, true
set :sessions, true

before do
puts session.inspect
@env['rack.errors'].write '[flash] %s ' % flash
@env['rack.errors'].write "\n"
end

get '/' do
Expand Down
4 changes: 3 additions & 1 deletion example/views/layout.erb
Expand Up @@ -95,6 +95,8 @@

.highlight {
background: #ffc;
padding: 10px;
margin-bottom: 10px;
}

#content {
Expand All @@ -104,7 +106,7 @@

#result {
font-size: 48px;
margin: 20% auto 0;
margin: 10% auto 20%;
text-align: center;
display: block;
}
Expand Down
35 changes: 24 additions & 11 deletions lib/sinatra-flash.rb
@@ -1,14 +1,14 @@
module Sinatra
module Flash
module Rack
class Flash
class SessionUnavailable < StandardError; end

# Implements bracket accessors for storing and retrieving flash entries.
class FlashHash
attr_reader :flagged

def initialize(session)
@session = session
@session[:__FLASH__] ||= {}
def initialize(store)
@store = store
@store[:__FLASH__] ||= {}
end

# Remove an entry from the session and return its value. Cache result in
Expand All @@ -34,6 +34,10 @@ def inspect
'#<FlashHash @values=%s>' % [values.inspect]
end

def to_s
values.inspect
end

# Mark existing entries to allow for sweeping.
def flag!
@flagged = values.keys
Expand Down Expand Up @@ -67,14 +71,23 @@ def cache
# Helper to access flash entries from :__FLASH__ session value. This key
# is used to prevent collisions with other user-defined session values.
def values
@session[:__FLASH__]
@store[:__FLASH__]
end
end

def flash
raise Sinatra::Flash::SessionUnavailable \
.new('You must have sessions enabled to use Sinatra::Flash.') unless env['rack.session']
@flash ||= Sinatra::Flash::FlashHash.new(session)

def initialize(app)
@app = app
@app.class.class_eval do
def flash
raise Rack::Flash::SessionUnavailable \
.new('You must have sessions enabled to use Rack::Flash.') unless env['rack.session']
@flash ||= Rack::Flash::FlashHash.new(env['rack.session'])
end
end
end

def call(env)
@app.call(env)
end
end
end
4 changes: 2 additions & 2 deletions test/test_flash.rb
Expand Up @@ -6,7 +6,7 @@
end

def new_flash(entries={})
flash = Sinatra::Flash::FlashHash.new(@fake_session)
flash = Rack::Flash::FlashHash.new(@fake_session)
entries.each { |key,val| flash[key] = val }
flash
end
Expand Down Expand Up @@ -60,7 +60,7 @@ def new_flash(entries={})
describe 'session integration' do
before do
mock_app {
include Sinatra::Flash
use Rack::Flash

set :sessions, true

Expand Down

0 comments on commit dca1d97

Please sign in to comment.