Class: Merb::MemorySession

Inherits:
Object show all
Defined in:
lib/merb-core/dispatch/session/memory.rb

Overview

Sessions stored in memory.

And a setting in merb.yml:

:session_store: memory
:memory_session_ttl: 3600 (in seconds, one hour)

Sessions will remain in memory until the server is stopped or the time as set in :memory_session_ttl expires.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(session_id) ⇒ MemorySession

Parameters

session_id<String>

A unique identifier for this session.



47
48
49
50
# File 'lib/merb-core/dispatch/session/memory.rb', line 47

def initialize(session_id)
  @session_id = session_id
  @data = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object (private)

Attempts to redirect any messages to the data object.



136
137
138
# File 'lib/merb-core/dispatch/session/memory.rb', line 136

def method_missing(name, *args, &block)
  @data.send(name, *args, &block)
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



42
43
44
# File 'lib/merb-core/dispatch/session/memory.rb', line 42

def data
  @data
end

Returns the value of attribute needs_new_cookie.



43
44
45
# File 'lib/merb-core/dispatch/session/memory.rb', line 43

def needs_new_cookie
  @needs_new_cookie
end

#session_idObject

Returns the value of attribute session_id.



41
42
43
# File 'lib/merb-core/dispatch/session/memory.rb', line 41

def session_id
  @session_id
end

Class Method Details

.generateObject

Generates a new session ID and creates a new session.

Returns

MemorySession

The new session.



58
59
60
61
# File 'lib/merb-core/dispatch/session/memory.rb', line 58

def generate
  sid = Merb::SessionMixin::rand_uuid
  MemorySessionContainer[sid] = new(sid)
end

.persist(session_id) ⇒ Object

Parameters

session_id<String

The ID of the session to retrieve.

Returns

Array

A pair consisting of a MemorySession and the session’s ID. If no sessions matched session_id, a new MemorySession will be generated.



70
71
72
73
74
75
76
77
78
# File 'lib/merb-core/dispatch/session/memory.rb', line 70

def persist(session_id)
  if session_id
    session = MemorySessionContainer[session_id]
  end
  unless session
    session = generate
  end
  [session, session.session_id]
end

Instance Method Details

#[](k) ⇒ Object

Parameters

k<~to_s>

The key of the session parameter to retrieve.

Returns

String

The value of the session parameter.



121
122
123
# File 'lib/merb-core/dispatch/session/memory.rb', line 121

def [](k) 
  @data[k] 
end

#[]=(k, v) ⇒ Object

Parameters

k<~to_s>

The key of the session parameter to set.

v<~to_s>

The value of the session parameter to set.



112
113
114
# File 'lib/merb-core/dispatch/session/memory.rb', line 112

def []=(k, v) 
  @data[k] = v
end

#deleteObject

Deletes the session by emptying stored data.



99
100
101
# File 'lib/merb-core/dispatch/session/memory.rb', line 99

def delete
  @data = {} 
end

#each(&b) ⇒ Object

Yields the session data to an each block.

Parameter

&b

The block to pass to each.



129
130
131
# File 'lib/merb-core/dispatch/session/memory.rb', line 129

def each(&b) 
  @data.each(&b) 
end

#loaded?Boolean

Returns

Boolean

True if session has been loaded already.

Returns:

  • (Boolean)


105
106
107
# File 'lib/merb-core/dispatch/session/memory.rb', line 105

def loaded?
  !! @data
end

#refresh_expirationObject

Recreates the cookie with the default expiration time. Useful during log in for pushing back the expiration date.



94
95
96
# File 'lib/merb-core/dispatch/session/memory.rb', line 94

def refresh_expiration 
  self.needs_new_cookie=true 
end

#regenerateObject

Regenerate the Session ID



83
84
85
86
87
88
89
90
# File 'lib/merb-core/dispatch/session/memory.rb', line 83

def regenerate
  new_sid = Merb::SessionMixin::rand_uuid 
  old_sid = @session_id
  MemorySessionContainer[new_sid] = MemorySessionContainer[old_sid]
  @session_id = new_sid
  MemorySessionContainer.delete(old_sid)
  self.needs_new_cookie=true 
end