summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/zorglub/node.rb2
-rw-r--r--lib/zorglub/session.rb54
2 files changed, 47 insertions, 9 deletions
diff --git a/lib/zorglub/node.rb b/lib/zorglub/node.rb
index 14808ae..751bb4d 100644
--- a/lib/zorglub/node.rb
+++ b/lib/zorglub/node.rb
@@ -88,7 +88,7 @@ module Zorglub
end
#
def session
- @session ||= Session.new( Config.session_on ? @request.cookies : {} )
+ @session ||= Session.new @request
end
#
def r
diff --git a/lib/zorglub/session.rb b/lib/zorglub/session.rb
index da30deb..a7c0b29 100644
--- a/lib/zorglub/session.rb
+++ b/lib/zorglub/session.rb
@@ -4,29 +4,67 @@ require 'securerandom'
#
module Zorglub
#
+ class SessionHash
+ #
+ @data = {}
+ class << self
+ attr_reader :data
+ end
+ #
+ def initialize sid
+ @sid = sid
+ # TODO if sid is nil, one should be created
+ @session_data = SessionHash.data[sid]||={}
+ end
+ #
+ def exists?
+ not @sid.nil?
+ end
+ #
+ def [] idx
+ @session_data[idx]
+ end
+ #
+ def []= idx, v
+ @session_data[idx] = v
+ end
+ end
+ #
class Session
#
@session_key = 'zorglub.sid'
- @session_data = {}
+ @session_kls = Zorglub::SessionHash
class << self
- attr_reader :session_key, :session_data
+ attr_accessor :session_key, :session_kls
end
#
- def initialize cookies
- @sid = cookies[self.class.session_key]
- @data = self.class.session_data[@sid]||={}
+ def initialize req
+ @request = req
+ @instance = nil
end
#
+ def setup!
+ if Config.session_on
+ @instance = Session.session_kls.new @request.cookies[Session.session_key]
+ else
+ @instance = {}
+ end
+ end
+ private :setup!
+ #
def exists?
- not @sid.nil?
+ setup! if @instance.nil?
+ @instance.exists?
end
#
def [] idx
- @data[idx]
+ setup! if @instance.nil?
+ @instance[idx]
end
#
def []= idx, v
- @data[idx] = v
+ setup! if @instance.nil?
+ @instance[idx] = v
end
#
end