class Sumac::LocalReferences
Public Class Methods
new(connection)
click to toggle source
# File lib/sumac/local_references.rb, line 4 def initialize(connection) raise "argument 'connection' must be a Connection" unless connection.is_a?(Connection) @connection = connection @id_allocator = IDAllocator.new @exposed_id_table = {} @native_id_table = {} @transaction = [] end
Public Instance Methods
commit_transaction()
click to toggle source
# File lib/sumac/local_references.rb, line 46 def commit_transaction @transaction = nil end
destroy()
click to toggle source
# File lib/sumac/local_references.rb, line 17 def destroy @exposed_id_table.values.each { |reference| reference.destroy } end
detach()
click to toggle source
# File lib/sumac/local_references.rb, line 13 def detach @exposed_id_table.values.each { |reference| reference.detach } end
from_id(exposed_id)
click to toggle source
# File lib/sumac/local_references.rb, line 21 def from_id(exposed_id) raise unless @id_allocator.valid?(exposed_id) reference = @exposed_id_table[exposed_id] reference end
from_object(exposed_object)
click to toggle source
# File lib/sumac/local_references.rb, line 27 def from_object(exposed_object) raise unless exposed_object.is_a?(ExposedObject) reference = find(exposed_object) || create(exposed_object) reference end
remove(reference)
click to toggle source
# File lib/sumac/local_references.rb, line 33 def remove(reference) @exposed_id_table.delete(reference.exposed_id) references = @native_id_table[reference.exposed_object.__native_id__] references.delete(reference) @native_id_table.delete(reference.exposed_object.__native_id__) if references.empty? @id_allocator.free(reference.exposed_id) end
rollback_transaction()
click to toggle source
# File lib/sumac/local_references.rb, line 41 def rollback_transaction @transaction.each { |reference| reference.quiet_forget } @transaction = [] end
start_transaction()
click to toggle source
# File lib/sumac/local_references.rb, line 50 def start_transaction @transaction = [] end
Private Instance Methods
create(exposed_object)
click to toggle source
# File lib/sumac/local_references.rb, line 56 def create(exposed_object) new_exposed_id = @id_allocator.allocate new_reference = LocalReference.new(@connection, new_exposed_id, exposed_object) @exposed_id_table[new_exposed_id] = new_reference references = @native_id_table[exposed_object.__native_id__] if references references << new_reference else @native_id_table[exposed_object.__native_id__] = [new_reference] end @transaction << new_reference if @transaction new_reference end
find(exposed_object)
click to toggle source
# File lib/sumac/local_references.rb, line 70 def find(exposed_object) references = @native_id_table[exposed_object.__native_id__] return nil unless references callable_references = references.select { |reference| reference.callable? } raise if callable_references.length > 1 reference = callable_references.first reference end