nakajima-rack-flash 0.0.6 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/rack/flash.rb +45 -7
  2. metadata +1 -1
data/lib/rack/flash.rb CHANGED
@@ -1,3 +1,31 @@
1
+ module Rack
2
+ class Builder
3
+ attr :ins
4
+ def use(middleware, *args, &block)
5
+ middleware.instance_variable_set "@rack_builder", self
6
+ def middleware.rack_builder
7
+ @rack_builder
8
+ end
9
+ @ins << lambda { |app|
10
+ middleware.new(app, *args, &block)
11
+ }
12
+ end
13
+
14
+ def run(app)
15
+ klass = app.class
16
+ klass.instance_variable_set "@rack_builder", self
17
+ def klass.rack_builder
18
+ @rack_builder
19
+ end
20
+ @ins << app #lambda { |nothing| app }
21
+ end
22
+
23
+ def leaf_app
24
+ ins.last
25
+ end
26
+ end
27
+ end
28
+
1
29
  module Rack
2
30
  class Flash
3
31
  # Raised when the session passed to FlashHash initialize is nil. This
@@ -46,6 +74,7 @@ module Rack
46
74
  def has?(key)
47
75
  [cache, values].any? { |store| store.keys.include?(key.to_sym) }
48
76
  end
77
+ alias_method :include?, :has?
49
78
 
50
79
  # Mark existing entries to allow for sweeping.
51
80
  def flag!
@@ -61,7 +90,7 @@ module Rack
61
90
  # Hide the underlying :__FLASH__ session key and only expose values stored
62
91
  # in the flash.
63
92
  def inspect
64
- '#<FlashHash @values=%s>' % [values.inspect]
93
+ '#<FlashHash @values=%s @cache=%s>' % [values.inspect, cache.inspect]
65
94
  end
66
95
 
67
96
  # Human readable for logging.
@@ -100,9 +129,9 @@ module Rack
100
129
  # - Rack Middleware implementation
101
130
 
102
131
  def initialize(app, opts={})
103
- if defined?(Sinatra::Base)
104
- Sinatra::Base.class_eval do
105
- def flash; env['rack-flash'] end
132
+ if klass = app_class(app, opts)
133
+ klass.class_eval do
134
+ def flash; env['x-rack.flash'] end
106
135
  end
107
136
  end
108
137
 
@@ -110,19 +139,28 @@ module Rack
110
139
  end
111
140
 
112
141
  def call(env)
113
- env['rack-flash'] ||= Rack::Flash::FlashHash.new(env['rack.session'], @opts)
142
+ env['x-rack.flash'] ||= Rack::Flash::FlashHash.new(env['rack.session'], @opts)
114
143
 
115
144
  if @opts[:sweep]
116
- env['rack-flash'].flag!
145
+ env['x-rack.flash'].flag!
117
146
  end
118
147
 
119
148
  res = @app.call(env)
120
149
 
121
150
  if @opts[:sweep]
122
- env['rack-flash'].sweep!
151
+ env['x-rack.flash'].sweep!
123
152
  end
124
153
 
125
154
  res
126
155
  end
156
+
157
+ private
158
+
159
+ def app_class(app, opts)
160
+ return nil if opts.has_key?(:helper) and not opts[:helper]
161
+ opts[:flash_app_class] ||
162
+ defined?(Sinatra::Base) && Sinatra::Base ||
163
+ self.class.rack_builder.leaf_app.class
164
+ end
127
165
  end
128
166
  end
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.6
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pat Nakajima