Skip to content
This repository
Browse code

bread AD::Request::Session to it's own file, consolidate HASH OF DOOM…

… lookups
  • Loading branch information...
commit c4f02295df74ed4e09b664dd5bf3d00a5aa9b6a4 1 parent 9aa5bb4
Aaron Patterson authored
5  actionpack/lib/action_dispatch/http/request.rb
@@ -6,6 +6,7 @@
6 6
 require 'active_support/core_ext/string/access'
7 7
 require 'active_support/inflector'
8 8
 require 'action_dispatch/http/headers'
  9
+require 'action_dispatch/request/session'
9 10
 require 'action_controller/metal/exceptions'
10 11
 
11 12
 module ActionDispatch
@@ -220,11 +221,11 @@ def reset_session
220 221
     end
221 222
 
222 223
     def session=(session) #:nodoc:
223  
-      @env['rack.session'] = session
  224
+      Session.set @env, session
224 225
     end
225 226
 
226 227
     def session_options=(options)
227  
-      @env['rack.session.options'] = options
  228
+      Session::Options.set @env, options
228 229
     end
229 230
 
230 231
     # Override Rack's GET method to support indifferent access
2  actionpack/lib/action_dispatch/middleware/flash.rb
@@ -218,7 +218,7 @@ def initialize(app)
218 218
     def call(env)
219 219
       @app.call(env)
220 220
     ensure
221  
-      session    = env['rack.session'] || {}
  221
+      session    = Request::Session.find(env) || {}
222 222
       flash_hash = env[KEY]
223 223
 
224 224
       if flash_hash
151  actionpack/lib/action_dispatch/middleware/session/abstract_store.rb
@@ -2,6 +2,7 @@
2 2
 require 'rack/request'
3 3
 require 'rack/session/abstract/id'
4 4
 require 'action_dispatch/middleware/cookies'
  5
+require 'action_dispatch/request/session'
5 6
 require 'active_support/core_ext/object/blank'
6 7
 
7 8
 module ActionDispatch
@@ -84,154 +85,4 @@ def set_cookie(env, session_id, cookie)
84 85
       end
85 86
     end
86 87
   end
