Class: PEROBS::Hash
- Inherits:
-
ObjectBase
- Object
- ObjectBase
- PEROBS::Hash
- Defined in:
- lib/perobs/Hash.rb
Overview
A Hash that is transparently persisted in the back-end storage. It is very similar to the Ruby built-in Hash class but has some additional limitations. The hash key must always be a String.
The implementation is largely a proxy around the standard Hash class. But all mutating methods must be re-implemented to convert PEROBS::Objects to POXReference objects and to register the object as modified with the cache.
We explicitely don’t support Hash::store() as it conflicts with ObjectBase::store() method to access the store.
Instance Attribute Summary
Attributes inherited from ObjectBase
Instance Method Summary collapse
-
#_delete_reference_to_id(id) ⇒ Object
This method should only be used during store repair operations.
-
#_deserialize(data) ⇒ Object
Restore the persistent data from a single data structure.
-
#_referenced_object_ids ⇒ Array of Fixnum or Bignum
Return a list of all object IDs of all persistend objects that this Hash is referencing.
-
#initialize(p, default = nil) ⇒ Hash
constructor
New PEROBS objects must always be created by calling # Store.new().
-
#inspect ⇒ String
Textual dump for debugging purposes.
Methods inherited from ObjectBase
#==, _finalize, #_initialize, #_restore, #_stash, #_sync, #_transfer, read, #restore
Constructor Details
#initialize(p, default = nil) ⇒ Hash
New PEROBS objects must always be created by calling # Store.new(). PEROBS users should never call this method or equivalents of derived methods directly.
84 85 86 87 88 89 90 91 |
# File 'lib/perobs/Hash.rb', line 84 def initialize(p, default = nil) super(p) @default = nil @data = {} # Ensure that the newly created object will be pushed into the database. @store.cache.cache_write(self) end |
Instance Method Details
#_delete_reference_to_id(id) ⇒ Object
This method should only be used during store repair operations. It will delete all referenced to the given object ID.
104 105 106 107 108 109 |
# File 'lib/perobs/Hash.rb', line 104 def _delete_reference_to_id(id) @data.delete_if do |k, v| v && v.respond_to?(:is_poxreference?) && v.id == id end @store.cache.cache_write(self) end |
#_deserialize(data) ⇒ Object
Restore the persistent data from a single data structure. This is a library internal method. Do not use outside of this library.
115 116 117 118 119 120 |
# File 'lib/perobs/Hash.rb', line 115 def _deserialize(data) @data = {} data.each { |k, v| @data[k] = v.is_a?(POReference) ? POXReference.new(@store, v.id) : v } @data end |
#_referenced_object_ids ⇒ Array of Fixnum or Bignum
Return a list of all object IDs of all persistend objects that this Hash is referencing.
96 97 98 99 |
# File 'lib/perobs/Hash.rb', line 96 def _referenced_object_ids @data.each_value.select { |v| v && v.respond_to?(:is_poxreference?) }. map { |o| o.id } end |
#inspect ⇒ String
Textual dump for debugging purposes
124 125 126 127 128 129 130 131 |
# File 'lib/perobs/Hash.rb', line 124 def inspect "<#{self.class}:#{@_id}>\n{\n" + @data.map do |k, v| " #{k.inspect} => " + (v.respond_to?(:is_poxreference?) ? "<PEROBS::ObjectBase:#{v._id}>" : v.inspect) end.join(",\n") + "\n}\n" end |