lib/perobs/ObjectBase.rb in perobs-0.0.1 vs lib/perobs/ObjectBase.rb in perobs-1.0.0
- old
+ new
@@ -23,17 +23,26 @@
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+require 'perobs/ClassMap'
+
module PEROBS
# This class is used to replace a direct reference to another Ruby object by
# the Store ID. This makes object disposable by the Ruby garbage collector
# since it's no longer referenced once it has been evicted from the
# PEROBS::Store cache.
class POReference < Struct.new(:id)
+
+ # Textual dump for debugging purposes
+ # @return [String]
+ def inspect
+ "@#{id}"
+ end
+
end
# Base class for all persistent objects. It provides the functionality
# common to all classes of persistent objects.
class ObjectBase
@@ -60,11 +69,11 @@
# Reset the stash map to ensure that it's reset before the next
# transaction is being started.
@_stash_map = nil
db_obj = {
- 'class' => self.class.to_s,
+ 'class_id' => @store.class_map.class_to_id(self.class.to_s),
'data' => _serialize
}
@store.db.put_object(db_obj, @_id)
end
@@ -72,11 +81,12 @@
# instantiate a new object of the specific type.
def ObjectBase.read(store, id)
# Read the object from database.
db_obj = store.db.get_object(id)
+ klass = store.class_map.id_to_class(db_obj['class_id'])
# Call the constructor of the specified class.
- obj = Object.const_get(db_obj['class']).new(store)
+ obj = Object.const_get(klass).new(store)
# The object gets created with a new ID by default. We need to restore
# the old one.
obj._change_id(id)
obj._deserialize(db_obj['data'])