Class: RodaSessionMiddleware::SessionHash
- Inherits:
-
Object
- Object
- RodaSessionMiddleware::SessionHash
- Defined in:
- lib/roda/session_middleware.rb
Overview
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.
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
The underlying data hash, or nil if the session has not yet been loaded.
-
#req ⇒ Object
readonly
The Roda::RodaRequest subclass instance related to the session.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object (also: #store)
-
#clear ⇒ Object
(also: #destroy)
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.
- #delete(key) ⇒ Object
- #each(&block) ⇒ Object
- #empty? ⇒ Boolean
-
#exists? ⇒ Boolean
Return whether the session cookie already exists.
- #fetch(key, default = (no_default = true), &block) ⇒ Object
- #has_key?(key) ⇒ Boolean (also: #key?, #include?)
-
#initialize(req) ⇒ SessionHash
constructor
A new instance of SessionHash.
-
#inspect ⇒ Object
If the session hasn’t been loaded, display that.
- #keys ⇒ Object
-
#loaded? ⇒ Boolean
Whether the session has already been loaded from the cookie yet.
-
#options ⇒ Object
The Roda sessions plugin options used by the middleware for this session hash.
- #replace(hash) ⇒ Object
- #to_hash ⇒ Object
- #update(hash) ⇒ Object (also: #merge!)
- #values ⇒ Object
Constructor Details
#initialize(req) ⇒ SessionHash
Returns a new instance of SessionHash.
30 31 32 |
# File 'lib/roda/session_middleware.rb', line 30 def initialize(req) @req = req end |
Instance Attribute Details
#data ⇒ Object (readonly)
The underlying data hash, or nil if the session has not yet been loaded.
28 29 30 |
# File 'lib/roda/session_middleware.rb', line 28 def data @data end |
#req ⇒ Object (readonly)
The Roda::RodaRequest subclass instance related to the session.
24 25 26 |
# File 'lib/roda/session_middleware.rb', line 24 def req @req end |
Instance Method Details
#[](key) ⇒ Object
45 46 47 48 |
# File 'lib/roda/session_middleware.rb', line 45 def [](key) load! @data[key.to_s] end |
#[]=(key, value) ⇒ Object Also known as: store
66 67 68 69 |
# File 'lib/roda/session_middleware.rb', line 66 def []=(key, value) load! @data[key.to_s] = value end |
#clear ⇒ Object Also known as: destroy
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.
75 76 77 78 79 80 81 |
# 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 |
#delete(key) ⇒ Object
104 105 106 107 |
# File 'lib/roda/session_middleware.rb', line 104 def delete(key) load! @data.delete(key.to_s) end |
#each(&block) ⇒ Object
40 41 42 43 |
# File 'lib/roda/session_middleware.rb', line 40 def each(&block) load! @data.each(&block) end |
#empty? ⇒ Boolean
130 131 132 133 |
# File 'lib/roda/session_middleware.rb', line 130 def empty? load! @data.empty? end |
#exists? ⇒ Boolean
Return whether the session cookie already exists. If this is false, then the session was set to an empty hash.
120 121 122 123 |
# File 'lib/roda/session_middleware.rb', line 120 def exists? load! req.env.has_key?('roda.session.serialized') end |
#fetch(key, default = (no_default = true), &block) ⇒ Object
50 51 52 53 54 55 56 57 |
# 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 |
#has_key?(key) ⇒ Boolean Also known as: key?, include?
59 60 61 62 |
# File 'lib/roda/session_middleware.rb', line 59 def has_key?(key) load! @data.has_key?(key.to_s) end |
#inspect ⇒ Object
If the session hasn’t been loaded, display that.
110 111 112 113 114 115 116 |
# 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 |
#keys ⇒ Object
135 136 137 138 |
# File 'lib/roda/session_middleware.rb', line 135 def keys load! @data.keys end |
#loaded? ⇒ Boolean
Whether the session has already been loaded from the cookie yet.
126 127 128 |
# File 'lib/roda/session_middleware.rb', line 126 def loaded? !!defined?(@data) end |
#options ⇒ Object
The Roda sessions plugin options used by the middleware for this session hash.
36 37 38 |
# File 'lib/roda/session_middleware.rb', line 36 def @req.roda_class.opts[:sessions] end |
#replace(hash) ⇒ Object
98 99 100 101 102 |
# File 'lib/roda/session_middleware.rb', line 98 def replace(hash) load! @data.clear update(hash) end |
#to_hash ⇒ Object
84 85 86 87 |
# File 'lib/roda/session_middleware.rb', line 84 def to_hash load! @data.dup end |
#update(hash) ⇒ Object Also known as: merge!
89 90 91 92 93 94 95 |
# 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 |
#values ⇒ Object
140 141 142 143 |
# File 'lib/roda/session_middleware.rb', line 140 def values load! @data.values end |