Class: Mack::SessionStore::DataMapper

Inherits:
Base
  • Object
show all
Defined in:
lib/mack-data_mapper/sessions/data_mapper_session_store.rb

Overview

Stores session information in the database. To set the expiry time for this session store use the following configatron setting:

data_mapper_session_store::expiry_time: <%= 4.hours %>

Class Method Summary collapse

Class Method Details

.expire(id, *args) ⇒ Object

Deletes the session from the db, if it exists.



39
40
41
42
43
# File 'lib/mack-data_mapper/sessions/data_mapper_session_store.rb', line 39

def expire(id, *args)
  create_storage_if_non_existent
  sess = Mack::DataMapper::Session.get(id)
  sess.destroy unless sess.nil?
end

.expire_all(*args) ⇒ Object

Deletes all the sessions from the db.



46
47
48
49
# File 'lib/mack-data_mapper/sessions/data_mapper_session_store.rb', line 46

def expire_all(*args)
  create_storage_if_non_existent
  Mack::DataMapper::Session.all.destroy!
end

.get(id, *args) ⇒ Object

Gets the session from the db, using the specified id. If the session has ‘expired’ then it’s deleted from the db.



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/mack-data_mapper/sessions/data_mapper_session_store.rb', line 12

def get(id, *args)
  create_storage_if_non_existent
  sess = Mack::DataMapper::Session.first(:id => id)
  return nil if sess.nil? 
  expire_date = DateTime.now.minus_seconds(configatron.mack.data_mapper_session_store.expiry_time)
  if sess.updated_at.to_s < expire_date.to_s
    sess.destroy
    return nil
  end
  return sess.data
end

.set(id, request, *args) ⇒ Object

Creates or updates the session in the db.



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/mack-data_mapper/sessions/data_mapper_session_store.rb', line 25

def set(id, request, *args)
  create_storage_if_non_existent
  sess = Mack::DataMapper::Session.get(id)
  if sess.nil?
    sess = Mack::DataMapper::Session.new(:id => id, :data => request.session)
    raise sess.errors.full_messages.inspect unless sess.valid?
    sess.save
  else
    sess.data = request.session
    sess.save
  end
end