class RodaSessionMiddleware::SessionHash
Class to hold session data. This is designed to mimic the API of Rack::Session::Abstract::SessionHash, but is simpler and faster. Undocumented methods operate the same as hash methods, but load the session from the cookie if it hasn’t been loaded yet, and convert keys to strings.
One difference between SessionHash
and Rack::Session::Abstract::SessionHash is that SessionHash
does not attempt to setup a session id, since one is not needed for cookie-based sessions, only for sessions that are loaded out of a database. If you need to have a session id for other reasons, manually create a session id using a randomly generated string.
Attributes
The underlying data hash, or nil if the session has not yet been loaded.
The Roda::RodaRequest
subclass instance related to the session.
Public Class Methods
Public Instance Methods
Source
# File lib/roda/session_middleware.rb, line 45 def [](key) load! @data[key.to_s] end
Source
# File lib/roda/session_middleware.rb, line 66 def []=(key, value) load! @data[key.to_s] = value end
Source
# File lib/roda/session_middleware.rb, line 75 def clear load! env = @req.env env.delete('roda.session.created_at') env.delete('roda.session.updated_at') @data.clear end
Clear the session, also removing a couple of roda session keys from the environment so that the related cookie will either be set or cleared in the rack response.
Source
# File lib/roda/session_middleware.rb, line 104 def delete(key) load! @data.delete(key.to_s) end
Source
# File lib/roda/session_middleware.rb, line 40 def each(&block) load! @data.each(&block) end
Source
# File lib/roda/session_middleware.rb, line 120 def exists? load! req.env.has_key?('roda.session.serialized') end
Return whether the session cookie already exists. If this is false, then the session was set to an empty hash.
Source
# File lib/roda/session_middleware.rb, line 50 def fetch(key, default = (no_default = true), &block) load! if no_default @data.fetch(key.to_s, &block) else @data.fetch(key.to_s, default, &block) end end
Source
# File lib/roda/session_middleware.rb, line 59 def has_key?(key) load! @data.has_key?(key.to_s) end
Source
# File lib/roda/session_middleware.rb, line 110 def inspect if loaded? @data.inspect else "#<#{self.class}:0x#{self.object_id.to_s(16)} not yet loaded>" end end
If the session hasn’t been loaded, display that.
Source
# File lib/roda/session_middleware.rb, line 126 def loaded? !!defined?(@data) end
Whether the session has already been loaded from the cookie yet.
Source
# File lib/roda/session_middleware.rb, line 36 def options @req.roda_class.opts[:sessions] end
The Roda
sessions plugin options used by the middleware for this session hash.
Source
# File lib/roda/session_middleware.rb, line 98 def replace(hash) load! @data.clear update(hash) end
Source
# File lib/roda/session_middleware.rb, line 89 def update(hash) load! hash.each do |key, value| @data[key.to_s] = value end @data end
Private Instance Methods
Source
# File lib/roda/session_middleware.rb, line 148 def load! @data ||= @req.send(:_load_session) end
Load the session from the cookie.