Class: ActionDispatch::Request::Session
- Inherits:
-
Object
- Object
- ActionDispatch::Request::Session
- Defined in:
- lib/action_dispatch/request/session.rb
Overview
Session is responsible for lazily loading the session from store.
Defined Under Namespace
Classes: Options
Constant Summary collapse
- DisabledSessionError =
:nodoc:
Class.new(StandardError)
- ENV_SESSION_KEY =
:nodoc:
Rack::RACK_SESSION
- ENV_SESSION_OPTIONS_KEY =
:nodoc:
Rack::RACK_SESSION_OPTIONS
- Unspecified =
Singleton object used to determine if an optional param wasn’t specified.
Object.new
Class Method Summary collapse
-
.create(store, req, default_options) ⇒ Object
Creates a session hash, merging the properties of the previous session if any.
- .delete(req) ⇒ Object
- .disabled(req) ⇒ Object
- .find(req) ⇒ Object
- .set(req, session) ⇒ Object
Instance Method Summary collapse
-
#[](key) ⇒ Object
Returns value of the key stored in the session or ‘nil` if the given key is not found in the session.
-
#[]=(key, value) ⇒ Object
(also: #store)
Writes given value to given key of the session.
-
#clear ⇒ Object
Clears the session.
-
#delete(key) ⇒ Object
Deletes given key from the session.
- #destroy ⇒ Object
-
#dig(*keys) ⇒ Object
Returns the nested value specified by the sequence of keys, returning ‘nil` if any intermediate step is `nil`.
- #each(&block) ⇒ Object
- #empty? ⇒ Boolean
- #enabled? ⇒ Boolean
- #exists? ⇒ Boolean
-
#fetch(key, default = Unspecified, &block) ⇒ Object
Returns value of the given key from the session, or raises ‘KeyError` if can’t find the given key and no default value is set.
-
#has_key?(key) ⇒ Boolean
(also: #key?, #include?)
Returns true if the session has the given key or false.
- #id ⇒ Object
- #id_was ⇒ Object
-
#initialize(by, req, enabled: true) ⇒ Session
constructor
A new instance of Session.
- #inspect ⇒ Object
-
#keys ⇒ Object
Returns keys of the session as Array.
- #loaded? ⇒ Boolean
- #options ⇒ Object
-
#to_hash ⇒ Object
(also: #to_h)
Returns the session as Hash.
-
#update(hash) ⇒ Object
(also: #merge!)
Updates the session with given Hash.
-
#values ⇒ Object
Returns values of the session as Array.
Constructor Details
#initialize(by, req, enabled: true) ⇒ Session
Returns a new instance of Session.
76 77 78 79 80 81 82 83 84 85 |
# File 'lib/action_dispatch/request/session.rb', line 76 def initialize(by, req, enabled: true) @by = by @req = req @delegate = {} @loaded = false @exists = nil # We haven't checked yet. @enabled = enabled @id_was = nil @id_was_initialized = false end |
Class Method Details
.create(store, req, default_options) ⇒ Object
Creates a session hash, merging the properties of the previous session if any.
19 20 21 22 23 24 25 26 27 |
# File 'lib/action_dispatch/request/session.rb', line 19 def self.create(store, req, ) session_was = find req session = Request::Session.new(store, req) session.merge! session_was if session_was set(req, session) Options.set(req, Request::Session::Options.new(store, )) session end |
.delete(req) ⇒ Object
43 44 45 |
# File 'lib/action_dispatch/request/session.rb', line 43 def self.delete(req) req.delete_header ENV_SESSION_KEY end |
.disabled(req) ⇒ Object
29 30 31 32 33 |
# File 'lib/action_dispatch/request/session.rb', line 29 def self.disabled(req) new(nil, req, enabled: false).tap do Session::Options.set(req, Session::Options.new(nil, { id: nil })) end end |
.find(req) ⇒ Object
35 36 37 |
# File 'lib/action_dispatch/request/session.rb', line 35 def self.find(req) req.get_header ENV_SESSION_KEY end |
.set(req, session) ⇒ Object
39 40 41 |
# File 'lib/action_dispatch/request/session.rb', line 39 def self.set(req, session) req.set_header ENV_SESSION_KEY, session end |
Instance Method Details
#[](key) ⇒ Object
Returns value of the key stored in the session or ‘nil` if the given key is not found in the session.
114 115 116 117 118 119 120 121 122 123 |
# File 'lib/action_dispatch/request/session.rb', line 114 def [](key) load_for_read! key = key.to_s if key == "session_id" id&.public_id else @delegate[key] end end |
#[]=(key, value) ⇒ Object Also known as: store
Writes given value to given key of the session.
154 155 156 157 |
# File 'lib/action_dispatch/request/session.rb', line 154 def []=(key, value) load_for_write! @delegate[key.to_s] = value end |
#clear ⇒ Object
Clears the session.
161 162 163 164 |
# File 'lib/action_dispatch/request/session.rb', line 161 def clear load_for_delete! @delegate.clear end |
#delete(key) ⇒ Object
Deletes given key from the session.
194 195 196 197 |
# File 'lib/action_dispatch/request/session.rb', line 194 def delete(key) load_for_delete! @delegate.delete key.to_s end |
#destroy ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/action_dispatch/request/session.rb', line 99 def destroy clear if enabled? = self. || {} @by.send(:delete_session, @req, .id(@req), ) # Load the new sid to be written with the response. @loaded = false load_for_write! end end |
#dig(*keys) ⇒ Object
Returns the nested value specified by the sequence of keys, returning ‘nil` if any intermediate step is `nil`.
127 128 129 130 131 |
# File 'lib/action_dispatch/request/session.rb', line 127 def dig(*keys) load_for_read! keys = keys.map.with_index { |key, i| i.zero? ? key.to_s : key } @delegate.dig(*keys) end |
#each(&block) ⇒ Object
245 246 247 |
# File 'lib/action_dispatch/request/session.rb', line 245 def each(&block) to_hash.each(&block) end |
#empty? ⇒ Boolean
240 241 242 243 |
# File 'lib/action_dispatch/request/session.rb', line 240 def empty? load_for_read! @delegate.empty? end |
#enabled? ⇒ Boolean
91 92 93 |
# File 'lib/action_dispatch/request/session.rb', line 91 def enabled? @enabled end |
#exists? ⇒ Boolean
230 231 232 233 234 |
# File 'lib/action_dispatch/request/session.rb', line 230 def exists? return false unless enabled? return @exists unless @exists.nil? @exists = @by.send(:session_exists?, @req) end |
#fetch(key, default = Unspecified, &block) ⇒ Object
Returns value of the given key from the session, or raises ‘KeyError` if can’t find the given key and no default value is set. Returns default value if specified.
session.fetch(:foo)
# => KeyError: key not found: "foo"
session.fetch(:foo, :bar)
# => :bar
session.fetch(:foo) do
:bar
end
# => :bar
213 214 215 216 217 218 219 220 |
# File 'lib/action_dispatch/request/session.rb', line 213 def fetch(key, default = Unspecified, &block) load_for_read! if default == Unspecified @delegate.fetch(key.to_s, &block) else @delegate.fetch(key.to_s, default, &block) end end |
#has_key?(key) ⇒ Boolean Also known as: key?, include?
Returns true if the session has the given key or false.
134 135 136 137 |
# File 'lib/action_dispatch/request/session.rb', line 134 def has_key?(key) load_for_read! @delegate.key?(key.to_s) end |
#id ⇒ Object
87 88 89 |
# File 'lib/action_dispatch/request/session.rb', line 87 def id .id(@req) end |
#id_was ⇒ Object
249 250 251 252 |
# File 'lib/action_dispatch/request/session.rb', line 249 def id_was load_for_read! @id_was end |
#inspect ⇒ Object
222 223 224 225 226 227 228 |
# File 'lib/action_dispatch/request/session.rb', line 222 def inspect if loaded? super else "#<#{self.class}:0x#{(object_id << 1).to_s(16)} not yet loaded>" end end |
#keys ⇒ Object
Returns keys of the session as Array.
142 143 144 145 |
# File 'lib/action_dispatch/request/session.rb', line 142 def keys load_for_read! @delegate.keys end |
#loaded? ⇒ Boolean
236 237 238 |
# File 'lib/action_dispatch/request/session.rb', line 236 def loaded? @loaded end |
#options ⇒ Object
95 96 97 |
# File 'lib/action_dispatch/request/session.rb', line 95 def Options.find @req end |
#to_hash ⇒ Object Also known as: to_h
Returns the session as Hash.
167 168 169 170 |
# File 'lib/action_dispatch/request/session.rb', line 167 def to_hash load_for_read! @delegate.dup.delete_if { |_, v| v.nil? } end |
#update(hash) ⇒ Object Also known as: merge!
Updates the session with given Hash.
session.to_hash
# => {"session_id"=>"e29b9ea315edf98aad94cc78c34cc9b2"}
session.update({ "foo" => "bar" })
# => {"session_id"=>"e29b9ea315edf98aad94cc78c34cc9b2", "foo" => "bar"}
session.to_hash
# => {"session_id"=>"e29b9ea315edf98aad94cc78c34cc9b2", "foo" => "bar"}
183 184 185 186 187 188 189 190 |
# File 'lib/action_dispatch/request/session.rb', line 183 def update(hash) unless hash.respond_to?(:to_hash) raise TypeError, "no implicit conversion of #{hash.class.name} into Hash" end load_for_write! @delegate.update hash.to_hash.stringify_keys end |
#values ⇒ Object
Returns values of the session as Array.
148 149 150 151 |
# File 'lib/action_dispatch/request/session.rb', line 148 def values load_for_read! @delegate.values end |