Skip to content
This repository
Newer
Older
100644 98 lines (64 sloc) 2.547 kb
dea52058 »
2009-02-26 IMPORTANT: Sinatra::Flash is now Rack::Flash.
1 # Rack Flash
21f51c5d »
2009-02-26 Implemented simple flash hash for Sinatra.
2
e2650316 »
2009-02-27 Removed metaid dependency and bumped version
3 flash[:notice] = "You can stop rolling your own now."
4
c7f7519a »
2009-02-27 Cleaned up dynamic accessors (thanks foca), added flash.now
5 Simple flash hash implementation for Rack apps.
21f51c5d »
2009-02-26 Implemented simple flash hash for Sinatra.
6
0c80686a »
2009-03-09 Added link to RDoc
7 [View the RDoc](http://gitrdoc.com/nakajima/rack-flash/tree/master).
8
21f51c5d »
2009-02-26 Implemented simple flash hash for Sinatra.
9 ## Usage
10
5c23efe5 »
2009-02-26 Added :accessorize option to dynamically generate accessors.
11 Here's how to use it.
12
87291306 »
2012-03-19 Added rack-flash3 install instructions.
13 ### Install / add to Gemfile
14
15 gem 'rack-flash3'
16
5c23efe5 »
2009-02-26 Added :accessorize option to dynamically generate accessors.
17 ### Vanilla Rack apps
18
fa8e736e »
2009-04-27 Switch to more spec friendly env key.
19 You can access flash entries via `env['x-rack.flash']`. You can treat it either
c7f7519a »
2009-02-27 Cleaned up dynamic accessors (thanks foca), added flash.now
20 like a regular flash hash:
21
fa8e736e »
2009-04-27 Switch to more spec friendly env key.
22 env['x-rack.flash'][:notice] = 'You have logged out.'
c7f7519a »
2009-02-27 Cleaned up dynamic accessors (thanks foca), added flash.now
23
24 Or you can pass the `:accessorize` option to declare your flash types. Each of
25 these will have accessors defined on the flash object:
26
27 use Rack::Flash, :accessorize => [:notice, :error]
28
29 # Set a flash entry
fa8e736e »
2009-04-27 Switch to more spec friendly env key.
30 env['x-rack.flash'].notice = 'You have logged out.'
c7f7519a »
2009-02-27 Cleaned up dynamic accessors (thanks foca), added flash.now
31
32 # Get a flash entry
fa8e736e »
2009-04-27 Switch to more spec friendly env key.
33 env['x-rack.flash'].notice # => 'You have logged out.'
c7f7519a »
2009-02-27 Cleaned up dynamic accessors (thanks foca), added flash.now
34
35 # Set a a flash entry for only the current request
fa8e736e »
2009-04-27 Switch to more spec friendly env key.
36 env['x-rack.flash'].notice! 'You have logged out.'
5c23efe5 »
2009-02-26 Added :accessorize option to dynamically generate accessors.
37
38 Sample rack app:
39
40 get = proc { |env|
41 [200, {},
fa8e736e »
2009-04-27 Switch to more spec friendly env key.
42 env['x-rack.flash'].notice || 'No flash set. Try going to /set'
5c23efe5 »
2009-02-26 Added :accessorize option to dynamically generate accessors.
43 ]
44 }
45
46 set = proc { |env|
fa8e736e »
2009-04-27 Switch to more spec friendly env key.
47 env['x-rack.flash'].notice = 'Hey, the flash was set!'
5c23efe5 »
2009-02-26 Added :accessorize option to dynamically generate accessors.
48 [302, {'Location' => '/'},
49 'You are being redirected.'
50 ]
51 }
52
53 builder = Rack::Builder.new do
54 use Rack::Session::Cookie
55 use Rack::Flash, :accessorize => true
56
57 map('/set') { run set }
58 map('/') { run get }
59 end
60
61 Rack::Handler::Mongrel.run builder, :Port => 9292
62
63 ### Sinatra
64
65 If you're using Sinatra, you can use the flash hash just like in Rails:
dea52058 »
2009-02-26 IMPORTANT: Sinatra::Flash is now Rack::Flash.
66
6293a160 »
2009-02-26 Abide by middleware dir convention
67 require 'sinatra/base'
68 require 'rack-flash'
69
21f51c5d »
2009-02-26 Implemented simple flash hash for Sinatra.
70 class MyApp < Sinatra::Base
9027f144 »
2010-01-26 Make Sinatra example in README enable sessions.
71 enable :sessions
dea52058 »
2009-02-26 IMPORTANT: Sinatra::Flash is now Rack::Flash.
72 use Rack::Flash
21f51c5d »
2009-02-26 Implemented simple flash hash for Sinatra.
73
c7f7519a »
2009-02-27 Cleaned up dynamic accessors (thanks foca), added flash.now
74 post '/set-flash' do
75 # Set a flash entry
76 flash[:notice] = "Thanks for signing up!"
77
78 # Get a flash entry
79 flash[:notice] # => "Thanks for signing up!"
80
81 # Set a flash entry for only the current request
82 flash.now[:notice] = "Thanks for signing up!"
21f51c5d »
2009-02-26 Implemented simple flash hash for Sinatra.
83 end
c7f7519a »
2009-02-27 Cleaned up dynamic accessors (thanks foca), added flash.now
84 end
21f51c5d »
2009-02-26 Implemented simple flash hash for Sinatra.
85
c7f7519a »
2009-02-27 Cleaned up dynamic accessors (thanks foca), added flash.now
86 If you've got any ideas on how to simplify access to the flash hash for vanilla
87 Rack apps, let me know. It still feels a bit off to me.
21f51c5d »
2009-02-26 Implemented simple flash hash for Sinatra.
88
c7f7519a »
2009-02-27 Cleaned up dynamic accessors (thanks foca), added flash.now
89 ## Sweeping stale entries
90
91 By default Rack::Flash has slightly different behavior than Rails in that it
92 doesn't delete entries until they are used. If you want entries to be cleared
93 even if they are not ever accessed, you can use the `:sweep` option:
94
95 use Rack::Flash, :sweep => true
21f51c5d »
2009-02-26 Implemented simple flash hash for Sinatra.
96
9027f144 »
2010-01-26 Make Sinatra example in README enable sessions.
97 This will sweep stale flash entries, whether or not you actually use them.
Something went wrong with that request. Please try again.