From db045dbbf60b53dbe013ef25554fd013baf88134 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Wed, 24 Nov 2004 01:04:44 +0000 Subject: Initial git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- .../session/active_record_store.rb | 72 ++++++++++++++++++++++ .../lib/action_controller/session/drb_server.rb | 9 +++ .../lib/action_controller/session/drb_store.rb | 31 ++++++++++ 3 files changed, 112 insertions(+) create mode 100644 actionpack/lib/action_controller/session/active_record_store.rb create mode 100644 actionpack/lib/action_controller/session/drb_server.rb create mode 100644 actionpack/lib/action_controller/session/drb_store.rb (limited to 'actionpack/lib/action_controller/session') diff --git a/actionpack/lib/action_controller/session/active_record_store.rb b/actionpack/lib/action_controller/session/active_record_store.rb new file mode 100644 index 0000000000..c144f62e35 --- /dev/null +++ b/actionpack/lib/action_controller/session/active_record_store.rb @@ -0,0 +1,72 @@ +begin + +require 'active_record' +require 'cgi' +require 'cgi/session' + +# Contributed by Tim Bates +class CGI + class Session + # ActiveRecord database based session storage class. + # + # Implements session storage in a database using the ActiveRecord ORM library. Assumes that the database + # has a table called +sessions+ with columns +id+ (numeric, primary key), +sessid+ and +data+ (text). + # The session data is stored in the +data+ column in YAML format; the user is responsible for ensuring that + # only data that can be YAMLized is stored in the session. + class ActiveRecordStore + # The ActiveRecord class which corresponds to the database table. + class Session < ActiveRecord::Base + serialize :data + # Isn't this class definition beautiful? + end + + # Create a new ActiveRecordStore instance. This constructor is used internally by CGI::Session. + # The user does not generally need to call it directly. + # + # +session+ is the session for which this instance is being created. + # + # +option+ is currently ignored as no options are recognized. + # + # This session's ActiveRecord database row will be created if it does not exist, or opened if it does. + def initialize(session, option=nil) + @session = Session.find_first(["sessid = '%s'", session.session_id]) + if @session + @data = @session.data + else + @session = Session.new("sessid" => session.session_id, "data" => {}) + end + end + + # Update and close the session's ActiveRecord object. + def close + return unless @session + update + @session = nil + end + + # Close and destroy the session's ActiveRecord object. + def delete + return unless @session + @session.destroy + @session = nil + end + + # Restore session state from the session's ActiveRecord object. + def restore + return unless @session + @data = @session.data + end + + # Save session state in the session's ActiveRecord object. + def update + return unless @session + @session.data = @data + @session.save + end + end #ActiveRecordStore + end #Session +end #CGI + +rescue LoadError + # Couldn't load Active Record, so don't make this store available +end \ No newline at end of file diff --git a/actionpack/lib/action_controller/session/drb_server.rb b/actionpack/lib/action_controller/session/drb_server.rb new file mode 100644 index 0000000000..6005b8b2b3 --- /dev/null +++ b/actionpack/lib/action_controller/session/drb_server.rb @@ -0,0 +1,9 @@ +#!/usr/local/bin/ruby -w + +# This is a really simple session storage daemon, basically just a hash, +# which is enabled for DRb access. + +require 'drb' + +DRb.start_service('druby://127.0.0.1:9192', Hash.new) +DRb.thread.join \ No newline at end of file diff --git a/actionpack/lib/action_controller/session/drb_store.rb b/actionpack/lib/action_controller/session/drb_store.rb new file mode 100644 index 0000000000..8ea23e8fff --- /dev/null +++ b/actionpack/lib/action_controller/session/drb_store.rb @@ -0,0 +1,31 @@ +require 'cgi' +require 'cgi/session' +require 'drb' + +class CGI #:nodoc:all + class Session + class DRbStore + @@session_data = DRbObject.new(nil, 'druby://localhost:9192') + + def initialize(session, option=nil) + @session_id = session.session_id + end + + def restore + @h = @@session_data[@session_id] || {} + end + + def update + @@session_data[@session_id] = @h + end + + def close + update + end + + def delete + @@session_data.delete(@session_id) + end + end + end +end -- cgit v1.2.3