Module: Lims::Core::Persistence::IdentityMap
- Included in:
- Class, IdentityMapClass
- Defined in:
- lib/lims-core/persistence/identity_map.rb
Overview
Mixing giving an identity map behavior ie a map (both way) between id and object
Defined Under Namespace
Classes: Class, DuplicateError, DuplicateIdError, DuplicateObjectError
Instance Method Summary collapse
-
#id_for(object, &block) ⇒ Object
Look for the id associated to an object and yield it to the block if found.
- #initialize(*args, &block) ⇒ Object
-
#map_id_object(id, object) ⇒ Object
bound an id to an object.
-
#object_for(id, &block) ⇒ Object
Look for the object associated to an object and yield it to the block if found.
Instance Method Details
#id_for(object, &block) ⇒ Object
Look for the id associated to an object and yield it to the block if found.
24 25 26 |
# File 'lib/lims-core/persistence/identity_map.rb', line 24 def id_for(object, &block) @object_to_id[object].andtap(&block) end |
#initialize(*args, &block) ⇒ Object
43 44 45 46 47 |
# File 'lib/lims-core/persistence/identity_map.rb', line 43 def initialize(*args, &block) super(*args, &block) @id_to_object = {} @object_to_id = {} end |
#map_id_object(id, object) ⇒ Object
bound an id to an object
35 36 37 38 39 40 41 |
# File 'lib/lims-core/persistence/identity_map.rb', line 35 def map_id_object(id, object) return nil unless id && object raise DuplicateIdError, id unless @id_to_object.fetch(id, object).equal? object raise DuplicateObjectError, object unless @object_to_id.fetch(object, id).equal? id @id_to_object[id] = object @object_to_id[object] = id end |
#object_for(id, &block) ⇒ Object
Look for the object associated to an object and yield it to the block if found.
30 31 32 |
# File 'lib/lims-core/persistence/identity_map.rb', line 30 def object_for(id, &block) @id_to_object[id].andtap(&block) end |