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

Instance Method Summary collapse

Constructor Details

#initialize(store, id) ⇒ POXReference

Returns a new instance of POXReference.



42
43
44
45
46
# File 'lib/perobs/ObjectBase.rb', line 42

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.



49
50
51
52
53
54
55
56
57
58
# 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

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



40
41
42
# File 'lib/perobs/ObjectBase.rb', line 40

def id
  @id
end

#storeObject (readonly)

Returns the value of attribute store.



40
41
42
# File 'lib/perobs/ObjectBase.rb', line 40

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.

Parameters:

  • obj

    object to compare this object with.



85
86
87
# File 'lib/perobs/ObjectBase.rb', line 85

def ==(obj)
  _referenced_object == obj
end

#_idObject

Shortcut to access the _id() method of the referenced object.



106
107
108
# File 'lib/perobs/ObjectBase.rb', line 106

def _id
  @id
end

#_referenced_objectObjectBase

not be used outside of the PEROBS library. Leaked references can cause data corruption.

Returns:

  • (ObjectBase)

    Return the referenced object. This method should



77
78
79
# File 'lib/perobs/ObjectBase.rb', line 77

def _referenced_object
  @store.object_by_id(@id)
end

#eql?(obj) ⇒ Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/perobs/ObjectBase.rb', line 89

def eql?(obj)
  _referenced_object._id == obj._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.

Parameters:

  • obj

    object to compare this object with.

Returns:

  • (Boolean)


97
98
99
100
101
102
103
# 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

#is_poxreference?Boolean

Just for completeness. We don’t want to be caught lying.

Returns:

  • (Boolean)


70
71
72
# File 'lib/perobs/ObjectBase.rb', line 70

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.

Returns:

  • (Boolean)


64
65
66
67
# 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