tpitale-dm_session_store 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.textile ADDED
@@ -0,0 +1,46 @@
1
+ h1. DmSessionStore
2
+
3
+ h2. Description
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.
8
+
9
+ h2. Installation
10
+
11
+ sudo gem install tpitale-dm_session_store
12
+
13
+ h2. Usage
14
+
15
+ In Rails config/environment.rb
16
+
17
+ config.gem 'tpitale-dm_session_store, :lib => 'dm_session_store'
18
+ config.action_controller.session_store = CGI::Session::DMSessionStore
19
+
20
+ Obviously, for this to work, you had better follow one of the many online
21
+ resources for using DataMapper with Rails.
22
+
23
+ h2. License
24
+
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.
data/Rakefile ADDED
@@ -0,0 +1,51 @@
1
+ require 'rubygems'
2
+ require 'rake/gempackagetask'
3
+ require 'rake/testtask'
4
+
5
+ require 'lib/dm_session_store/version'
6
+
7
+ task :default => :test
8
+
9
+ spec = Gem::Specification.new do |s|
10
+ s.name = 'dm_session_store'
11
+ s.version = DmSessionStore::Version.to_s
12
+ s.summary = "Database session store using DataMapper in Rails"
13
+ s.author = 'Tony Pitale'
14
+ s.email = 'tpitale@gmail.com'
15
+ s.homepage = 'http://t.pitale.com'
16
+ s.files = %w(README.textile Rakefile) + Dir.glob("lib/**/*")
17
+ s.test_files = Dir.glob("test/**/*_test.rb")
18
+
19
+ s.add_dependency('dm-core', '~> 0.9.11')
20
+ end
21
+
22
+ Rake::GemPackageTask.new(spec) do |pkg|
23
+ pkg.gem_spec = spec
24
+ end
25
+
26
+ Rake::TestTask.new do |t|
27
+ t.libs << 'test'
28
+ t.test_files = FileList["test/**/*_test.rb"]
29
+ t.verbose = true
30
+ end
31
+
32
+ desc 'Generate the gemspec to serve this Gem from Github'
33
+ task :github do
34
+ file = File.dirname(__FILE__) + "/#{spec.name}.gemspec"
35
+ File.open(file, 'w') {|f| f << spec.to_ruby }
36
+ puts "Created gemspec: #{file}"
37
+ end
38
+
39
+ begin
40
+ require 'rcov/rcovtask'
41
+
42
+ desc "Generate RCov coverage report"
43
+ Rcov::RcovTask.new(:rcov) do |t|
44
+ t.test_files = FileList['test/**/*_test.rb']
45
+ t.rcov_opts << "-x lib/dm_session_store.rb -x lib/dm_session_store/version.rb"
46
+ end
47
+ rescue LoadError
48
+ nil
49
+ end
50
+
51
+ task :default => 'test'
@@ -0,0 +1,85 @@
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
@@ -0,0 +1,13 @@
1
+ module DmSessionStore
2
+ module Version
3
+
4
+ MAJOR = 0
5
+ MINOR = 1
6
+ TINY = 0
7
+
8
+ def self.to_s # :nodoc:
9
+ [MAJOR, MINOR, TINY].join('.')
10
+ end
11
+
12
+ end
13
+ end
@@ -0,0 +1,3 @@
1
+ $:.unshift File.dirname(__FILE__)
2
+
3
+ require 'dm_session_store/session_ext'
@@ -0,0 +1,62 @@
1
+ require File.join(File.dirname(__FILE__), "..", "test_helper")
2
+ require 'cgi'
3
+ require 'cgi/session'
4
+
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
18
+ end
19
+
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
48
+
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
54
+
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
59
+ end
60
+ end
61
+ end
62
+ end
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tpitale-dm_session_store
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Tony Pitale
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-05-02 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: dm-core
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ~>
22
+ - !ruby/object:Gem::Version
23
+ version: 0.9.11
24
+ version:
25
+ description:
26
+ email: tpitale@gmail.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files: []
32
+
33
+ files:
34
+ - README.textile
35
+ - Rakefile
36
+ - lib/dm_session_store
37
+ - lib/dm_session_store/session_ext.rb
38
+ - lib/dm_session_store/version.rb
39
+ - lib/dm_session_store.rb
40
+ - test/unit/dm_session_store_test.rb
41
+ has_rdoc: false
42
+ homepage: http://t.pitale.com
43
+ post_install_message:
44
+ rdoc_options: []
45
+
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: "0"
53
+ version:
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: "0"
59
+ version:
60
+ requirements: []
61
+
62
+ rubyforge_project:
63
+ rubygems_version: 1.2.0
64
+ signing_key:
65
+ specification_version: 2
66
+ summary: Database session store using DataMapper in Rails
67
+ test_files:
68
+ - test/unit/dm_session_store_test.rb