Class: Merb::DohDbSession

Inherits:
Object show all
Defined in:
lib/doh/merb/db_session.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}) ⇒ DohDbSession

Returns a new instance of DohDbSession.



34
35
36
# File 'lib/doh/merb/db_session.rb', line 34

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

Instance Attribute Details

#dataObject

Returns the value of attribute data.



32
33
34
# File 'lib/doh/merb/db_session.rb', line 32

def data
  @data
end

Returns the value of attribute needs_new_cookie.



32
33
34
# File 'lib/doh/merb/db_session.rb', line 32

def needs_new_cookie
  @needs_new_cookie
end

#session_idObject

Returns the value of attribute session_id.



32
33
34
# File 'lib/doh/merb/db_session.rb', line 32

def session_id
  @session_id
end

#updated_atObject

Returns the value of attribute updated_at.



32
33
34
# File 'lib/doh/merb/db_session.rb', line 32

def updated_at
  @updated_at
end

Class Method Details

.create_session_tableObject



72
73
74
# File 'lib/doh/merb/db_session.rb', line 72

def create_session_table
  DohDb::query("CREATE TABLE session (session_id CHAR(255), data TEXT, updated_at DATETIME)")
end

.ensure_session_table_existsObject



64
65
66
67
68
69
70
# File 'lib/doh/merb/db_session.rb', line 64

def ensure_session_table_exists
  return if @session_table_exists
  if !DohDb::table_exist?('session')
    create_session_table
  end
  @session_table_exists = true
end

.generateObject

Generates a new session ID and creates a row for the new session in the database.



40
41
42
43
44
# File 'lib/doh/merb/db_session.rb', line 40

def generate
  new_session = self.new({})
  new_session.session_id = Merb::SessionMixin::rand_uuid
  new_session
end

.marshal(data) ⇒ Object



76
# File 'lib/doh/merb/db_session.rb', line 76

def marshal(data) Base64.encode64(Marshal.dump(data)) if data end

.persist(session_id) ⇒ Object

Gets the existing session based on the session_id available in cookies. If none is found, generates a new session.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/doh/merb/db_session.rb', line 48

def persist(session_id)
  ensure_session_table_exists
  if !session_id.blank?
    row = DohDb::select_optional_row("SELECT * FROM session WHERE session_id = #{session_id.to_sql}")
    if row
      session = self.new(unmarshal(row['data']))
      session.updated_at = row['updated_at']
      session.session_id = row['session_id']
    end
  end
  unless session
    session = generate
  end
  [session, session.session_id]
end

.unmarshal(data) ⇒ Object



77
# File 'lib/doh/merb/db_session.rb', line 77

def unmarshal(data) Marshal.load(Base64.decode64(data)) if data end

Instance Method Details

#[](key) ⇒ Object



110
111
112
# File 'lib/doh/merb/db_session.rb', line 110

def [](key)
  data[key]
end

#[]=(key, val) ⇒ Object



114
115
116
# File 'lib/doh/merb/db_session.rb', line 114

def []=(key, val)
  data[key] = val
end

#delete(key = nil) ⇒ Object

delete of session data



94
95
96
# File 'lib/doh/merb/db_session.rb', line 94

def delete(key = nil)
  key ? data.delete(key) : data.clear
end

#delete_old_sessionsObject



118
119
120
121
122
123
# File 'lib/doh/merb/db_session.rb', line 118

def delete_old_sessions
  if !@last_deleted || @last_deleted != Date.today
    DohDb::query("DELETE FROM session WHERE updated_at < DATE_SUB(NOW(), interval 20 day)")
  end
  @last_deleted = Date.today
end

#each(&b) ⇒ Object



102
103
104
# File 'lib/doh/merb/db_session.rb', line 102

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

#each_with_index(&b) ⇒ Object



106
107
108
# File 'lib/doh/merb/db_session.rb', line 106

def each_with_index(&b)
  data.each_with_index(&b)
end

#empty?Boolean

Returns:

  • (Boolean)


98
99
100
# File 'lib/doh/merb/db_session.rb', line 98

def empty?
  data.empty?
end

#refresh_expirationObject

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



89
90
91
# File 'lib/doh/merb/db_session.rb', line 89

def refresh_expiration
  self.needs_new_cookie = true
end

#regenerateObject

Regenerate the Session ID



81
82
83
84
85
# File 'lib/doh/merb/db_session.rb', line 81

def regenerate
  self.session_id = Merb::SessionMixin::rand_uuid
  self.needs_new_cookie = true
  self.save
end

#saveObject



125
126
127
128
129
130
131
132
133
134
# File 'lib/doh/merb/db_session.rb', line 125

def save
  DohDbSession::ensure_session_table_exists
  delete_old_sessions
  row = DohDb::select_optional_row("SELECT updated_at FROM session WHERE session_id = #{session_id.to_sql}")
  if row
    DohDb::query("UPDATE session SET data = #{DohDbSession::marshal(data).to_sql}, updated_at = NOW() WHERE session_id = #{session_id.to_sql}")
  else
    DohDb::query("INSERT INTO session (session_id, data, updated_at) VALUES (#{session_id.to_sql}, #{DohDbSession::marshal(data).to_sql}, NOW())")
  end
end