Class: ActionDispatch::Request::Session
Relationships & Source Files | |
Namespace Children | |
Classes:
| |
Inherits: | Object |
Defined in: | actionpack/lib/action_dispatch/request/session.rb |
Overview
Session
is responsible for lazily loading the session from store.
Constant Summary
-
DisabledSessionError =
# File 'actionpack/lib/action_dispatch/request/session.rb', line 11Class.new(StandardError)
-
ENV_SESSION_KEY =
# File 'actionpack/lib/action_dispatch/request/session.rb', line 12Rack::RACK_SESSION
-
ENV_SESSION_OPTIONS_KEY =
# File 'actionpack/lib/action_dispatch/request/session.rb', line 13Rack::RACK_SESSION_OPTIONS
-
Unspecified =
::Singleton
object used to determine if an optional param wasn’t specified.Object.new
Class Method Summary
-
.create(store, req, default_options)
Creates a session hash, merging the properties of the previous session if any.
- .delete(req)
- .disabled(req)
- .find(req)
- .new(by, req, enabled: true) ⇒ Session constructor
- .set(req, session)
Instance Attribute Summary
- #empty? ⇒ Boolean readonly
- #enabled? ⇒ Boolean readonly
- #exists? ⇒ Boolean readonly
- #loaded? ⇒ Boolean readonly
Instance Method Summary
-
#[](key)
Returns value of the key stored in the session or
nil
if the given key is not found in the session. -
#[]=(key, value)
Writes given value to given key of the session.
-
#clear
Clears the session.
-
#delete(key)
Deletes given key from the session.
- #destroy
-
#dig(*keys)
Returns the nested value specified by the sequence of keys, returning
nil
if any intermediate step isnil
. - #each(&block)
-
#fetch(key, default = Unspecified, &block)
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
- #id_was
-
#include?(key)
Alias for #has_key?.
- #inspect
-
#key?(key)
Alias for #has_key?.
-
#keys
Returns keys of the session as
::Array
. -
#merge!(hash)
Alias for #update.
- #options
-
#to_h
Alias for #to_hash.
-
#to_hash
(also: #to_h)
Returns the session as
::Hash
. -
#update(hash)
(also: #merge!)
Updates the session with given
::Hash
. -
#values
Returns values of the session as
::Array
. - #load! private
- #load_for_delete! private
- #load_for_read! private
- #load_for_write! private
Constructor Details
.new(by, req, enabled: true) ⇒ Session
# File 'actionpack/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)
Creates a session hash, merging the properties of the previous session if any.
.delete(req)
[ GitHub ]# File 'actionpack/lib/action_dispatch/request/session.rb', line 43
def self.delete(req) req.delete_header ENV_SESSION_KEY end
.disabled(req)
[ GitHub ].find(req)
[ GitHub ]# File 'actionpack/lib/action_dispatch/request/session.rb', line 35
def self.find(req) req.get_header ENV_SESSION_KEY end
.set(req, session)
[ GitHub ]# File 'actionpack/lib/action_dispatch/request/session.rb', line 39
def self.set(req, session) req.set_header ENV_SESSION_KEY, session end
Instance Attribute Details
#empty? ⇒ Boolean
(readonly)
[ GitHub ]
# File 'actionpack/lib/action_dispatch/request/session.rb', line 239
def empty? load_for_read! @delegate.empty? end
#enabled? ⇒ Boolean
(readonly)
[ GitHub ]
# File 'actionpack/lib/action_dispatch/request/session.rb', line 91
def enabled? @enabled end
#exists? ⇒ Boolean
(readonly)
[ GitHub ]
# File 'actionpack/lib/action_dispatch/request/session.rb', line 229
def exists? return false unless enabled? return @exists unless @exists.nil? @exists = @by.send(:session_exists?, @req) end
#loaded? ⇒ Boolean
(readonly)
[ GitHub ]
# File 'actionpack/lib/action_dispatch/request/session.rb', line 235
def loaded? @loaded end
Instance Method Details
#[](key)
Returns value of the key stored in the session or nil
if the given key is not found in the session.
# File 'actionpack/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)
Writes given value to given key of the session.
# File 'actionpack/lib/action_dispatch/request/session.rb', line 154
def []=(key, value) load_for_write! @delegate[key.to_s] = value end
#clear
Clears the session.
# File 'actionpack/lib/action_dispatch/request/session.rb', line 160
def clear load_for_delete! @delegate.clear end
#delete(key)
Deletes given key from the session.
# File 'actionpack/lib/action_dispatch/request/session.rb', line 193
def delete(key) load_for_delete! @delegate.delete key.to_s end
#destroy
[ GitHub ]# File 'actionpack/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)
Returns the nested value specified by the sequence of keys, returning nil
if any intermediate step is nil
.
# File 'actionpack/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)
[ GitHub ]# File 'actionpack/lib/action_dispatch/request/session.rb', line 244
def each(&block) to_hash.each(&block) end
#fetch(key, default = Unspecified, &block)
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
session.fetch(:foo) do
:
end
# => :bar
# File 'actionpack/lib/action_dispatch/request/session.rb', line 212
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.
# File 'actionpack/lib/action_dispatch/request/session.rb', line 134
def has_key?(key) load_for_read! @delegate.key?(key.to_s) end
#id
[ GitHub ]# File 'actionpack/lib/action_dispatch/request/session.rb', line 87
def id .id(@req) end
#id_was
[ GitHub ]# File 'actionpack/lib/action_dispatch/request/session.rb', line 248
def id_was load_for_read! @id_was end
#include?(key)
Alias for #has_key?.
# File 'actionpack/lib/action_dispatch/request/session.rb', line 139
alias :include? :has_key?
#inspect
[ GitHub ]# File 'actionpack/lib/action_dispatch/request/session.rb', line 221
def inspect if loaded? super else "#<#{self.class}:0x#{(object_id << 1).to_s(16)} not yet loaded>" end end
#key?(key)
Alias for #has_key?.
# File 'actionpack/lib/action_dispatch/request/session.rb', line 138
alias :key? :has_key?
#keys
Returns keys of the session as ::Array
.
# File 'actionpack/lib/action_dispatch/request/session.rb', line 142
def keys load_for_read! @delegate.keys end
#load! (private)
[ GitHub ]# File 'actionpack/lib/action_dispatch/request/session.rb', line 270
def load! if enabled? @id_was_initialized = true unless exists? id, session = @by.load_session @req [:id] = id @delegate.replace(session.stringify_keys) @id_was = id unless @id_was_initialized end @id_was_initialized = true @loaded = true end
#load_for_delete! (private)
[ GitHub ]#load_for_read! (private)
[ GitHub ]#load_for_write! (private)
[ GitHub ]# File 'actionpack/lib/action_dispatch/request/session.rb', line 258
def load_for_write! if enabled? load! unless loaded? else raise DisabledSessionError, "Your application has sessions disabled. To write to the session you must first configure a session store" end end
#merge!(hash)
Alias for #update.
# File 'actionpack/lib/action_dispatch/request/session.rb', line 190
alias :merge! :update
#options
[ GitHub ]#to_h
Alias for #to_hash.
# File 'actionpack/lib/action_dispatch/request/session.rb', line 170
alias :to_h :to_hash
#to_hash Also known as: #to_h
Returns the session as ::Hash
.
# File 'actionpack/lib/action_dispatch/request/session.rb', line 166
def to_hash load_for_read! @delegate.dup.delete_if { |_, v| v.nil? } end
#update(hash) 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"}
# File 'actionpack/lib/action_dispatch/request/session.rb', line 182
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
Returns values of the session as ::Array
.
# File 'actionpack/lib/action_dispatch/request/session.rb', line 148
def values load_for_read! @delegate.values end