File: README.md

package info (click to toggle)
ruby-rack-flash3 1.0.5-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, sid, stretch
  • size: 160 kB
  • ctags: 52
  • sloc: ruby: 296; makefile: 7
file content (110 lines) | stat: -rw-r--r-- 2,917 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# 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.