From 64e2170cd80b4fbb5e6986074df654c661b5a89b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Zurcher?= Date: Tue, 21 Jun 2011 07:25:07 +0200 Subject: cleanup session.rb --- lib/zorglub/node.rb | 2 +- lib/zorglub/session.rb | 54 ++++++++++++++++++++++++++++++++++++++++++-------- 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 -- cgit v1.1-2-g2b99