Class: Merb::MemorySession

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

Overview

Sessions stored in memory.

Set it up by adding the following to your init file:

Merb::Config.use do |c|
  c[:session_store]      = :memory
  c[:memory_session_ttl] = 3600 # in seconds, one hour
end

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.



54
55
56
57
# File 'lib/merb-core/dispatch/session/memory.rb', line 54

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.



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

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

Instance Attribute Details

#dataObject

Returns the value of attribute data.



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

def data
  @data
end

Returns the value of attribute needs_new_cookie.



50
51
52
# File 'lib/merb-core/dispatch/session/memory.rb', line 50

def needs_new_cookie
  @needs_new_cookie
end

#session_idObject

Returns the value of attribute session_id.



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

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.



65
66
67
68
# File 'lib/merb-core/dispatch/session/memory.rb', line 65

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.



77
78
79
80
81
82
83
84
85
# File 'lib/merb-core/dispatch/session/memory.rb', line 77

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.



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

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.



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

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

#deleteObject

Deletes the session by emptying stored data.



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

def delete
  @data = {} 
end

#each(&b) ⇒ Object

Yields the session data to an each block.

Parameter

&b

The block to pass to each.



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

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

#loaded?Boolean

Returns

Boolean

True if session has been loaded already.

Returns:

  • (Boolean)


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

def loaded?
  !! @data
end

#refresh_expirationObject

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



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

def refresh_expiration 
  self.needs_new_cookie=true 
end

#regenerateObject

Regenerate the Session ID



90
91
92
93
94
95
96
97
# File 'lib/merb-core/dispatch/session/memory.rb', line 90

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