Module: Merb::SessionMixin

Defined in:
lib/merb-core/dispatch/session.rb,
lib/merb-core/dispatch/session/cookie.rb,
lib/merb-core/dispatch/session/memory.rb,
lib/merb-core/dispatch/session/memcached.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.finalize_session_exception_callbacks(&block) ⇒ Object

Adds a callback to the list of callbacks run when exception is raised on session finalization, so you can recover.

See session mixins documentation for details on session finalization.

Params

&block

A block to be added to the callbacks that will be executed if there’s exception on session finalization.



48
49
50
51
52
53
54
# File 'lib/merb-core/dispatch/session.rb', line 48

def finalize_session_exception_callbacks(&block)
  if block_given?
    @_finalize_session_exception_callbacks << block
  else
    @_finalize_session_exception_callbacks
  end
end

.needs_new_cookie!Object

Marks this session as needing a new cookie.



33
34
35
# File 'lib/merb-core/dispatch/session.rb', line 33

def needs_new_cookie!
  @_new_cookie = true
end

.persist_exception_callbacks(&block) ⇒ Object

Adds a callback to the list of callbacks run when exception is raised on session persisting, so you can recover.

See session mixins documentation for details on session persisting.

Params

&block

A block to be added to the callbacks that will be executed if there’s exception on session persisting.



67
68
69
70
71
72
73
# File 'lib/merb-core/dispatch/session.rb', line 67

def persist_exception_callbacks(&block)
  if block_given?
    @_persist_exception_callbacks << block
  else
    @_persist_exception_callbacks
  end
end

.rand_uuidObject

Returns

String

A random 32 character string for use as a unique session ID.



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/merb-core/dispatch/session.rb', line 19

def rand_uuid
  values = [
    rand(0x0010000),
    rand(0x0010000),
    rand(0x0010000),
    rand(0x0010000),
    rand(0x0010000),
    rand(0x1000000),
    rand(0x1000000),
  ]
  "%04x%04x%04x%04x%04x%06x%06x" % values
end

Instance Method Details

#finalize_sessionObject

Finalizes the session by storing the session ID in a cookie, if the session has changed.



20
21
22
23
24
25
26
27
# File 'lib/merb-core/dispatch/session/cookie.rb', line 20

def finalize_session
  new_session = request.session.read_cookie
  if @original_session != new_session
    options = {:expires => (Time.now + _session_expiry)}
    options[:domain] = _session_cookie_domain if _session_cookie_domain
    cookies.set_cookie(_session_id_key, new_session, options)
  end
end

#session_store_typeObject

Returns

String

The session store type, i.e. “memcache”.



31
32
33
# File 'lib/merb-core/dispatch/session/cookie.rb', line 31

def session_store_type
  "cookie"
end

Sets the session id cookie, along with the correct expiry and domain – used for new or reset sessions



6
7
8
9
10
11
12
# File 'lib/merb-core/dispatch/session.rb', line 6

def set_session_id_cookie(key)
  options = {}
  options[:value] = key
  options[:expires] = Time.now + _session_expiry if _session_expiry > 0
  options[:domain] = _session_cookie_domain if _session_cookie_domain
  cookies[_session_id_key] = options
end

#setup_sessionObject

Adds a before and after dispatch hook for setting up the memcached session store.

Parameters

base<Class>

The class to which the SessionMixin is mixed into.



13
14
15
16
# File 'lib/merb-core/dispatch/session/cookie.rb', line 13

def setup_session
  request.session = Merb::CookieSession.new(cookies[_session_id_key], _session_secret_key)
  @original_session = request.session.read_cookie
end