tpitale-dm_session_store 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.textile CHANGED
@@ -2,45 +2,51 @@ h1. DmSessionStore
2
2
 
3
3
  h2. Description
4
4
 
5
- For those using DataMapper with Rails there was no option for storing sessions
6
- in a database (most people remove ActiveRecord). This little gem adds the
7
- required classes to use DataMapper to store sessions in the database.
5
+ For those using DataMapper with Rails there was no option for storing sessions
6
+ in a database (most people remove ActiveRecord). This little gem adds the
7
+ required classes to use DataMapper to store sessions in the database.
8
+
9
+ Now supports Rails >= 2.3.0 in version 0.2.0. If you need support before that,
10
+ try version 0.1.0.
8
11
 
9
12
  h2. Installation
10
13
 
11
- sudo gem install tpitale-dm_session_store
14
+ sudo gem install tpitale-dm_session_store
12
15
 
13
16
  h2. Usage
14
17
 
15
- In Rails config/environment.rb
18
+ In Rails config/environment.rb
19
+
20
+ config.gem 'tpitale-dm_session_store, :lib => 'dm_session_store'
21
+
22
+ In the session_store initializer:
16
23
 
17
- config.gem 'tpitale-dm_session_store, :lib => 'dm_session_store'
18
- config.action_controller.session_store = CGI::Session::DMSessionStore
24
+ ActionController::Base.session_store = DataMapper::SessionStore
19
25
 
20
- Obviously, for this to work, you had better follow one of the many online
21
- resources for using DataMapper with Rails.
26
+ Obviously, for this to work, you had better follow one of the many online
27
+ resources for using DataMapper with Rails.
22
28
 
23
29
  h2. License
24
30
 
25
- Copyright (c) 2009 Tony Pitale
26
-
27
- Permission is hereby granted, free of charge, to any person
28
- obtaining a copy of this software and associated documentation
29
- files (the "Software"), to deal in the Software without
30
- restriction, including without limitation the rights to use,
31
- copy, modify, merge, publish, distribute, sublicense, and/or sell
32
- copies of the Software, and to permit persons to whom the
33
- Software is furnished to do so, subject to the following
34
- conditions:
35
-
36
- The above copyright notice and this permission notice shall be
37
- included in all copies or substantial portions of the Software.
38
-
39
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
40
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
41
- OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
42
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
43
- HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
44
- WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
45
- FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
46
- OTHER DEALINGS IN THE SOFTWARE.
31
+ Copyright (c) 2009 Tony Pitale
32
+
33
+ Permission is hereby granted, free of charge, to any person
34
+ obtaining a copy of this software and associated documentation
35
+ files (the "Software"), to deal in the Software without
36
+ restriction, including without limitation the rights to use,
37
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
38
+ copies of the Software, and to permit persons to whom the
39
+ Software is furnished to do so, subject to the following
40
+ conditions:
41
+
42
+ The above copyright notice and this permission notice shall be
43
+ included in all copies or substantial portions of the Software.
44
+
45
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
46
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
47
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
48
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
49
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
50
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
51
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
52
+ OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile CHANGED
@@ -17,6 +17,7 @@ spec = Gem::Specification.new do |s|
17
17
  s.test_files = Dir.glob("test/**/*_test.rb")
18
18
 
19
19
  s.add_dependency('dm-core', '~> 0.9.11')
20
+ s.add_dependency('actionpack', '~> 2.3.0')
20
21
  end
21
22
 
22
23
  Rake::GemPackageTask.new(spec) do |pkg|
