Module: Hold::IdentitySetRepository
- Includes:
- SetRepository
- Included in:
- Hold::InMemory::IdentitySetRepository, Sequel::IdentitySetRepository, Sequel::PolymorphicRepository, Serialized::IdentitySetRepository
- Defined in:
- lib/hold/interfaces/identity_set_repository.rb
Overview
A special kind of SetRepository which stores Objects whose identities are determined by an identity property, and supports indexed lookup by their id.
May allocate the IDs itself, or not.
Exposes a somewhat more familiar CRUD-style persistence interface as a result.
Comes with default implementations for most of the extra interface
Defined Under Namespace
Classes: IdCell
Instance Method Summary collapse
-
#allocates_ids? ⇒ Boolean
Either the repository allocates IDs, and you don’t (in which case any entity with an ID may be assumed to be already persisted in the repo), or the repository doesn’t allocate IDs (in which case you must always supply one when persisting a new object).
- #cell(object) ⇒ Object
- #contains_id?(id) ⇒ Boolean
-
#delete_id(id) ⇒ Object
deletes the object with the given identity where it exists in the repo.
-
#get_by_id(id) ⇒ Object
Looks up a persisted object by the value of its identity property.
- #get_many_by_ids(ids) ⇒ Object
- #id_cell(id) ⇒ Object
-
#load(object) ⇒ Object
Like reload, but updates the given instance in-place with the updated data.
-
#reload(object) ⇒ Object
Loads a fresh instance of the given object by its id Returns nil where the object is no longer present in the repository.
-
#update(entity, update_entity) ⇒ Object
Applies an in-place update to the object, where it exists in the repository.
-
#update_by_id(id, update_entity) ⇒ Object
Applies an in-place update to the object with the given identity, where it exists in the repository.
Methods included from SetRepository
#can_get_class?, #can_set_class?, #contains?, #delete, #get_all, #store, #store_new
Instance Method Details
#allocates_ids? ⇒ Boolean
Either the repository allocates IDs, and you don’t (in which case any entity with an ID may be assumed to be already persisted in the repo), or the repository doesn’t allocate IDs (in which case you must always supply one when persisting a new object).
If you allocates_ids?, you should deal with an object without an identity as an argument to store and store_new, and you should set the id property on it before returning it.
If you don’t, you may raise MissingIdentity if passed an object without one.
26 27 28 |
# File 'lib/hold/interfaces/identity_set_repository.rb', line 26 def allocates_ids? false end |
#cell(object) ⇒ Object
88 89 90 91 |
# File 'lib/hold/interfaces/identity_set_repository.rb', line 88 def cell(object) id = object.id or raise MissingIdentity id_cell(id) end |
#contains_id?(id) ⇒ Boolean
75 76 77 |
# File 'lib/hold/interfaces/identity_set_repository.rb', line 75 def contains_id?(id) !!get_by_id(id) end |
#delete_id(id) ⇒ Object
deletes the object with the given identity where it exists in the repo
36 37 38 |
# File 'lib/hold/interfaces/identity_set_repository.rb', line 36 def delete_id(id) delete(get_by_id(id)) end |
#get_by_id(id) ⇒ Object
Looks up a persisted object by the value of its identity property
31 32 33 |
# File 'lib/hold/interfaces/identity_set_repository.rb', line 31 def get_by_id(id) raise UnsupportedOperation end |
#get_many_by_ids(ids) ⇒ Object
80 81 82 |
# File 'lib/hold/interfaces/identity_set_repository.rb', line 80 def get_many_by_ids(ids) ids.map {|id| get_by_id(id)} end |
#id_cell(id) ⇒ Object
84 85 86 |
# File 'lib/hold/interfaces/identity_set_repository.rb', line 84 def id_cell(id) IdCell.new(self, id) end |
#load(object) ⇒ Object
Like reload, but updates the given instance in-place with the updated data. Returns nil where the object is no longer present in the repository
50 51 52 53 54 55 |
# File 'lib/hold/interfaces/identity_set_repository.rb', line 50 def load(object) raise UnsupportedOperation unless object.respond_to?(:merge!) updated = reload(object) or return object.merge!(updated) object end |
#reload(object) ⇒ Object
Loads a fresh instance of the given object by its id Returns nil where the object is no longer present in the repository
42 43 44 45 |
# File 'lib/hold/interfaces/identity_set_repository.rb', line 42 def reload(object) id = object.id or raise MissingIdentity get_by_id(id) end |
#update(entity, update_entity) ⇒ Object
Applies an in-place update to the object, where it exists in the repository
59 60 61 62 63 64 |
# File 'lib/hold/interfaces/identity_set_repository.rb', line 59 def update(entity, update_entity) raise UnsupportedOperation unless entity.respond_to?(:merge!) load(entity) or return entity.merge!(update_entity) store(entity) end |
#update_by_id(id, update_entity) ⇒ Object
Applies an in-place update to the object with the given identity, where it exists in the repository
68 69 70 71 72 73 |
# File 'lib/hold/interfaces/identity_set_repository.rb', line 68 def update_by_id(id, update_entity) entity = get_by_id(id) or return raise UnsupportedOperation unless entity.respond_to?(:merge!) entity.merge!(update_entity) store(entity) end |