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 33 |
# File 'lib/roda/session_middleware.rb', line 30 def initialize(req) @req = req @data = nil 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
46 47 48 49 |
# File 'lib/roda/session_middleware.rb', line 46 def [](key) load! @data[key.to_s] end |
#[]=(key, value) ⇒ Object Also known as: store
67 68 69 70 |
# File 'lib/roda/session_middleware.rb', line 67 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.
76 77 78 79 80 81 82 |
# File 'lib/roda/session_middleware.rb', line 76 def clear load! env = @req.env env.delete('roda.session.created_at') env.delete('roda.session.updated_at') @data.clear end |
#delete(key) ⇒ Object
105 106 107 108 |
# File 'lib/roda/session_middleware.rb', line 105 def delete(key) load! @data.delete(key.to_s) end |
#each(&block) ⇒ Object
41 42 43 44 |
# File 'lib/roda/session_middleware.rb', line 41 def each(&block) load! @data.each(&block) end |
#empty? ⇒ Boolean
131 132 133 134 |
# File 'lib/roda/session_middleware.rb', line 131 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.
121 122 123 124 |
# File 'lib/roda/session_middleware.rb', line 121 def exists? load! req.env.has_key?('roda.session.serialized') end |
#fetch(key, default = (no_default = true), &block) ⇒ Object
51 52 53 54 55 56 57 58 |
# File 'lib/roda/session_middleware.rb', line 51 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?
60 61 62 63 |
# File 'lib/roda/session_middleware.rb', line 60 def has_key?(key) load! @data.has_key?(key.to_s) end |
#inspect ⇒ Object
If the session hasn't been loaded, display that.
111 112 113 114 115 116 117 |
# File 'lib/roda/session_middleware.rb', line 111 def inspect if loaded? @data.inspect else "#<#{self.class}:0x#{self.object_id.to_s(16)} not yet loaded>" end end |
#keys ⇒ Object
136 137 138 139 |
# File 'lib/roda/session_middleware.rb', line 136 def keys load! @data.keys end |
#loaded? ⇒ Boolean
Whether the session has already been loaded from the cookie yet.
127 128 129 |
# File 'lib/roda/session_middleware.rb', line 127 def loaded? !!@data end |
#options ⇒ Object
The Roda sessions plugin options used by the middleware for this session hash.
37 38 39 |
# File 'lib/roda/session_middleware.rb', line 37 def @req.roda_class.opts[:sessions] end |
#replace(hash) ⇒ Object
99 100 101 102 103 |
# File 'lib/roda/session_middleware.rb', line 99 def replace(hash) load! @data.clear update(hash) end |
#to_hash ⇒ Object
85 86 87 88 |
# File 'lib/roda/session_middleware.rb', line 85 def to_hash load! @data.dup end |
#update(hash) ⇒ Object Also known as: merge!
90 91 92 93 94 95 96 |
# File 'lib/roda/session_middleware.rb', line 90 def update(hash) load! hash.each do |key, value| @data[key.to_s] = value end @data end |
#values ⇒ Object
141 142 143 144 |
# File 'lib/roda/session_middleware.rb', line 141 def values load! @data.values end |