From f8922272bbf9816dec7fe4c57002afb54d353f50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Zurcher?= Date: Mon, 20 Jun 2011 16:57:59 +0200 Subject: simple never cleaned in memory session data management --- lib/zorglub/config.rb | 3 ++- lib/zorglub/node.rb | 11 ++++++----- lib/zorglub/session.rb | 26 +++++++++++++++++++++----- 3 files changed, 29 insertions(+), 11 deletions(-) diff --git a/lib/zorglub/config.rb b/lib/zorglub/config.rb index 70fdfcc..3916ac7 100644 --- a/lib/zorglub/config.rb +++ b/lib/zorglub/config.rb @@ -8,7 +8,8 @@ module Zorglub :engine => nil, :view_dir => 'view', :layout_dir => 'layout', - :default_layout => 'default' + :default_layout => 'default', + :session_on => false } @engines = { } class << self diff --git a/lib/zorglub/node.rb b/lib/zorglub/node.rb index 2e080ca..14808ae 100644 --- a/lib/zorglub/node.rb +++ b/lib/zorglub/node.rb @@ -33,8 +33,6 @@ module Zorglub action = {:engine=>engine,:layout=>layout,:view=>File.join(r,meth),:method=>meth,:args=>args} node = self.new Rack::Request.new(env), Rack::Response.new, action return error_404 node if not node.respond_to? meth - # TODO - # - session node.realize end # @@ -58,9 +56,8 @@ module Zorglub # def realize @content = self.send @action[:method], *@action[:args] - e = Config.engine_proc @action[:engine] - v = view - l = layout + e, v, l = Config.engine_proc(@action[:engine]), view, layout + # TODO compile and cache @content = e.call v, self if e and File.exists? v @content = e.call l, self if e and File.exists? l response.write @content @@ -90,6 +87,10 @@ module Zorglub self.class.r end # + def session + @session ||= Session.new( Config.session_on ? @request.cookies : {} ) + end + # def r File.join map, @action[:method] end diff --git a/lib/zorglub/session.rb b/lib/zorglub/session.rb index b119e2b..da30deb 100644 --- a/lib/zorglub/session.rb +++ b/lib/zorglub/session.rb @@ -4,13 +4,29 @@ require 'securerandom' # module Zorglub # - Config.session_id_length ||= 64 - Config.session_ttl ||= (60 * 60 * 24 * 5) - # class Session # - def gen_session_id - SecureRandom.hex Config.session_id_length + @session_key = 'zorglub.sid' + @session_data = {} + class << self + attr_reader :session_key, :session_data + end + # + def initialize cookies + @sid = cookies[self.class.session_key] + @data = self.class.session_data[@sid]||={} + end + # + def exists? + not @sid.nil? + end + # + def [] idx + @data[idx] + end + # + def []= idx, v + @data[idx] = v end # end -- cgit v1.1-2-g2b99