@@ -0,0 +1,37 @@
1
+ require 'dm-core'
2
+
3
+ module DataMapper
4
+ class SessionStore < ActionController::Session::AbstractStore
5
+ class Session
6
+ include DataMapper::Resource
7
+
8
+ property :session_id, String, :size => 32, :nullable => false, :key => true
9
+ property :data, Object, :default => {}, :lazy => false
10
+ property :created_at, DateTime, :default => Proc.new { |r, p| DateTime.now }
11
+ end
12
+
13
+ # The class used for session storage.
14
+ cattr_accessor :session_class
15
+ self.session_class = Session
16
+
17
+ SESSION_RECORD_KEY = 'rack.session.record'.freeze
18
+
19
+ private
20
+ def get_session(env, sid)
21
+ session = find_session(sid)
22
+ env[SESSION_RECORD_KEY] = session
23
+ [sid, session.data]
24
+ end
25
+
26
+ def set_session(env, sid, session_data)
27
+ record = env[SESSION_RECORD_KEY] ||= find_session(sid)
28
+ record.data = session_data
29
+ record.save
30
+ end
31
+
32
+ def find_session(id)
33
+ @@session_class.first(:session_id => id) ||
34
+ @@session_class.new(:session_id => id, :data => {})
35
+ end
36
+ end
37
+ end
@@ -2,7 +2,7 @@ module DmSessionStore
2
2
  module Version
3
3
 
4
4
  MAJOR = 0
5
- MINOR = 1
5
+ MINOR = 2
6
6
  TINY = 0
7
7
 
8
8
  def self.to_s # :nodoc:
@@ -1,3 +1,3 @@
1
1
  $:.unshift File.dirname(__FILE__)
2
2
 
3
- require 'dm_session_store/session_ext'
3
+ require 'dm_session_store/session_store'
@@ -1,61 +1,35 @@
1
1
  require File.join(File.dirname(__FILE__), "..", "test_helper")
2
- require 'cgi'
3
- require 'cgi/session'
4
2
 
5
- class CGI
6
- class Session
7
-
8
- class DMSessionStoreTest < Test::Unit::TestCase
9
- context "The DMSessionStore initialize method" do
10
- should "raise CGI:Session::NoSession if session is not a new_session" do
11
- DMSessionStore::DMSession.stubs(:find_by_session_id).with("1").returns(nil)
12
- session = DMSessionStore::DMSession.new(:session_id => "1")
13
- session.stubs(:new_session).with().returns(false)
14
- assert_raise CGI::Session::NoSession, "uninitialized session" do
15
- DMSessionStore.new(session)
16
- end
17
- end
3
+ module DataMapper
4
+ class SessionStoreTest < Test::Unit::TestCase
5
+ context "An instance of the SessionStore class" do
6
+ setup do
7
+ @session_klass = SessionStore::Session
8
+ @session_store = SessionStore.new('app')
18
9
  end
19
10
 
20
- context "An instance of the DMSessionStore class" do
21
- setup do
22
- @session = DMSessionStore::DMSession.new(:session_id => "1", :data => {:hello => 'world'})
23
- DMSessionStore::DMSession.stubs(:find_by_session_id).with("1").returns(nil)
24
- old_session = DMSessionStore::DMSession.new(:session_id => "1")
25
- old_session.stubs(:new_session).with().returns(true)
26
- DMSessionStore::DMSession.stubs(:new).with(:session_id => "1", :data => {}).returns(@session)
27
- @session_store = DMSessionStore.new(old_session)
28
- end
29
-
30
- should "have the session" do
31
- assert_equal @session, @session_store.model
32
- end
33
-
34
- should "return the data from session" do
35
- assert_equal({:hello => 'world'}, @session_store.restore)
36
- end
37
-
38
- should "save the session" do
39
- @session.expects(:save).with().returns(true)
40
- assert_equal true, @session_store.update
41
- end
42
-
43
- should "save and unreference the session" do
44
- @session_store.expects(:update)
45
- @session_store.close
46
- assert_equal nil, @session_store.model
47
- end
11
+ should "get a session" do
12
+ env = {}
13
+ session = stub(:data => 'data')
14
+ @session_store.expects(:find_session).with(1).returns(session)
15
+ assert_equal([1, 'data'], @session_store.send(:get_session, env, 1))
16
+ assert_equal({'rack.session.record' => session}, env)
17
+ end
48
18
 
