ActiveRecord session store class.
Create a new ActiveRecordStore object, using the (ActiveRecord::Base subclass) class specified by the given klass_uri.
# File /Users/ged/source/ruby/Arrow/lib/arrow/session/activerecordstore.rb, line 54
54: def initialize( klass_uri, idobj )
55: klass_string = klass_uri.opaque or
56: raise Arrow::ConfigError, "Invalid session storeType URI %p" % [klass_uri]
57: @klass = find_class( klass_string ) or
58: raise Arrow::ConfigError, "No such class for session storeType %s" % [klass_uri]
59: @instance = nil
60:
61: super
62: end
Insert the specified data hash into the database.
# File /Users/ged/source/ruby/Arrow/lib/arrow/session/activerecordstore.rb, line 75
75: def insert
76: dbo = nil
77:
78: super {|data|
79: dbo = self.db_object
80: self.log.debug "Saving session to the database."
81: dbo.session_data = data
82: self.log.debug "Session db object is at version %d" % [dbo.lock_version]
83:
84: unless dbo.save
85: raise Arrow::SessionError,
86: "Could not save session: %s" % [ dbo.errors.full_messages.join(', ') ]
87: end
88: self.log.debug "Session data saved."
89: }
90: rescue ActiveRecord::StaleObjectError => err
91: otherdbo = @klass.find_by_session_id( @id.to_s )
92: self.log.notice "Conflicting session updates: %p" %
93: [ otherdbo.attributes.diff(dbo.attributes) ]
94: # :TODO: Handle merging sessions
95:
96: rescue ActiveRecord::ActiveRecordError => err
97: # :TODO: Give up on the session after logging the error, but don't
98: # propagate the exception.
99: self.log.error "Error inserting/saving session: %s at %s" %
100: [err.message, err.backtrace.first]
101: end
Remove the session data object from the database
# File /Users/ged/source/ruby/Arrow/lib/arrow/session/activerecordstore.rb, line 114
114: def remove
115: self.log.debug "Removing session %s from the database" % [@id.to_s]
116: super
117: @klass.delete( @id.to_s )
118: @instance = nil
119: end
Fetch the session data hash from the session object in the database
# File /Users/ged/source/ruby/Arrow/lib/arrow/session/activerecordstore.rb, line 106
106: def retrieve
107: self.log.debug "Retrieving session %s from the database" % [ @id.to_s ]
108: super { self.db_object.session_data }
109: self.log.debug "Retrieved session %s" % [ @id.to_s ]
110: end
Look up or create the session data object in the database and return it.
# File /Users/ged/source/ruby/Arrow/lib/arrow/session/activerecordstore.rb, line 146
146: def db_object
147: if @instance.nil?
148: self.log.debug "Fetching session object from the database (id = %p)" %
149: [ @id.to_s ]
150: @instance = @klass.find_or_create_by_session_id( @id.to_s )
151: @instance.session_data = {} if @instance.new_record?
152: end
153:
154: return @instance
155: end
--- SEC00101
--- ""
---
- name: klass
rw: R
a_desc: |+
The ActiveRecord::Base (-like?) object which should be instantiated to
store the session data
---
- methods:
- visibility: public
aref: M000134
name: new
sourcecode: " <span class=\"ruby-comment cmt\"># File /Users/ged/source/ruby/Arrow/lib/arrow/session/activerecordstore.rb, line 54</span>\n\
54: <span class=\"ruby-keyword kw\">def</span> <span class=\"ruby-identifier\">initialize</span>( <span class=\"ruby-identifier\">klass_uri</span>, <span class=\"ruby-identifier\">idobj</span> )\n\
55: <span class=\"ruby-identifier\">klass_string</span> = <span class=\"ruby-identifier\">klass_uri</span>.<span class=\"ruby-identifier\">opaque</span> <span class=\"ruby-keyword kw\">or</span>\n\
56: <span class=\"ruby-identifier\">raise</span> <span class=\"ruby-constant\">Arrow</span><span class=\"ruby-operator\">::</span><span class=\"ruby-constant\">ConfigError</span>, <span class=\"ruby-value str\">"Invalid session storeType URI %p"</span> <span class=\"ruby-operator\">%</span> [<span class=\"ruby-identifier\">klass_uri</span>]\n\
57: <span class=\"ruby-ivar\">@klass</span> = <span class=\"ruby-identifier\">find_class</span>( <span class=\"ruby-identifier\">klass_string</span> ) <span class=\"ruby-keyword kw\">or</span>\n\
58: <span class=\"ruby-identifier\">raise</span> <span class=\"ruby-constant\">Arrow</span><span class=\"ruby-operator\">::</span><span class=\"ruby-constant\">ConfigError</span>, <span class=\"ruby-value str\">"No such class for session storeType %s"</span> <span class=\"ruby-operator\">%</span> [<span class=\"ruby-identifier\">klass_uri</span>]\n\
59: <span class=\"ruby-ivar\">@instance</span> = <span class=\"ruby-keyword kw\">nil</span>\n\
60: \n\
61: <span class=\"ruby-keyword kw\">super</span>\n\
62: <span class=\"ruby-keyword kw\">end</span>"
m_desc: |-
<p>
Create a <a href="ActiveRecordStore.html#M000134">new</a> <a
href="ActiveRecordStore.html">ActiveRecordStore</a> object, using the
(ActiveRecord::Base subclass) class specified by the given
<tt>klass_uri</tt>.
</p>
params: ( klass_uri, idobj )
category: Class
type: Public
- methods:
- visibility: public
aref: M000135
name: insert
sourcecode: " <span class=\"ruby-comment cmt\"># File /Users/ged/source/ruby/Arrow/lib/arrow/session/activerecordstore.rb, line 75</span>\n 75: <span class=\"ruby-keyword kw\">def</span> <span class=\"ruby-identifier\">insert</span>\n 76: <span class=\"ruby-identifier\">dbo</span> = <span class=\"ruby-keyword kw\">nil</span>\n 77: \n 78: <span class=\"ruby-keyword kw\">super</span> {<span class=\"ruby-operator\">|</span><span class=\"ruby-identifier\">data</span><span class=\"ruby-operator\">|</span>\n 79: <span class=\"ruby-identifier\">dbo</span> = <span class=\"ruby-keyword kw\">self</span>.<span class=\"ruby-identifier\">db_object</span>\n 80: <span class=\"ruby-keyword kw\">self</span>.<span class=\"ruby-identifier\">log</span>.<span class=\"ruby-identifier\">debug</span> <span class=\"ruby-value str\">"Saving session to the database."</span>\n 81: <span class=\"ruby-identifier\">dbo</span>.<span class=\"ruby-identifier\">session_data</span> = <span class=\"ruby-identifier\">data</span>\n 82: <span class=\"ruby-keyword kw\">self</span>.<span class=\"ruby-identifier\">log</span>.<span class=\"ruby-identifier\">debug</span> <span class=\"ruby-value str\">"Session db object is at version %d"</span> <span class=\"ruby-operator\">%</span> [<span class=\"ruby-identifier\">dbo</span>.<span class=\"ruby-identifier\">lock_version</span>]\n 83: \n 84: <span class=\"ruby-keyword kw\">unless</span> <span class=\"ruby-identifier\">dbo</span>.<span class=\"ruby-identifier\">save</span>\n 85: <span class=\"ruby-identifier\">raise</span> <span class=\"ruby-constant\">Arrow</span><span class=\"ruby-operator\">::</span><span class=\"ruby-constant\">SessionError</span>, \n 86: <span class=\"ruby-value str\">"Could not save session: %s"</span> <span class=\"ruby-operator\">%</span> [ <span class=\"ruby-identifier\">dbo</span>.<span class=\"ruby-identifier\">errors</span>.<span class=\"ruby-identifier\">full_messages</span>.<span class=\"ruby-identifier\">join</span>(<span class=\"ruby-value str\">', '</span>) ]\n 87: <span class=\"ruby-keyword kw\">end</span>\n 88: <span class=\"ruby-keyword kw\">self</span>.<span class=\"ruby-identifier\">log</span>.<span class=\"ruby-identifier\">debug</span> <span class=\"ruby-value str\">"Session data saved."</span>\n 89: }\n 90: <span class=\"ruby-keyword kw\">rescue</span> <span class=\"ruby-constant\">ActiveRecord</span><span class=\"ruby-operator\">::</span><span class=\"ruby-constant\">StaleObjectError</span> =<span class=\"ruby-operator\">></span> <span class=\"ruby-identifier\">err</span>\n 91: <span class=\"ruby-identifier\">otherdbo</span> = <span class=\"ruby-ivar\">@klass</span>.<span class=\"ruby-identifier\">find_by_session_id</span>( <span class=\"ruby-ivar\">@id</span>.<span class=\"ruby-identifier\">to_s</span> )\n 92: <span class=\"ruby-keyword kw\">self</span>.<span class=\"ruby-identifier\">log</span>.<span class=\"ruby-identifier\">notice</span> <span class=\"ruby-value str\">"Conflicting session updates: %p"</span> <span class=\"ruby-operator\">%</span>\n 93: [ <span class=\"ruby-identifier\">otherdbo</span>.<span class=\"ruby-identifier\">attributes</span>.<span class=\"ruby-identifier\">diff</span>(<span class=\"ruby-identifier\">dbo</span>.<span class=\"ruby-identifier\">attributes</span>) ]\n 94: <span class=\"ruby-comment cmt\"># :TODO: Handle merging sessions</span>\n 95: \n 96: <span class=\"ruby-keyword kw\">rescue</span> <span class=\"ruby-constant\">ActiveRecord</span><span class=\"ruby-operator\">::</span><span class=\"ruby-constant\">ActiveRecordError</span> =<span class=\"ruby-operator\">></span> <span class=\"ruby-identifier\">err</span>\n 97: <span class=\"ruby-comment cmt\"># :TODO: Give up on the session after logging the error, but don't </span>\n 98: <span class=\"ruby-comment cmt\"># propagate the exception.</span>\n 99: <span class=\"ruby-keyword kw\">self</span>.<span class=\"ruby-identifier\">log</span>.<span class=\"ruby-identifier\">error</span> <span class=\"ruby-value str\">"Error inserting/saving session: %s at %s"</span> <span class=\"ruby-operator\">%</span>\n\
100: [<span class=\"ruby-identifier\">err</span>.<span class=\"ruby-identifier\">message</span>, <span class=\"ruby-identifier\">err</span>.<span class=\"ruby-identifier\">backtrace</span>.<span class=\"ruby-identifier\">first</span>]\n\
101: <span class=\"ruby-keyword kw\">end</span>"
aka:
- aref: ActiveRecordStore.html#M000136
name: update
m_desc: |-
<p>
Insert the specified <tt>data</tt> hash into the database.
</p>
params: ()
- visibility: public
aref: M000138
name: remove
sourcecode: " <span class=\"ruby-comment cmt\"># File /Users/ged/source/ruby/Arrow/lib/arrow/session/activerecordstore.rb, line 114</span>\n\
114: <span class=\"ruby-keyword kw\">def</span> <span class=\"ruby-identifier\">remove</span>\n\
115: <span class=\"ruby-keyword kw\">self</span>.<span class=\"ruby-identifier\">log</span>.<span class=\"ruby-identifier\">debug</span> <span class=\"ruby-value str\">"Removing session %s from the database"</span> <span class=\"ruby-operator\">%</span> [<span class=\"ruby-ivar\">@id</span>.<span class=\"ruby-identifier\">to_s</span>]\n\
116: <span class=\"ruby-keyword kw\">super</span>\n\
117: <span class=\"ruby-ivar\">@klass</span>.<span class=\"ruby-identifier\">delete</span>( <span class=\"ruby-ivar\">@id</span>.<span class=\"ruby-identifier\">to_s</span> )\n\
118: <span class=\"ruby-ivar\">@instance</span> = <span class=\"ruby-keyword kw\">nil</span>\n\
119: <span class=\"ruby-keyword kw\">end</span>"
m_desc: |-
<p>
Remove the session data object from the database
</p>
params: ()
- visibility: public
aref: M000137
name: retrieve
sourcecode: " <span class=\"ruby-comment cmt\"># File /Users/ged/source/ruby/Arrow/lib/arrow/session/activerecordstore.rb, line 106</span>\n\
106: <span class=\"ruby-keyword kw\">def</span> <span class=\"ruby-identifier\">retrieve</span>\n\
107: <span class=\"ruby-keyword kw\">self</span>.<span class=\"ruby-identifier\">log</span>.<span class=\"ruby-identifier\">debug</span> <span class=\"ruby-value str\">"Retrieving session %s from the database"</span> <span class=\"ruby-operator\">%</span> [ <span class=\"ruby-ivar\">@id</span>.<span class=\"ruby-identifier\">to_s</span> ]\n\
108: <span class=\"ruby-keyword kw\">super</span> { <span class=\"ruby-keyword kw\">self</span>.<span class=\"ruby-identifier\">db_object</span>.<span class=\"ruby-identifier\">session_data</span> }\n\
109: <span class=\"ruby-keyword kw\">self</span>.<span class=\"ruby-identifier\">log</span>.<span class=\"ruby-identifier\">debug</span> <span class=\"ruby-value str\">"Retrieved session %s"</span> <span class=\"ruby-operator\">%</span> [ <span class=\"ruby-ivar\">@id</span>.<span class=\"ruby-identifier\">to_s</span> ]\n\
110: <span class=\"ruby-keyword kw\">end</span>"
m_desc: |-
<p>
Fetch the session data hash from the session object in the database
</p>
params: ()
- visibility: public
aref: M000136
name: update
m_desc: |-
<p>
Alias for <a href="ActiveRecordStore.html#M000135">#insert</a>
</p>
params: ()
category: Instance
type: Public
- methods:
- visibility: protected
aref: M000139
name: db_object
sourcecode: " <span class=\"ruby-comment cmt\"># File /Users/ged/source/ruby/Arrow/lib/arrow/session/activerecordstore.rb, line 146</span>\n\
146: <span class=\"ruby-keyword kw\">def</span> <span class=\"ruby-identifier\">db_object</span>\n\
147: <span class=\"ruby-keyword kw\">if</span> <span class=\"ruby-ivar\">@instance</span>.<span class=\"ruby-identifier\">nil?</span>\n\
148: <span class=\"ruby-keyword kw\">self</span>.<span class=\"ruby-identifier\">log</span>.<span class=\"ruby-identifier\">debug</span> <span class=\"ruby-value str\">"Fetching session object from the database (id = %p)"</span> <span class=\"ruby-operator\">%</span>\n\
149: [ <span class=\"ruby-ivar\">@id</span>.<span class=\"ruby-identifier\">to_s</span> ]\n\
150: <span class=\"ruby-ivar\">@instance</span> = <span class=\"ruby-ivar\">@klass</span>.<span class=\"ruby-identifier\">find_or_create_by_session_id</span>( <span class=\"ruby-ivar\">@id</span>.<span class=\"ruby-identifier\">to_s</span> )\n\
151: <span class=\"ruby-ivar\">@instance</span>.<span class=\"ruby-identifier\">session_data</span> = {} <span class=\"ruby-keyword kw\">if</span> <span class=\"ruby-ivar\">@instance</span>.<span class=\"ruby-identifier\">new_record?</span>\n\
152: <span class=\"ruby-keyword kw\">end</span>\n\
153: \n\
154: <span class=\"ruby-keyword kw\">return</span> <span class=\"ruby-ivar\">@instance</span>\n\
155: <span class=\"ruby-keyword kw\">end</span>"
m_desc: |-
<p>
Look up or create the session data object in the database and return it.
</p>
params: ()
category: Instance
type: Protected
---
---
- name: SVNRev
desc: |+
SVN Revision
value: "%q$Rev: 432 $"
- name: SVNId
desc: |+
SVN <a href="Id.html">Id</a>
value: "%q$Id: activerecordstore.rb 432 2008-02-20 19:26:10Z deveiant $"
- name: RecommendedLocker
desc: |+
The name of the recommended locker to use with this session store type.
Since ActiveRecord does its own optimistic locking, just use the null
locker.
value: "'null'"
Generated with the Darkfish Rdoc Generator.