nakajima-rack-flash 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/rack-flash.rb +94 -0
- metadata +62 -0
data/lib/rack-flash.rb
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
module Rack
|
2
|
+
class Flash
|
3
|
+
class SessionUnavailable < StandardError; end
|
4
|
+
|
5
|
+
# Implements bracket accessors for storing and retrieving flash entries.
|
6
|
+
class FlashHash
|
7
|
+
attr_reader :flagged
|
8
|
+
|
9
|
+
def initialize(store)
|
10
|
+
@store = store
|
11
|
+
@store[:__FLASH__] ||= {}
|
12
|
+
end
|
13
|
+
|
14
|
+
# Remove an entry from the session and return its value. Cache result in
|
15
|
+
# the instance cache.
|
16
|
+
def [](key)
|
17
|
+
get(key.to_sym)
|
18
|
+
end
|
19
|
+
|
20
|
+
# Store the entry in the session, updating the instance cache as well.
|
21
|
+
def []=(key,val)
|
22
|
+
set(key.to_sym, val)
|
23
|
+
end
|
24
|
+
|
25
|
+
# Checks for the presence of a flash entry without retrieving or removing
|
26
|
+
# it from the cache or store.
|
27
|
+
def has?(key)
|
28
|
+
[cache, values].any? { |store| store.keys.include?(key.to_sym) }
|
29
|
+
end
|
30
|
+
|
31
|
+
# Mark existing entries to allow for sweeping.
|
32
|
+
def flag!
|
33
|
+
@flagged = values.keys
|
34
|
+
end
|
35
|
+
|
36
|
+
# Remove flagged entries from flash session, clear flagged list.
|
37
|
+
def sweep!
|
38
|
+
Array(flagged).each { |key| values.delete(key) }
|
39
|
+
flagged.clear
|
40
|
+
end
|
41
|
+
|
42
|
+
# Hide the underlying :__FLASH__ session key and only expose values stored
|
43
|
+
# in the flash.
|
44
|
+
def inspect
|
45
|
+
'#<FlashHash @values=%s>' % [values.inspect]
|
46
|
+
end
|
47
|
+
|
48
|
+
# Human readable for logging.
|
49
|
+
def to_s
|
50
|
+
values.inspect
|
51
|
+
end
|
52
|
+
|
53
|
+
private
|
54
|
+
|
55
|
+
def get(key)
|
56
|
+
raise ArgumentError.new("Flash key must be symbol.") unless key.is_a?(Symbol)
|
57
|
+
cache[key] ||= values.delete(key)
|
58
|
+
end
|
59
|
+
|
60
|
+
def set(key, val)
|
61
|
+
raise ArgumentError.new("Flash key must be symbol.") unless key.is_a?(Symbol)
|
62
|
+
cache[key] = values[key] = val
|
63
|
+
end
|
64
|
+
|
65
|
+
# Maintain an instance-level cache of retrieved flash entries. These entries
|
66
|
+
# will have been removed from the session, but are still available through
|
67
|
+
# the cache.
|
68
|
+
def cache
|
69
|
+
@cache ||= {}
|
70
|
+
end
|
71
|
+
|
72
|
+
# Helper to access flash entries from :__FLASH__ session value. This key
|
73
|
+
# is used to prevent collisions with other user-defined session values.
|
74
|
+
def values
|
75
|
+
@store[:__FLASH__]
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
def initialize(app)
|
80
|
+
@app = app
|
81
|
+
@app.class.class_eval do
|
82
|
+
def flash
|
83
|
+
raise Rack::Flash::SessionUnavailable \
|
84
|
+
.new('Rack::Flash depends on session middleware.') unless env['rack.session']
|
85
|
+
@flash ||= Rack::Flash::FlashHash.new(env['rack.session'])
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
def call(env)
|
91
|
+
@app.call(env)
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
metadata
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: nakajima-rack-flash
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Pat Nakajima
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-02-26 00:00:00 -08:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: rack
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
version:
|
25
|
+
description:
|
26
|
+
email: patnakajima@gmail.com
|
27
|
+
executables: []
|
28
|
+
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files: []
|
32
|
+
|
33
|
+
files:
|
34
|
+
- lib/rack-flash.rb
|
35
|
+
has_rdoc: false
|
36
|
+
homepage:
|
37
|
+
post_install_message:
|
38
|
+
rdoc_options: []
|
39
|
+
|
40
|
+
require_paths:
|
41
|
+
- lib
|
42
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: "0"
|
47
|
+
version:
|
48
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: "0"
|
53
|
+
version:
|
54
|
+
requirements: []
|
55
|
+
|
56
|
+
rubyforge_project:
|
57
|
+
rubygems_version: 1.2.0
|
58
|
+
signing_key:
|
59
|
+
specification_version: 2
|
60
|
+
summary: Flash hash implementation for Rack apps.
|
61
|
+
test_files: []
|
62
|
+
|