Module: Merb::SessionMixin::RequestMixin

Defined in:
lib/merb-core/dispatch/session.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

Adds class methods to Merb::Request object. Sets up repository of session store types. Sets the session ID key and expiry values.



109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/merb-core/dispatch/session.rb', line 109

def self.included(base)
  base.extend ClassMethods
  
  # Keep track of all known session store types.
  base.cattr_accessor :registered_session_types
  base.registered_session_types = Dictionary.new
  base.class_inheritable_accessor :_session_id_key, :_session_secret_key,
                                  :_session_expiry
  
  base._session_id_key        = Merb::Config[:session_id_key] || '_session_id'
  base._session_expiry        = Merb::Config[:session_expiry] || 0
  base._session_secret_key    = Merb::Config[:session_secret_key]
end

Instance Method Details

#default_cookiesObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Assign default cookie values



225
226
227
228
229
230
231
232
# File 'lib/merb-core/dispatch/session.rb', line 225

def default_cookies
  defaults = {}
  if route && route.allow_fixation? && params.key?(_session_id_key)
    Merb.logger.info("Fixated session id: #{_session_id_key}")
    defaults[_session_id_key] = params[_session_id_key]
  end
  defaults
end

#default_session_storeObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The default session store type.



142
143
144
# File 'lib/merb-core/dispatch/session.rb', line 142

def default_session_store
  Merb::Config[:session_store] && Merb::Config[:session_store].to_sym
end

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Destroy the session cookie.



260
261
262
# File 'lib/merb-core/dispatch/session.rb', line 260

def destroy_session_cookie
  cookies.delete(_session_id_key)
end

#finalize_sessionObject Also known as: finalize_sessions

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Teardown and/or persist the current sessions.



217
218
219
# File 'lib/merb-core/dispatch/session.rb', line 217

def finalize_session
  session_stores.each { |name, store| store.finalize(self) }
end

#session(session_store = nil) ⇒ Object

Returns session container. Merb is able to handle multiple session stores, hence a parameter to pick it.

Parameters

session_store<String>

The type of session store to access,

defaults to default_session_store.

Notes

If no suitable session store type is given, it defaults to cookie-based sessions.

Returns

SessionContainer

an instance of a session store extending Merb::SessionContainer.



170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/merb-core/dispatch/session.rb', line 170

def session(session_store = nil)
  session_store ||= default_session_store
  if class_name = self.class.registered_session_types[session_store]
    session_stores[session_store] ||= Object.full_const_get(class_name).setup(self)
  elsif fallback = self.class.registered_session_types.keys.first
    Merb.logger.warn "Session store '#{session_store}' not found. Check your configuration in init file."
    Merb.logger.warn "Falling back to #{fallback} session store."
    session(fallback)
  else
    msg = "No session store set. Set it in init file like this: c[:session_store] = 'activerecord'"
    Merb.logger.error!(msg)
    raise NoSessionContainer, msg            
  end
end

#session=(new_session) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters

new_session<Merb::SessionContainer>

A session store instance.

Notes

The session is assigned internally by its session_store_type key.



192
193
194
195
196
197
198
199
200
# File 'lib/merb-core/dispatch/session.rb', line 192

def session=(new_session)
  if self.session?(new_session.class.session_store_type)
    original_session_id = self.session(new_session.class.session_store_type).session_id
    if new_session.session_id != original_session_id
      set_session_id_cookie(new_session.session_id)
    end
  end
  session_stores[new_session.class.session_store_type] = new_session
end

#session?(session_store = nil) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Whether a session has been setup

Returns

Boolean

true if the session is part of the session stores configured.

Returns:

  • (Boolean)


208
209
210
211
212
# File 'lib/merb-core/dispatch/session.rb', line 208

def session?(session_store = nil)
  (session_store ? [session_store] : session_stores).any? do |type, store|
    store.is_a?(Merb::SessionContainer)
  end
end

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns

String

The value of the session cookie; either the session id or the actual encoded data.



252
253
254
# File 'lib/merb-core/dispatch/session.rb', line 252

def session_cookie_value
  cookies[_session_id_key]
end

#session_storesObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns

Hash

All active session stores by type.



150
151
152
# File 'lib/merb-core/dispatch/session.rb', line 150

def session_stores
  @session_stores ||= {}
end

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Sets session cookie value.

Parameters

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.



241
242
243
244
245
# File 'lib/merb-core/dispatch/session.rb', line 241

def set_session_cookie_value(value, options = {})
  defaults = {}
  defaults[:expires] = Time.now + _session_expiry if _session_expiry > 0
  cookies.set_cookie(_session_id_key, value, defaults.merge(options))
end