]> git.openstreetmap.org Git - rails.git/blob - vendor/plugins/sql_session_store/lib/abstract_session.rb
Update bundle
[rails.git] / vendor / plugins / sql_session_store / lib / abstract_session.rb
1 #
2 # This is a common base class for database-specific session store implementations
3 #
4 require 'cgi/session'
5 require 'base64'
6
7 class AbstractSession
8   # if you need Rails components, and you have a pages which create
9   # new sessions, and embed components insides this pages that need
10   # session access, then you *must* set +eager_session_creation+ to
11   # true (as of Rails 1.0).
12   cattr_accessor :eager_session_creation
13   @@eager_session_creation = false
14
15   # Some attributes you may want to store natively in the database
16   # in actual columns. This allows other models and database queries
17   # to get to the data without having to unmarshal the data blob.
18   # One common example is the user_id of the session, so it can be
19   # related to the users table
20   cattr_accessor :native_columns
21   @@native_columns = []
22
23   # Allow the user to change the table name
24   cattr_accessor :table_name
25   @@table_name = 'sessions'
26
27   cattr_reader :timestamp_columns
28   @@timestamp_columns = [:created_at, :updated_at]
29
30   attr_accessor :id, :session_id, :data
31
32   def initialize(session_id, data, id=nil)
33     @session_id = session_id
34     @data = data
35     @id = id
36   end
37
38   class << self
39     # delete all sessions meeting a given +condition+. it is the
40     # caller's responsibility to pass a valid sql condition
41     def delete_all(condition=nil)
42       if condition
43         session_connection.exec("DELETE FROM sessions WHERE #{condition}")
44       else
45         session_connection.exec("DELETE FROM sessions")
46       end
47     end
48
49     # retrieve the session table connection and get the 'raw' driver connection from it
50     def session_connection
51       SqlSession.connection.raw_connection
52     end
53
54     def unmarshalize(data)
55       Marshal.load(Base64.decode64(data))
56     end
57
58     def marshalize(data)
59       Base64.encode64(Marshal.dump(data))
60     end
61   end
62 end
63
64 __END__
65
66 # This software is released under the MIT license
67 #
68 # Copyright (c) 2005, 2006, 2008 Stefan Kaes
69 # Copyright (c) 2008, 2009 Nate Wiger
70 #
71 # Permission is hereby granted, free of charge, to any person obtaining
72 # a copy of this software and associated documentation files (the
73 # "Software"), to deal in the Software without restriction, including
74 # without limitation the rights to use, copy, modify, merge, publish,
75 # distribute, sublicense, and/or sell copies of the Software, and to
76 # permit persons to whom the Software is furnished to do so, subject to
77 # the following conditions:
78
79 # The above copyright notice and this permission notice shall be
80 # included in all copies or substantial portions of the Software.
81
82 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
83 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
84 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
85 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
86 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
87 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
88 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.