Module | Merb::SessionMixin::RequestMixin |
In: |
merb-core/lib/merb-core/dispatch/session.rb
|
Adds class methods to Merb::Request object. Sets up repository of session store types. Sets the session ID key and expiry values.
:api: private
# File merb-core/lib/merb-core/dispatch/session.rb, line 115 115: def self.included(base) 116: base.extend ClassMethods 117: 118: # Keep track of all known session store types. 119: base.cattr_accessor :registered_session_types 120: base.registered_session_types = Dictionary.new 121: base.class_inheritable_accessor :_session_id_key, :_session_secret_key, 122: :_session_expiry 123: 124: base._session_id_key = Merb::Config[:session_id_key] || '_session_id' 125: base._session_expiry = Merb::Config[:session_expiry] || 0 126: base._session_secret_key = Merb::Config[:session_secret_key] 127: end
Assign default cookie values
:api: private
# File merb-core/lib/merb-core/dispatch/session.rb, line 231 231: def default_cookies 232: defaults = {} 233: if route && route.allow_fixation? && params.key?(_session_id_key) 234: Merb.logger.info("Fixated session id: #{_session_id_key}") 235: defaults[_session_id_key] = params[_session_id_key] 236: end 237: defaults 238: end
Teardown and/or persist the current sessions.
:api: private
# File merb-core/lib/merb-core/dispatch/session.rb, line 223 223: def finalize_session 224: session_stores.each { |name, store| store.finalize(self) } 225: end
Returns session container. Merb is able to handle multiple session stores, hence a parameter to pick it.
session_store<String>: | The type of session store to access, |
defaults to default_session_store.
If no suitable session store type is given, it defaults to cookie-based sessions.
SessionContainer: | an instance of a session store extending Merb::SessionContainer. |
:api: public
# File merb-core/lib/merb-core/dispatch/session.rb, line 176 176: def session(session_store = nil) 177: session_store ||= default_session_store 178: if class_name = self.class.registered_session_types[session_store] 179: session_stores[session_store] ||= Object.full_const_get(class_name).setup(self) 180: elsif fallback = self.class.registered_session_types.keys.first 181: Merb.logger.warn "Session store '#{session_store}' not found. Check your configuration in init file." 182: Merb.logger.warn "Falling back to #{fallback} session store." 183: session(fallback) 184: else 185: msg = "No session store set. Set it in init file like this: c[:session_store] = 'activerecord'" 186: Merb.logger.error!(msg) 187: raise NoSessionContainer, msg 188: end 189: end
new_session<Merb::SessionContainer>: | A session store instance. |
The session is assigned internally by its session_store_type key.
:api: private
# File merb-core/lib/merb-core/dispatch/session.rb, line 198 198: def session=(new_session) 199: if self.session?(new_session.class.session_store_type) 200: original_session_id = self.session(new_session.class.session_store_type).session_id 201: if new_session.session_id != original_session_id 202: set_session_id_cookie(new_session.session_id) 203: end 204: end 205: session_stores[new_session.class.session_store_type] = new_session 206: end
Whether a session has been setup
Boolean: | true if the session is part of the session stores configured. |
:api: private
# File merb-core/lib/merb-core/dispatch/session.rb, line 214 214: def session?(session_store = nil) 215: (session_store ? [session_store] : session_stores).any? do |type, store| 216: store.is_a?(Merb::SessionContainer) 217: end 218: end
Sets session cookie value.
value<String>: | The value of the session cookie; either the session id or the actual encoded data. |
options<Hash>: | Cookie options like domain, path and expired. |
:api: private
# File merb-core/lib/merb-core/dispatch/session.rb, line 247 247: def set_session_cookie_value(value, options = {}) 248: defaults = {} 249: defaults[:expires] = Time.now + _session_expiry if _session_expiry > 0 250: cookies.set_cookie(_session_id_key, value, defaults.merge(options)) 251: end