Class: PEROBS::Array
- Inherits:
-
ObjectBase
- Object
- ObjectBase
- PEROBS::Array
- Defined in:
- lib/perobs/Array.rb
Overview
An Array that is transparently persisted onto the back-end storage. It is very similar to the Ruby built-in Array class but like other PEROBS object classes it converts direct references to other PEROBS objects into POXReference objects that only indirectly reference the other object. It also tracks all reads and write to any Array element and updates the cache accordingly.
We don’t support an Array.initialize_copy proxy as this would conflict with BasicObject.initialize_copy. You can use PEROBS::Array.replace() instead.
Direct Known Subclasses
Constant Summary
Constants inherited from ObjectBase
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
Attributes inherited from ObjectBase
Instance Method Summary collapse
-
#[]=(*args) ⇒ Object
Proxy for the assignment method.
-
#_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 Integer
Return a list of all object IDs of all persistend objects that this Array is referencing.
-
#initialize(p, arg1 = 0, default = nil, &block) ⇒ Array
constructor
New PEROBS objects must always be created by calling # Store.new().
-
#inspect ⇒ String
Textual dump for debugging purposes.
-
#unshift(val) ⇒ Object
Proxy for the unshift method.
Methods inherited from ObjectBase
#==, #_check_assignment_value, _finalize, #_initialize, #_restore, #_stash, #_sync, #_transfer, read, #restore
Constructor Details
#initialize(p, arg1 = 0, default = nil, &block) ⇒ Array
New PEROBS objects must always be created by calling # Store.new(). PEROBS users should never call this method or equivalents of derived methods directly.
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/perobs/Array.rb', line 107 def initialize(p, arg1 = 0, default = nil, &block) super(p) if arg1.is_a?(::Array) arg1.each { |v| _check_assignment_value(v) } @data = arg1.dup elsif block_given? @data = ::Array.new(arg1) do _check_assignment_value(yield) end else @data = ::Array.new(arg1, _check_assignment_value(default)) end # Ensure that the newly created object will be pushed into the database. @store.cache.cache_write(self) end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
45 46 47 |
# File 'lib/perobs/Array.rb', line 45 def data @data end |
Instance Method Details
#[]=(*args) ⇒ Object
Proxy for the assignment method.
125 126 127 128 129 130 131 132 133 |
# File 'lib/perobs/Array.rb', line 125 def []=(*args) if (args.length == 2) _check_assignment_value(args[1]) else _check_assignment_value(args[2]) end @store.cache.cache_write(self) @data.[]=(*args) end |
#_delete_reference_to_id(id) ⇒ Object
This method should only be used during store repair operations. It will delete all references to the given object ID.
154 155 156 157 158 159 |
# File 'lib/perobs/Array.rb', line 154 def _delete_reference_to_id(id) @data.delete_if do |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.
165 166 167 168 |
# File 'lib/perobs/Array.rb', line 165 def _deserialize(data) @data = data.map { |v| v.is_a?(POReference) ? POXReference.new(@store, v.id) : v } end |
#_referenced_object_ids ⇒ Array of Integer
Return a list of all object IDs of all persistend objects that this Array is referencing.
145 146 147 148 149 |
# File 'lib/perobs/Array.rb', line 145 def _referenced_object_ids @data.each.select do |v| v && v.respond_to?(:is_poxreference?) end.map { |o| o.id } end |
#inspect ⇒ String
Textual dump for debugging purposes
172 173 174 175 176 177 178 179 |
# File 'lib/perobs/Array.rb', line 172 def inspect "<#{self.class}:#{@_id}>\n[\n" + @data.map do |v| " " + (v.respond_to?(:is_poxreference?) ? "<PEROBS::ObjectBase:#{v._id}>" : v.inspect) end.join(",\n") + "\n]\n" end |
#unshift(val) ⇒ Object
Proxy for the unshift method.
136 137 138 139 140 |
# File 'lib/perobs/Array.rb', line 136 def unshift(val) _check_assignment_value(val) @store.cache.cache_write(self) @data.unshift(val) end |