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
- 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.
- .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
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
- #each(&block) ⇒ Object
- #empty? ⇒ 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
-
#initialize(by, req) ⇒ Session
constructor
A new instance of Session.
- #inspect ⇒ Object
-
#keys ⇒ Object
Returns keys of the session as Array.
- #loaded? ⇒ Boolean
- #merge!(other) ⇒ Object
- #options ⇒ Object
-
#to_hash ⇒ Object
Returns the session as Hash.
-
#update(hash) ⇒ Object
Updates the session with given Hash.
-
#values ⇒ Object
Returns values of the session as Array.
Constructor Details
#initialize(by, req) ⇒ Session
Returns a new instance of Session.
61 62 63 64 65 66 67 |
# File 'lib/action_dispatch/request/session.rb', line 61 def initialize(by, req) @by = by @req = req @delegate = {} @loaded = false @exists = nil # we haven't checked yet end |
Class Method Details
.create(store, req, default_options) ⇒ Object
Creates a session hash, merging the properties of the previous session if any
14 15 16 17 18 19 20 21 22 |
# File 'lib/action_dispatch/request/session.rb', line 14 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 |
.find(req) ⇒ Object
24 25 26 |
# File 'lib/action_dispatch/request/session.rb', line 24 def self.find(req) req.get_header ENV_SESSION_KEY end |
.set(req, session) ⇒ Object
28 29 30 |
# File 'lib/action_dispatch/request/session.rb', line 28 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.
89 90 91 92 |
# File 'lib/action_dispatch/request/session.rb', line 89 def [](key) load_for_read! @delegate[key.to_s] end |
#[]=(key, value) ⇒ Object
Writes given value to given key of the session.
113 114 115 116 |
# File 'lib/action_dispatch/request/session.rb', line 113 def []=(key, value) load_for_write! @delegate[key.to_s] = value end |
#clear ⇒ Object
Clears the session.
119 120 121 122 |
# File 'lib/action_dispatch/request/session.rb', line 119 def clear load_for_write! @delegate.clear end |
#delete(key) ⇒ Object
Deletes given key from the session.
146 147 148 149 |
# File 'lib/action_dispatch/request/session.rb', line 146 def delete(key) load_for_write! @delegate.delete key.to_s end |
#destroy ⇒ Object
77 78 79 80 81 82 83 84 85 |
# File 'lib/action_dispatch/request/session.rb', line 77 def destroy clear = self. || {} @by.send(:delete_session, @req, .id(@req), ) # Load the new sid to be written with the response @loaded = false load_for_write! end |
#each(&block) ⇒ Object
201 202 203 |
# File 'lib/action_dispatch/request/session.rb', line 201 def each(&block) to_hash.each(&block) end |
#empty? ⇒ Boolean
191 192 193 194 |
# File 'lib/action_dispatch/request/session.rb', line 191 def empty? load_for_read! @delegate.empty? end |
#exists? ⇒ Boolean
182 183 184 185 |
# File 'lib/action_dispatch/request/session.rb', line 182 def exists? 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
165 166 167 168 169 170 171 172 |
# File 'lib/action_dispatch/request/session.rb', line 165 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.
95 96 97 98 |
# File 'lib/action_dispatch/request/session.rb', line 95 def has_key?(key) load_for_read! @delegate.key?(key.to_s) end |
#id ⇒ Object
69 70 71 |
# File 'lib/action_dispatch/request/session.rb', line 69 def id .id(@req) end |
#inspect ⇒ Object
174 175 176 177 178 179 180 |
# File 'lib/action_dispatch/request/session.rb', line 174 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.
103 104 105 |
# File 'lib/action_dispatch/request/session.rb', line 103 def keys @delegate.keys end |
#loaded? ⇒ Boolean
187 188 189 |
# File 'lib/action_dispatch/request/session.rb', line 187 def loaded? @loaded end |
#merge!(other) ⇒ Object
196 197 198 199 |
# File 'lib/action_dispatch/request/session.rb', line 196 def merge!(other) load_for_write! @delegate.merge!(other) end |
#options ⇒ Object
73 74 75 |
# File 'lib/action_dispatch/request/session.rb', line 73 def Options.find @req end |
#to_hash ⇒ Object
Returns the session as Hash.
125 126 127 128 |
# File 'lib/action_dispatch/request/session.rb', line 125 def to_hash load_for_read! @delegate.dup.delete_if { |_, v| v.nil? } end |
#update(hash) ⇒ Object
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"}
140 141 142 143 |
# File 'lib/action_dispatch/request/session.rb', line 140 def update(hash) load_for_write! @delegate.update stringify_keys(hash) end |
#values ⇒ Object
Returns values of the session as Array.
108 109 110 |
# File 'lib/action_dispatch/request/session.rb', line 108 def values @delegate.values end |