rack-datamapper-session 0.1

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,13 @@
1
+ DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
2
+ Version 2, December 2004
3
+
4
+ Copyright (C) 2004 Sam Hocevar
5
+ 14 rue de Plaisance, 75014 Paris, France
6
+ Everyone is permitted to copy and distribute verbatim or modified
7
+ copies of this license document, and changing it is allowed as long
8
+ as the name is changed.
9
+
10
+ DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
11
+ TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
12
+
13
+ 0. You just DO WHAT THE FUCK YOU WANT TO.
data/README ADDED
@@ -0,0 +1,7 @@
1
+ To use with Sinatra, for example:
2
+
3
+ require 'rack-datamapper-session'
4
+ use Rack::Session::DataMapper
5
+ DataMapper.auto_upgrade!
6
+
7
+ Licensed under Do What The Fuck You Want To Public License: http://en.wikipedia.org/wiki/WTFPL
@@ -0,0 +1,64 @@
1
+ require 'rack/session/abstract/id'
2
+
3
+ module Rack
4
+ module Session
5
+ class DataMapperSession
6
+ include DataMapper::Resource
7
+
8
+ property :id, Serial
9
+ property :sid, String, :length => 128
10
+ property :data_object, Object
11
+
12
+ def [] key
13
+ data[key]
14
+ end
15
+
16
+ def []= key, value
17
+ data[key] = value
18
+ end
19
+
20
+ def data
21
+ self.data_object ||= {}
22
+ end
23
+
24
+ def data= new_data
25
+ self.data_object = new_data
26
+ end
27
+ end
28
+
29
+ class DataMapper < Abstract::ID
30
+ def get_session(env, sid)
31
+ session = DataMapperSession.first(:sid => sid) if sid
32
+
33
+ unless sid and session
34
+ env['rack.errors'].puts("Session '#{sid.inspect}' not found, initializing...") if $VERBOSE and not sid.nil?
35
+ sid = generate_sid
36
+ session = DataMapperSession.new(:sid => sid)
37
+ raise 'Unable to store session' unless session.save
38
+ end
39
+
40
+ # !!! check expiry
41
+ return [sid, session]
42
+ end
43
+
44
+ def set_session(env, sid, new_session, options)
45
+ expiry = options[:expire_after]
46
+ expiry = expiry.nil? ? 0 : expiry + 1
47
+
48
+ session = DataMapperSession.first(:sid => sid) if sid
49
+
50
+ if options[:renew] or options[:drop]
51
+ session.destroy
52
+ return false if options[:drop]
53
+ sid = generate_sid
54
+ session = DataMapperSession.new(:sid => sid)
55
+ end
56
+
57
+ session.data = new_session.data
58
+ raise 'Unable to update session' unless session.save
59
+
60
+ return sid
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,16 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = 'rack-datamapper-session'
5
+ s.version = '0.1'
6
+
7
+ s.authors = ['Phil Pirozhkov']
8
+ s.date = '2009-11-03'
9
+ s.description = "Datamapper session for any Rack based application. Don't forget to automigrate."
10
+ s.email = ['pirj@mail.ru']
11
+ s.files = ['rack-datamapper-session.gemspec', 'README', 'LICENSE', 'lib/rack-datamapper-session.rb']
12
+ s.summary ='Datamapper session for any Rack based application'
13
+
14
+ s.add_dependency 'rack', '>= 1.0'
15
+ s.add_dependency 'dm-core', '>= 0.9'
16
+ end
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-datamapper-session
3
+ version: !ruby/object:Gem::Version
4
+ version: "0.1"
5
+ platform: ruby
6
+ authors:
7
+ - Phil Pirozhkov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-11-03 00:00:00 +03:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rack
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "1.0"
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: dm-core
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: "0.9"
34
+ version:
35
+ description: Datamapper session for any Rack based application. Don't forget to automigrate.
36
+ email:
37
+ - pirj@mail.ru
38
+ executables: []
39
+
40
+ extensions: []
41
+
42
+ extra_rdoc_files: []
43
+
44
+ files:
45
+ - rack-datamapper-session.gemspec
46
+ - README
47
+ - LICENSE
48
+ - lib/rack-datamapper-session.rb
49
+ has_rdoc: true
50
+ homepage:
51
+ licenses: []
52
+
53
+ post_install_message:
54
+ rdoc_options: []
55
+
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: "0"
63
+ version:
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: "0"
69
+ version:
70
+ requirements: []
71
+
72
+ rubyforge_project:
73
+ rubygems_version: 1.3.5
74
+ signing_key:
75
+ specification_version: 3
76
+ summary: Datamapper session for any Rack based application
77
+ test_files: []
78
+