diff options
author | Jérémy Zurcher <jeremy@asynk.ch> | 2011-06-21 07:25:07 +0200 |
---|---|---|
committer | Jérémy Zurcher <jeremy@asynk.ch> | 2011-06-21 07:25:07 +0200 |
commit | 64e2170cd80b4fbb5e6986074df654c661b5a89b (patch) | |
tree | ed432d538e3d71a3e7c5e4533cc34ad315932fd8 /lib | |
parent | 58e3798c12dd9343855668f01a433f6dbc6a2e51 (diff) | |
download | zorglub-64e2170cd80b4fbb5e6986074df654c661b5a89b.zip zorglub-64e2170cd80b4fbb5e6986074df654c661b5a89b.tar.gz |
cleanup session.rb
Diffstat (limited to 'lib')
-rw-r--r-- | lib/zorglub/node.rb | 2 | ||||
-rw-r--r-- | 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 |