Class: Egalite::SessionSequel
Instance Attribute Summary
Attributes inherited from Session
#cookie_name, #expire_after, #hash
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from Session
#[], #[]=
Constructor Details
#initialize(env, cookies, opts = {}) ⇒ SessionSequel
Returns a new instance of SessionSequel.
58
59
60
61
62
63
|
# File 'lib/egalite/session.rb', line 58
def initialize(env, cookies, opts = {})
@db = env.db
@table = opts[:tablename] || :sessions
super(env, cookies, opts)
end
|
Class Method Details
.create_table(db, opts = {}) ⇒ Object
39
40
41
42
43
44
45
46
47
48
|
# File 'lib/egalite/session.rb', line 39
def self.create_table(db, opts = {})
table = opts[:table_name] || :sessions
db.create_table(table) {
primary_key :id, :integer, :auto_increment => true
column :mac, :varchar
column :updated_at, :timestamp
column :expire_sec, :integer }
end
|
Instance Method Details
#_load(_sstr) ⇒ Object
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
# File 'lib/egalite/session.rb', line 75
def _load(_sstr)
return false unless _sstr and _sstr.size > 0
(sid,mac) = _sstr.split(/_/)
sid = sid.to_i
return false if sid <= 0
return false unless mac and mac.size > 0
rec = @db[@table][:id => sid]
return false unless rec and rec[:mac] == mac
updated = rec[:updated_at]
return false if Time.now > expire(updated)
@sstr = _sstr
@hash = rec
@sid = sid
@mac = mac
@loaded = true
@cookies[@cookie_name] = cookie
@db[@table].filter(:id => sid).update(:updated_at => Time.now)
true
end
|
64
65
66
67
68
69
70
71
|
# File 'lib/egalite/session.rb', line 64
def cookie
{
:value => @sstr,
:expires => expire,
:path => @path,
:secure => @secure
}
end
|
#create(hash = nil) ⇒ Object
109
110
111
112
113
114
115
116
117
118
119
120
121
|
# File 'lib/egalite/session.rb', line 109
def create(hash = nil)
@sid = @db[@table].insert({})
@mac = OpenSSL::Random.random_bytes(8).unpack('h*')[0]
hash ||= {}
@db[@table].filter(:id => @sid).update(hash.merge(:mac => @mac,:updated_at => Time.now))
@hash = @db[@table][:id => @sid]
@sstr = "#@sid" + "_#@mac"
@cookies[@cookie_name] = cookie
@loaded = true
true
end
|
122
123
124
125
126
127
128
129
130
131
132
|
# File 'lib/egalite/session.rb', line 122
def delete
@cookies[@cookie_name] = {
:value => nil,
:expires => Time.now - 3600,
:path => @path,
:secure => @secure
}
@db[@table].filter(:id => @sid).delete
@loaded = false
true
end
|
#expire(t = nil) ⇒ Object
49
50
51
52
53
54
55
56
|
# File 'lib/egalite/session.rb', line 49
def expire(t = nil)
t ||= Time.now
if @individual_expire and @hash and @hash[:expire_sec]
t + @hash[:expire_sec]
else
t + @expire_after
end
end
|
104
105
106
107
108
|
# File 'lib/egalite/session.rb', line 104
def load
_sstr = @cookies[@cookie_name]
_sstr = _sstr[0] if _sstr.is_a?(Array)
_load(_sstr)
end
|
#load_from_param(_sstr) ⇒ Object
101
102
103
|
# File 'lib/egalite/session.rb', line 101
def load_from_param(_sstr)
_load(_sstr)
end
|
133
134
135
136
137
138
139
140
|
# File 'lib/egalite/session.rb', line 133
def save
return false unless @loaded
[:updated_at, :mac, :id].each { |s| @hash.delete(s) }
@db[@table].filter(:id => @sid).update(
{:updated_at => Time.now}.merge(@hash)
)
true
end
|
72
73
74
|
# File 'lib/egalite/session.rb', line 72
def sstr
@sstr
end
|