class PEROBS::Array
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.
Attributes
Public Class Methods
New PEROBS
objects must always be created by calling # Store.new()
. PEROBS
users should never call this method or equivalents of derived methods directly. @param p [PEROBS::Handle] PEROBS
handle @param arg1 [Integer or Array] The requested size of the Array
or an
Array to initialize
@param default [Any] The default value that is returned when no value is
stored for a specific key.
# 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
Public Instance Methods
Proxy for the assignment method.
# 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
This method should only be used during store repair operations. It will delete all references to the given object ID. @param id [Integer] targeted object ID
# 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
Restore the persistent data from a single data structure. This is a library internal method. Do not use outside of this library. @param data [Array] the actual Array
object @private
# 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
Return a list of all object IDs of all persistend objects that this Array
is referencing. @return [Array of Integer] IDs of referenced objects
# 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
Textual dump for debugging purposes @return [String]
# 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
Proxy for the unshift method.
# File lib/perobs/Array.rb, line 136 def unshift(val) _check_assignment_value(val) @store.cache.cache_write(self) @data.unshift(val) end
Private Instance Methods
# File lib/perobs/Array.rb, line 183 def _serialize @data.map do |v| if v.respond_to?(:is_poxreference?) POReference.new(v.id) else # Outside of the PEROBS library all PEROBS::ObjectBase derived # objects should not be used directly. The library only exposes them # via POXReference proxy objects. if v.is_a?(ObjectBase) PEROBS.log.fatal 'A PEROBS::ObjectBase object escaped! ' + "It is stored in a PEROBS::Array at index #{@data.index(v)}. " + 'Have you used self() instead of myself() to ' + "get the reference of this PEROBS object?\n" + v.inspect end v end end end