87  
-
88  
-  class Request
89  
-    # SessionHash is responsible to lazily load the session from store.
90  
-    class Session # :nodoc:
91  
-      ENV_SESSION_KEY         = Rack::Session::Abstract::ENV_SESSION_KEY # :nodoc:
92  
-      ENV_SESSION_OPTIONS_KEY = Rack::Session::Abstract::ENV_SESSION_OPTIONS_KEY # :nodoc:
93  
-
94  
-      def self.create(store, env, default_options)
95  
-        session_was                  = env[ENV_SESSION_KEY]
96  
-        session                      = Request::Session.new(store, env)
97  
-        env[ENV_SESSION_KEY]         = session
98  
-        env[ENV_SESSION_OPTIONS_KEY] = Request::Session::Options.new(store, env, default_options)
99  
-        env[ENV_SESSION_KEY].merge! session_was if session_was
100  
-        session
101  
-      end
102  
-
103  
-      def self.find(env)
104  
-        env[ENV_SESSION_KEY]
105  
-      end
106  
-
107  
-      class Options #:nodoc:
108  
-        def initialize(by, env, default_options)
109  
-          @by       = by
110  
-          @env      = env
111  
-          @delegate = default_options
112  
-        end
113  
-
114  
-        def [](key)
115  
-          if key == :id
116  
-            @delegate.fetch(key) {
117  
-              @delegate[:id] = @by.send(:extract_session_id, @env)
118  
-            }
119  
-          else
120  
-            @delegate[key]
121  
-          end
122  
-        end
123  
-
124  
-        def []=(k,v);         @delegate[k] = v; end
125  
-        def to_hash;          @delegate.dup; end
126  
-        def values_at(*args); @delegate.values_at(*args); end
127  
-      end
128  
-
129  
-      def initialize(by, env)
130  
-        @by       = by
131  
-        @env      = env
132  
-        @delegate = {}
133  
-        @loaded   = false
134  
-        @exists   = nil # we haven't checked yet
135  
-      end
136  
-
137  
-      def options
138  
-        @env[ENV_SESSION_OPTIONS_KEY]
139  
-      end
140  
-
141  
-      def destroy
142  
-        clear
143  
-        options = self.options || {}
144  
-        @by.send(:destroy_session, @env, options[:id], options)
145  
-        options[:id] = nil
146  
-        @loaded = false
147  
-      end
148  
-
149  
-      def [](key)
150  
-        load_for_read!
151  
-        @delegate[key.to_s]
152  
-      end
153  
-
154  
-      def has_key?(key)
155  
-        load_for_read!
156  
-        @delegate.key?(key.to_s)
157  
-      end
158  
-      alias :key? :has_key?
159  
-      alias :include? :has_key?
160  
-
161  
-      def []=(key, value)
162  
-        load_for_write!
163  
-        @delegate[key.to_s] = value
164  
-      end
165  
-
166  
-      def clear
167  
-        load_for_write!
168  
-        @delegate.clear
169  
-      end
170  
-
171  
-      def to_hash
172  
-        load_for_read!
173  
-        @delegate.dup.delete_if { |_,v| v.nil? }
174  
-      end
175  
-
176  
-      def update(hash)
177  
-        load_for_write!
178  
-        @delegate.update stringify_keys(hash)
179  
-      end
180  
-
181  
-      def delete(key)
182  
-        load_for_write!
183  
-        @delegate.delete key.to_s
184  
-      end
185  
-
186  
-      def inspect
187  
-        if loaded?
188  
-          super
189  
-        else
190  
-          "#<#{self.class}:0x#{(object_id << 1).to_s(16)} not yet loaded>"
191  
-        end
192  
-      end
193  
-
194  
-      def exists?
195  
-        return @exists unless @exists.nil?
196  
-        @exists = @by.send(:session_exists?, @env)
197  
-      end
198  
-
199  
-      def loaded?
200  
-        @loaded
201  
-      end
202  
-
203  
-      def empty?
204  
-        load_for_read!
205  
-        @delegate.empty?
206  
-      end
207  
-
208  
-      def merge!(other)
209  
-        load_for_write!
210  
-        @delegate.merge!(other)
211  
-      end
212  
-
213  
-      private
214  
-
215  
-      def load_for_read!
216  
-        load! if !loaded? && exists?
217  
-      end
218  
-
219  
-      def load_for_write!
220  
-        load! unless loaded?
221  
-      end
222  
-
223  
-      def load!
224  
-        id, session = @by.load_session @env
225  
-        options[:id] = id
226  
-        @delegate.replace(stringify_keys(session))
227  
-        @loaded = true
228  
-      end
229  
-
230  
-      def stringify_keys(other)
231  
-        other.each_with_object({}) { |(key, value), hash|
232  
-          hash[key.to_s] = value
233  
-        }
234  
-      end
235  
-    end
236  
-  end
237 88
 end
166  actionpack/lib/action_dispatch/request/session.rb
... ...
@@ -0,0 +1,166 @@
  1
+require 'rack/session/abstract/id'
  2
+
  3
+module ActionDispatch
  4
+  class Request < Rack::Request
  5
+    # SessionHash is responsible to lazily load the session from store.
  6
+    class Session # :nodoc:
  7
+      ENV_SESSION_KEY         = Rack::Session::Abstract::ENV_SESSION_KEY # :nodoc:
  8
+      ENV_SESSION_OPTIONS_KEY = Rack::Session::Abstract::ENV_SESSION_OPTIONS_KEY # :nodoc:
  9
+
  10
+      def self.create(store, env, default_options)
  11
+        session_was = find env
  12
+        session     = Request::Session.new(store, env)
  13
+        session.merge! session_was if session_was
  14
+
  15
+        set(env, session)
  16
+        Options.set(env, Request::Session::Options.new(store, env, default_options))
  17
+        session
  18
+      end
  19
+
  20
+      def self.find(env)
  21
+        env[ENV_SESSION_KEY]
  22
+      end
  23
+
  24
+      def self.set(env, session)
  25
+        env[ENV_SESSION_KEY] = session
  26
+      end
  27
+
  28
