class PEROBS::POXReference
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. The POXReference
objects function as a transparent proxy for the objects they are referencing.
Attributes
Public Class Methods
# File lib/perobs/ObjectBase.rb, line 42 def initialize(store, id) super() @store = store @id = id end
Public Instance Methods
BasicObject provides a ==() method that prevents method_missing
from being called. So we have to pass the call manually to the referenced object. @param obj object to compare this object with.
# File lib/perobs/ObjectBase.rb, line 85 def ==(obj) _referenced_object == obj end
Shortcut to access the _id() method of the referenced object.
# File lib/perobs/ObjectBase.rb, line 113 def _id @id end
@return [ObjectBase] Return the referenced object. This method should not be used outside of the PEROBS
library. Leaked references can cause data corruption.
# File lib/perobs/ObjectBase.rb, line 77 def _referenced_object @store.object_by_id(@id) end
# File lib/perobs/ObjectBase.rb, line 89 def eql?(obj) _referenced_object._id == obj._id end
BasicObject provides a equal?() method that prevents method_missing
from being called. So we have to pass the call manually to the referenced object. @param obj object to compare this object with.
# File lib/perobs/ObjectBase.rb, line 97 def equal?(obj) if obj.respond_to?(:is_poxreference?) _referenced_object.equal?(obj._referenced_object) else _referenced_object.equal?(obj) end end
To allow POXReference
objects to be used as Hash
keys we need to implement this function. Conveniently, we can just use the PEROBS
object ID since that is unique.
# File lib/perobs/ObjectBase.rb, line 108 def hash @id end
Just for completeness. We don't want to be caught lying.
# File lib/perobs/ObjectBase.rb, line 70 def is_poxreference? true end
Proxy all calls to unknown methods to the referenced object.
# File lib/perobs/ObjectBase.rb, line 49 def method_missing(method_sym, *args, &block) unless (obj = _referenced_object) ::PEROBS.log.fatal "Internal consistency error. No object with ID " + "#{@id} found in the store." end if obj.respond_to?(:is_poxreference?) ::PEROBS.log.fatal "POXReference that references a POXReference found." end obj.send(method_sym, *args, &block) end
Proxy all calls to unknown methods to the referenced object. Calling respond_to?(:is_poxreference?) is the only reliable way to find out if the object is a POXReference
or not as pretty much every method call is proxied to the referenced object.
# File lib/perobs/ObjectBase.rb, line 64 def respond_to?(method_sym, include_private = false) (method_sym == :is_poxreference?) || _referenced_object.respond_to?(method_sym, include_private) end