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.
Methods
Public Class
Public Instance
Public Instance Aliases
Attributes
data | [R] |
The underlying data hash, or nil if the session has not yet been loaded. |
req | [R] |
The |
Public Class methods
# File lib/roda/session_middleware.rb 30 def initialize(req) 31 @req = req 32 end
Public Instance methods
# File lib/roda/session_middleware.rb 45 def [](key) 46 load! 47 @data[key.to_s] 48 end
# File lib/roda/session_middleware.rb 66 def []=(key, value) 67 load! 68 @data[key.to_s] = value 69 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.
# File lib/roda/session_middleware.rb 75 def clear 76 load! 77 env = @req.env 78 env.delete('roda.session.created_at') 79 env.delete('roda.session.updated_at') 80 @data.clear 81 end
# File lib/roda/session_middleware.rb 104 def delete(key) 105 load! 106 @data.delete(key.to_s) 107 end
# File lib/roda/session_middleware.rb 40 def each(&block) 41 load! 42 @data.each(&block) 43 end
# File lib/roda/session_middleware.rb 130 def empty? 131 load! 132 @data.empty? 133 end
Return whether the session cookie already exists. If this is false, then the session was set to an empty hash.
# File lib/roda/session_middleware.rb 120 def exists? 121 load! 122 req.env.has_key?('roda.session.serialized') 123 end
# File lib/roda/session_middleware.rb 50 def fetch(key, default = (no_default = true), &block) 51 load! 52 if no_default 53 @data.fetch(key.to_s, &block) 54 else 55 @data.fetch(key.to_s, default, &block) 56 end 57 end
# File lib/roda/session_middleware.rb 59 def has_key?(key) 60 load! 61 @data.has_key?(key.to_s) 62 end
If the session hasn’t been loaded, display that.
# File lib/roda/session_middleware.rb 110 def inspect 111 if loaded? 112 @data.inspect 113 else 114 "#<#{self.class}:0x#{self.object_id.to_s(16)} not yet loaded>" 115 end 116 end
# File lib/roda/session_middleware.rb 135 def keys 136 load! 137 @data.keys 138 end
Whether the session has already been loaded from the cookie yet.
# File lib/roda/session_middleware.rb 126 def loaded? 127 !!defined?(@data) 128 end
The Roda
sessions plugin options used by the middleware for this session hash.
# File lib/roda/session_middleware.rb 36 def options 37 @req.roda_class.opts[:sessions] 38 end
# File lib/roda/session_middleware.rb 98 def replace(hash) 99 load! 100 @data.clear 101 update(hash) 102 end
# File lib/roda/session_middleware.rb 84 def to_hash 85 load! 86 @data.dup 87 end
# File lib/roda/session_middleware.rb 89 def update(hash) 90 load! 91 hash.each do |key, value| 92 @data[key.to_s] = value 93 end 94 @data 95 end
# File lib/roda/session_middleware.rb 140 def values 141 load! 142 @data.values 143 end