Class: Sumac::LocalReferences

Inherits:
Object
  • Object
show all
Defined in:
lib/sumac/local_references.rb

Instance Method Summary collapse

Constructor Details

#initialize(connection) ⇒ LocalReferences

Returns a new instance of LocalReferences.



4
5
6
7
8
9
10
11
# 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

Instance Method Details

#commit_transactionObject



46
47
48
# File 'lib/sumac/local_references.rb', line 46

def commit_transaction
  @transaction = nil
end

#destroyObject



17
18
19
# File 'lib/sumac/local_references.rb', line 17

def destroy
  @exposed_id_table.values.each { |reference| reference.destroy }
end

#detachObject



13
14
15
# File 'lib/sumac/local_references.rb', line 13

def detach
  @exposed_id_table.values.each { |reference| reference.detach }
end

#from_id(exposed_id) ⇒ Object



21
22
23
24
25
# 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) ⇒ Object



27
28
29
30
31
# 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) ⇒ Object



33
34
35
36
37
38
39
# 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_transactionObject



41
42
43
44
# File 'lib/sumac/local_references.rb', line 41

def rollback_transaction
  @transaction.each { |reference| reference.quiet_forget }
  @transaction = []
end

#start_transactionObject



50
51
52
# File 'lib/sumac/local_references.rb', line 50

def start_transaction
  @transaction = []
end