Class: PEROBS::POXReference
- Inherits:
- BasicObject
- Defined in:
- lib/perobs/ObjectBase.rb
Overview
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.
Instance Attribute Summary collapse
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#store ⇒ Object
readonly
Returns the value of attribute store.
Instance Method Summary collapse
-
#==(obj) ⇒ Object
BasicObject provides a ==() method that prevents method_missing from being called.
-
#_id ⇒ Object
Shortcut to access the _id() method of the referenced object.
-
#_referenced_object ⇒ ObjectBase
not be used outside of the PEROBS library.
-
#equal?(obj) ⇒ Boolean
BasicObject provides a equal?() method that prevents method_missing from being called.
-
#initialize(store, id) ⇒ POXReference
constructor
A new instance of POXReference.
-
#is_poxreference? ⇒ Boolean
Just for completeness.
-
#method_missing(method_sym, *args, &block) ⇒ Object
Proxy all calls to unknown methods to the referenced object.
-
#respond_to?(method_sym, include_private = false) ⇒ Boolean
Proxy all calls to unknown methods to the referenced object.
Constructor Details
#initialize(store, id) ⇒ POXReference
Returns a new instance of POXReference.
41 42 43 44 45 |
# File 'lib/perobs/ObjectBase.rb', line 41 def initialize(store, id) super() @store = store @id = id end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_sym, *args, &block) ⇒ Object
Proxy all calls to unknown methods to the referenced object.
48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/perobs/ObjectBase.rb', line 48 def method_missing(method_sym, *args, &block) unless (obj = _referenced_object) ::Kernel.raise ::RuntimeError, "Internal consistency error. No object with ID #{@id} found in " + 'the store.' end if obj.respond_to?(:is_poxreference?) ::Kernel.raise ::RuntimeError, "POXReference that references a POXReference found." end obj.send(method_sym, *args, &block) end |
Instance Attribute Details
#id ⇒ Object (readonly)
Returns the value of attribute id.
39 40 41 |
# File 'lib/perobs/ObjectBase.rb', line 39 def id @id end |
#store ⇒ Object (readonly)
Returns the value of attribute store.
39 40 41 |
# File 'lib/perobs/ObjectBase.rb', line 39 def store @store end |
Instance Method Details
#==(obj) ⇒ Object
BasicObject provides a ==() method that prevents method_missing from being called. So we have to pass the call manually to the referenced object.
86 87 88 |
# File 'lib/perobs/ObjectBase.rb', line 86 def ==(obj) _referenced_object == obj end |
#_id ⇒ Object
Shortcut to access the _id() method of the referenced object.
103 104 105 |
# File 'lib/perobs/ObjectBase.rb', line 103 def _id @id end |
#_referenced_object ⇒ ObjectBase
not be used outside of the PEROBS library. Leaked references can cause data corruption.
78 79 80 |
# File 'lib/perobs/ObjectBase.rb', line 78 def _referenced_object @store.object_by_id(@id) end |
#equal?(obj) ⇒ Boolean
BasicObject provides a equal?() method that prevents method_missing from being called. So we have to pass the call manually to the referenced object.
94 95 96 97 98 99 100 |
# File 'lib/perobs/ObjectBase.rb', line 94 def equal?(obj) if obj.respond_to?(:is_poxreference?) _referenced_object.equal?(obj._referenced_object) else _referenced_object.equal?(obj) end end |
#is_poxreference? ⇒ Boolean
Just for completeness. We don’t want to be caught lying.
71 72 73 |
# File 'lib/perobs/ObjectBase.rb', line 71 def is_poxreference? true end |
#respond_to?(method_sym, include_private = false) ⇒ Boolean
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.
65 66 67 68 |
# File 'lib/perobs/ObjectBase.rb', line 65 def respond_to?(method_sym, include_private = false) (method_sym == :is_poxreference?) || _referenced_object.respond_to?(method_sym, include_private) end |