+      class Options #:nodoc:
  29
+        def self.set(env, options)
  30
+          env[ENV_SESSION_OPTIONS_KEY] = options
  31
+        end
  32
+
  33
+        def self.find(env)
  34
+          env[ENV_SESSION_OPTIONS_KEY]
  35
+        end
  36
+
  37
+        def initialize(by, env, default_options)
  38
+          @by       = by
  39
+          @env      = env
  40
+          @delegate = default_options
  41
+        end
  42
+
  43
+        def [](key)
  44
+          if key == :id
  45
+            @delegate.fetch(key) {
  46
+              @delegate[:id] = @by.send(:extract_session_id, @env)
  47
+            }
  48
+          else
  49
+            @delegate[key]
  50
+          end
  51
+        end
  52
+
  53
+        def []=(k,v);         @delegate[k] = v; end
  54
+        def to_hash;          @delegate.dup; end
  55
+        def values_at(*args); @delegate.values_at(*args); end
  56
+      end
  57
+
  58
+      def initialize(by, env)
  59
+        @by       = by
  60
+        @env      = env
  61
+        @delegate = {}
  62
+        @loaded   = false
  63
+        @exists   = nil # we haven't checked yet
  64
+      end
  65
+
  66
+      def options
  67
+        Options.find @env
  68
+      end
  69
+
  70
+      def destroy
  71
+        clear
  72
+        options = self.options || {}
  73
+        @by.send(:destroy_session, @env, options[:id], options)
  74
+        options[:id] = nil
  75
+        @loaded = false
  76
+      end
  77
+
  78
+      def [](key)
  79
+        load_for_read!
  80
+        @delegate[key.to_s]
  81
+      end
  82
+
  83
+      def has_key?(key)
  84
+        load_for_read!
  85
+        @delegate.key?(key.to_s)
  86
+      end
  87
+      alias :key? :has_key?
  88
+      alias :include? :has_key?
  89
+
  90
+      def []=(key, value)
  91
+        load_for_write!
  92
+        @delegate[key.to_s] = value
  93
+      end
  94
+
  95
+      def clear
  96
+        load_for_write!
  97
+        @delegate.clear
  98
+      end
  99
+
  100
+      def to_hash
  101
+        load_for_read!
  102
+        @delegate.dup.delete_if { |_,v| v.nil? }
  103
+      end
  104
+
  105
+      def update(hash)
  106
+        load_for_write!
  107
+        @delegate.update stringify_keys(hash)
  108
+      end
  109
+
  110
+      def delete(key)
  111
+        load_for_write!
  112
+        @delegate.delete key.to_s
  113
+      end
  114
+
  115
+      def inspect
  116
+        if loaded?
  117
+          super
  118
+        else
  119
+          "#<#{self.class}:0x#{(object_id << 1).to_s(16)} not yet loaded>"
  120
+        end
  121
+      end
  122
+
  123
+      def exists?
  124
+        return @exists unless @exists.nil?
  125
+        @exists = @by.send(:session_exists?, @env)
  126
+      end
  127
+
  128
+      def loaded?
  129
+        @loaded
  130
+      end
  131
+
  132
+      def empty?
  133
+        load_for_read!
  134
+        @delegate.empty?
  135
+      end
  136
+
  137
+      def merge!(other)
  138
+        load_for_write!
  139
+        @delegate.merge!(other)
  140
+      end
  141
+
  142
+      private
  143
+
  144
+      def load_for_read!
  145
+        load! if !loaded? && exists?
  146
+      end
  147
+
  148
+      def load_for_write!
  149
+        load! unless loaded?
  150
+      end
  151
+
  152
+      def load!
  153
+        id, session = @by.load_session @env
  154
+        options[:id] = id
  155
+        @delegate.replace(stringify_keys(session))
  156
+        @loaded = true
  157
+      end
  158
+
  159
+      def stringify_keys(other)
  160
+        other.each_with_object({}) { |(key, value), hash|
  161
+          hash[key.to_s] = value
  162
+        }
  163
+      end
  164
+    end
  165
+  end
  166
+end

0 notes on commit c4f0229

Please sign in to comment.
Something went wrong with that request. Please try again.