49
- should "destroy and unreference the session" do
50
- @session.expects(:destroy)
51
- @session_store.delete
52
- assert_equal nil, @session_store.model
53
- end
19
+ should "set a session" do
20
+ env = {}
21
+ session = stub
22
+ session.expects(:data=).with({:color => 'yellow'})
23
+ session.stubs(:save).returns(true)
24
+ @session_store.expects(:find_session).with(1).returns(session)
25
+ assert @session_store.send(:set_session, env, 1, {:color => 'yellow'})
26
+ assert_equal({'rack.session.record' => session}, env)
27
+ end
54
28
 
55
- should "log with DataMapper" do
56
- DataMapper::Logger.expects(:new).with(STDERR, :fatal).returns('logger')
57
- assert_equal 'logger', @session_store.send(:logger)
58
- end
29
+ should "find a session" do
30
+ @session_klass.expects(:first).with(:session_id => 1).returns(nil)
31
+ @session_klass.expects(:new).with(:session_id => 1, :data => {}).returns('session')
32
+ assert_equal 'session', @session_store.send(:find_session, 1)
59
33
  end
60
34
  end
61
35
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tpitale-dm_session_store
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tony Pitale
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-05-02 00:00:00 -07:00
12
+ date: 2009-05-03 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -22,6 +22,16 @@ dependencies:
22
22
  - !ruby/object:Gem::Version
23
23
  version: 0.9.11
24
24
  version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: actionpack
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 2.3.0
34
+ version:
25
35
  description:
26
36
  email: tpitale@gmail.com
27
37
  executables: []
@@ -34,7 +44,7 @@ files:
34
44
  - README.textile
35
45
  - Rakefile
36
46
  - lib/dm_session_store
37
- - lib/dm_session_store/session_ext.rb
47
+ - lib/dm_session_store/session_store.rb
38
48
  - lib/dm_session_store/version.rb
39
49
  - lib/dm_session_store.rb
40
50
  - test/unit/dm_session_store_test.rb
@@ -1,85 +0,0 @@
1
- require 'cgi'
2
- require 'cgi/session'
3
- require 'dm-core'
4
-
5
- class CGI
6
- class Session
7
- attr_reader :data
8
-
9
- # Return this session's underlying Session instance. Useful for the DB-backed session stores.
10
- def model
11
- @dbman.model if @dbman
12
- end
13
-
14
- class DMSessionStore
15
- class DMSession
16
- include DataMapper::Resource
17
-
18
- storage_names[:default] = 'sessions'
19
-
20
- property :session_id, String, :size => 32, :nullable => false, :key => true
21
- property :data, Object, :default => {}, :lazy => false
22
- property :created_at, DateTime, :default => Proc.new { |r, p| DateTime.now }
23
-
24
- def self.find_by_session_id(session_id)
25
- first(:session_id => session_id)
26
- end
27
- end
28
-
29
- # The class used for session storage.
30
- cattr_accessor :session_class
31
- self.session_class = DMSession
32
-
33
- # Find or instantiate a session given a CGI::Session.
34
- def initialize(session, option = nil)
35
- session_id = session.session_id
36
- unless @session = @@session_class.find_by_session_id(session_id)
37
- unless session.new_session
38
- raise CGI::Session::NoSession, 'uninitialized session'
39
- end
40
- @session = @@session_class.new(:session_id => session_id, :data => {})
41
- end
42
- end
43
-
44
- # Access the underlying session model.
45
- def model
46
- @session
47
- end
48
-
49
- # Restore session state. The session model handles unmarshaling.
50
- def restore
51
- if @session
52
- @session.data
53
- end
54
- end
55
-
56
- # Save session store.
57
- def update
58
- if @session
59
- @session.save
60
- end
61
- end
62
-
63
- # Save and close the session store.
64
- def close
65
- if @session
66
- update
67
- @session = nil
68
- end
69
- end
70
-
71
- # Delete and close the session store.
72
- def delete
73
- if @session
74
- @session.destroy
75
- @session = nil
76
- end
77
- end
78
-
79
- protected
80
- def logger
81
- DataMapper::Logger.new(STDERR, :fatal) rescue nil
82
- end
83
- end
84
- end
85
- end