nakajima-rack-flash 0.0.2 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/rack/flash.rb +125 -0
  2. data/lib/rack-flash.rb +1 -94
  3. metadata +12 -1
data/lib/rack/flash.rb ADDED
@@ -0,0 +1,125 @@
1
+ require 'metaid'
2
+
3
+ module Rack
4
+ class Flash
5
+ class SessionUnavailable < StandardError; end
6
+
7
+ # Implements bracket accessors for storing and retrieving flash entries.
8
+ class FlashHash
9
+ attr_reader :flagged
10
+
11
+ def initialize(store, opts={})
12
+ raise Rack::Flash::SessionUnavailable \
13
+ .new('Rack::Flash depends on session middleware.') unless store
14
+
15
+ @opts = opts
16
+ @store = store
17
+ @store[:__FLASH__] ||= {}
18
+ end
19
+
20
+ # Remove an entry from the session and return its value. Cache result in
21
+ # the instance cache.
22
+ def [](key)
23
+ get(key.to_sym)
24
+ end
25
+
26
+ # Store the entry in the session, updating the instance cache as well.
27
+ def []=(key,val)
28
+ set(key.to_sym, val)
29
+ end
30
+
31
+ # Checks for the presence of a flash entry without retrieving or removing
32
+ # it from the cache or store.
33
+ def has?(key)
34
+ [cache, values].any? { |store| store.keys.include?(key.to_sym) }
35
+ end
36
+
37
+ # Mark existing entries to allow for sweeping.
38
+ def flag!
39
+ @flagged = values.keys
40
+ end
41
+
42
+ # Remove flagged entries from flash session, clear flagged list.
43
+ def sweep!
44
+ Array(flagged).each { |key| values.delete(key) }
45
+ flagged.clear
46
+ end
47
+
48
+ # Hide the underlying :__FLASH__ session key and only expose values stored
49
+ # in the flash.
50
+ def inspect
51
+ '#<FlashHash @values=%s>' % [values.inspect]
52
+ end
53
+
54
+ # Human readable for logging.
55
+ def to_s
56
+ values.inspect
57
+ end
58
+
59
+ # Allow more convenient style for accessing flash entries (This isn't really
60
+ # necessary for Sinatra, since it provides the flash[:foo] hash that we're all
61
+ # used to. This is for vanilla Rack apps where it can be difficult to define
62
+ # such helpers as middleware).
63
+ def method_missing(sym, *args)
64
+ super unless @opts[:accessorize]
65
+ key = sym.to_s =~ /\w=$/ ? sym.to_s[0..-2] : sym
66
+ def_accessors(key)
67
+ send(sym, *args)
68
+ end
69
+
70
+ private
71
+
72
+ def get(key)
73
+ raise ArgumentError.new("Flash key must be symbol.") unless key.is_a?(Symbol)
74
+ cache[key] ||= values.delete(key)
75
+ end
76
+
77
+ def set(key, val)
78
+ raise ArgumentError.new("Flash key must be symbol.") unless key.is_a?(Symbol)
79
+ cache[key] = values[key] = val
80
+ end
81
+
82
+ def def_accessors(key)
83
+ return if respond_to?(key)
84
+
85
+ meta_def(key) do
86
+ self[key]
87
+ end
88
+
89
+ meta_def("#{key}=") do |val|
90
+ self[key] = val
91
+ end
92
+ end
93
+
94
+ # Maintain an instance-level cache of retrieved flash entries. These entries
95
+ # will have been removed from the session, but are still available through
96
+ # the cache.
97
+ def cache
98
+ @cache ||= {}
99
+ end
100
+
101
+ # Helper to access flash entries from :__FLASH__ session value. This key
102
+ # is used to prevent collisions with other user-defined session values.
103
+ def values
104
+ @store[:__FLASH__]
105
+ end
106
+ end
107
+
108
+ def initialize(app, opts={})
109
+ @app, @opts = app, opts
110
+ end
111
+
112
+ def call(env)
113
+ env['rack-flash'] = Rack::Flash::FlashHash.new(env['rack.session'], @opts)
114
+ @app.call(env)
115
+ end
116
+ end
117
+ end
118
+
119
+ if defined?(Sinatra::Base)
120
+ Sinatra::Base.class_eval do
121
+ def flash
122
+ env['rack-flash']
123
+ end
124
+ end
125
+ end
data/lib/rack-flash.rb CHANGED
@@ -1,94 +1 @@
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
1
+ require File.join(File.dirname(__FILE__), *%w[rack flash])
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nakajima-rack-flash
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pat Nakajima
@@ -22,6 +22,16 @@ dependencies:
22
22
  - !ruby/object:Gem::Version
23
23
  version: "0"
24
24
  version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: metaid
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: "0"
34
+ version:
25
35
  description:
26
36
  email: patnakajima@gmail.com
27
37
  executables: []
@@ -32,6 +42,7 @@ extra_rdoc_files: []
32
42
 
33
43
  files:
34
44
  - lib/rack-flash.rb
45
+ - lib/rack/flash.rb
35
46
  has_rdoc: false
36
47
  homepage:
37
48
  post_install_message: