Class: Kiss::SequelSession

Inherits:
Object show all
Defined in:
lib/kiss/sequel_session.rb

Overview

Session store using Sequel.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object) ⇒ SequelSession

Returns a new instance of SequelSession.



96
97
98
99
# File 'lib/kiss/sequel_session.rb', line 96

def initialize(object)
  @_object = object
  @_data = self.class.unmarshal(@_object[:data]) || {}
end

Instance Attribute Details

#dataObject

good Lazy-unmarshal session state.



145
146
147
# File 'lib/kiss/sequel_session.rb', line 145

def data
  @_data
end

Class Method Details

.generate(controller) ⇒ Object

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



57
58
59
60
61
62
63
64
# File 'lib/kiss/sequel_session.rb', line 57

def generate(controller)
  new(Sequel::Model(controller.db[:sessions]).create(
    :session_id => rand_uuid,
    :data => marshal({}),
    :created_at => Time.now,
    :updated_at => Time.now
  ))
end

.marshal(data) ⇒ Object

good



86
87
88
# File 'lib/kiss/sequel_session.rb', line 86

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

.persist(controller, session_id) ⇒ Object

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



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/kiss/sequel_session.rb', line 68

def persist(controller, session_id)
  if session_id
    if object = Sequel::Model(controller.db[:sessions]).where(:session_id => session_id).first
      session = new( object )
    end
  end
  unless session
    session = generate( controller )
  end
  session
end

.rand_uuidObject



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/kiss/sequel_session.rb', line 43

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

.reloadable?Boolean

Don’t try to reload ARStore::Session in dev mode.

Returns:

  • (Boolean)


81
82
83
# File 'lib/kiss/sequel_session.rb', line 81

def reloadable? #:nodoc:
  false
end

.setup_storage(controller) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/kiss/sequel_session.rb', line 18

def setup_storage(controller)
  unless controller.db.table_exists?(:sessions)
    controller.db.create_table :sessions do
      primary_key :id, :null => false
      column :session_id, :varchar, :null => false
      column :data, :text
      column :updated_at, :timestamp, :null => false
      column :created_at, :timestamp, :null => false
      index :session_id, :unique => true
      index :updated_at
    end

    # db << <<-EOT
    #   CREATE TABLE `sessions` (
    #     `id` int(11) NOT NULL auto_increment,
    #     `session_id` varchar(255) default NULL,
    #     `data` text,
    #     `created_at` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
    #     `updated_at` timestamp NOT NULL default '0000-00-00 00:00:00',
    #     PRIMARY KEY (`id`)
    #   );
    #   EOT
  end
end

.unmarshal(data) ⇒ Object

good



91
92
93
# File 'lib/kiss/sequel_session.rb', line 91

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

Instance Method Details

#[](key) ⇒ Object

good



124
125
126
# File 'lib/kiss/sequel_session.rb', line 124

def [](key)
  data[key]
end

#[]=(key, val) ⇒ Object

good



129
130
131
# File 'lib/kiss/sequel_session.rb', line 129

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

#delete(key = nil) ⇒ Object

good Lazy-delete of session data



119
120
121
# File 'lib/kiss/sequel_session.rb', line 119

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

#each(&b) ⇒ Object

good



139
140
141
# File 'lib/kiss/sequel_session.rb', line 139

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

#empty?Boolean

good

Returns:

  • (Boolean)


134
135
136
# File 'lib/kiss/sequel_session.rb', line 134

def empty?
  data.empty?
end

#loaded?Boolean

good Has the session been loaded yet?

Returns:

  • (Boolean)


151
152
153
# File 'lib/kiss/sequel_session.rb', line 151

def loaded?
  !! @_data
end

#refresh_expirationObject

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



113
114
115
# File 'lib/kiss/sequel_session.rb', line 113

def refresh_expiration
  @_needs_new_cookie = true
end

#regenerateObject

Regenerate the Session ID



106
107
108
109
# File 'lib/kiss/sequel_session.rb', line 106

def regenerate
  @_object.update_attributes(:session_id => Kiss::SessionMixin::rand_uuid)
  @_needs_new_cookie = true
end

#saveObject

good



156
157
158
159
160
# File 'lib/kiss/sequel_session.rb', line 156

def save
  @_object[:data] = self.class.marshal(self.data)
  @_object[:updated_at] = Time.now
  @_object.save
end

#values(*args) ⇒ Object



101
102
103
# File 'lib/kiss/sequel_session.rb', line 101

def values(*args)
  @_object.values(*args)
end