Class: Sumac::LocalReferences
Instance Method Summary collapse
- #commit_transaction ⇒ Object
- #destroy ⇒ Object
- #detach ⇒ Object
- #from_id(exposed_id) ⇒ Object
- #from_object(exposed_object) ⇒ Object
-
#initialize(connection) ⇒ LocalReferences
constructor
A new instance of LocalReferences.
- #remove(reference) ⇒ Object
- #rollback_transaction ⇒ Object
- #start_transaction ⇒ Object
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_transaction ⇒ Object
46 47 48 |
# File 'lib/sumac/local_references.rb', line 46 def commit_transaction @transaction = nil end |
#destroy ⇒ Object
17 18 19 |
# File 'lib/sumac/local_references.rb', line 17 def destroy @exposed_id_table.values.each { |reference| reference.destroy } end |
#detach ⇒ Object
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_transaction ⇒ Object
41 42 43 44 |
# File 'lib/sumac/local_references.rb', line 41 def rollback_transaction @transaction.each { |reference| reference.quiet_forget } @transaction = [] end |
#start_transaction ⇒ Object
50 51 52 |
# File 'lib/sumac/local_references.rb', line 50 def start_transaction @transaction = [] end |