class RodaSessionMiddleware::SessionHash

  1. lib/roda/session_middleware.rb
Superclass: Object

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.

Public Instance Aliases

destroy -> clear
include? -> has_key?
key? -> has_key?
merge! -> update
store -> []=

Attributes

data [R]

The underlying data hash, or nil if the session has not yet been loaded.

req [R]

The Roda::RodaRequest subclass instance related to the session.

Public Class methods

new(req)
[show source]
   # File lib/roda/session_middleware.rb
30 def initialize(req)
31   @req = req
32   @data = nil
33 end

Public Instance methods

[](key)
[show source]
   # File lib/roda/session_middleware.rb
46 def [](key)
47   load!
48   @data[key.to_s]
49 end
[]=(key, value)
[show source]
   # File lib/roda/session_middleware.rb
67 def []=(key, value)
68   load!
69   @data[key.to_s] = value
70 end
clear()

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.

[show source]
   # File lib/roda/session_middleware.rb
76 def clear
77   load!
78   env = @req.env
79   env.delete('roda.session.created_at')
80   env.delete('roda.session.updated_at')
81   @data.clear
82 end
delete(key)
[show source]
    # File lib/roda/session_middleware.rb
105 def delete(key)
106   load!
107   @data.delete(key.to_s)
108 end
each(&block)
[show source]
   # File lib/roda/session_middleware.rb
41 def each(&block)
42   load!
43   @data.each(&block)
44 end
empty?()
[show source]
    # File lib/roda/session_middleware.rb
131 def empty?
132   load!
133   @data.empty?
134 end
exists?()

Return whether the session cookie already exists. If this is false, then the session was set to an empty hash.

[show source]
    # File lib/roda/session_middleware.rb
121 def exists?
122   load!
123   req.env.has_key?('roda.session.serialized')
124 end
fetch(key, default = (no_default = true), &block)
[show source]
   # File lib/roda/session_middleware.rb
51 def fetch(key, default = (no_default = true), &block)
52   load!
53   if no_default
54     @data.fetch(key.to_s, &block)
55   else
56     @data.fetch(key.to_s, default, &block)
57   end
58 end
has_key?(key)
[show source]
   # File lib/roda/session_middleware.rb
60 def has_key?(key)
61   load!
62   @data.has_key?(key.to_s)
63 end
inspect()

If the session hasn’t been loaded, display that.

[show source]
    # File lib/roda/session_middleware.rb
111 def inspect
112   if loaded?
113     @data.inspect
114   else
115     "#<#{self.class}:0x#{self.object_id.to_s(16)} not yet loaded>"
116   end
117 end
keys()
[show source]
    # File lib/roda/session_middleware.rb
136 def keys
137   load!
138   @data.keys
139 end
loaded?()

Whether the session has already been loaded from the cookie yet.

[show source]
    # File lib/roda/session_middleware.rb
127 def loaded?
128   !!@data
129 end
options()

The Roda sessions plugin options used by the middleware for this session hash.

[show source]
   # File lib/roda/session_middleware.rb
37 def options
38   @req.roda_class.opts[:sessions]
39 end
replace(hash)
[show source]
    # File lib/roda/session_middleware.rb
 99 def replace(hash)
100   load!
101   @data.clear
102   update(hash)
103 end
to_hash()
[show source]
   # File lib/roda/session_middleware.rb
85 def to_hash
86   load!
87   @data.dup
88 end
update(hash)
[show source]
   # File lib/roda/session_middleware.rb
90 def update(hash)
91   load!
92   hash.each do |key, value|
93     @data[key.to_s] = value
94   end
95   @data
96 end
values()
[show source]
    # File lib/roda/session_middleware.rb
141 def values
142   load!
143   @data.values
144 end