Class: Valkyrie::Persistence::CompositePersister
- Inherits:
-
Object
- Object
- Valkyrie::Persistence::CompositePersister
- Defined in:
- lib/valkyrie/persistence/composite_persister.rb
Overview
Wraps up multiple persisters under a common interface, to transparently persist to multiple places at once.
Instance Attribute Summary collapse
-
#persisters ⇒ Object
readonly
Returns the value of attribute persisters.
Instance Method Summary collapse
-
#delete(resource:) ⇒ Object
Delete a resource.
-
#initialize(*persisters) ⇒ CompositePersister
constructor
A new instance of CompositePersister.
-
#save(resource:, external_resource: false) ⇒ Valkyrie::Resource
Save a single resource.
-
#save_all(resources:) ⇒ Array<Valkyrie::Resource>
Save a batch of resources.
- #wipe! ⇒ Object
Constructor Details
#initialize(*persisters) ⇒ CompositePersister
Returns a new instance of CompositePersister.
22 23 24 |
# File 'lib/valkyrie/persistence/composite_persister.rb', line 22 def initialize(*persisters) @persisters = persisters end |
Instance Attribute Details
#persisters ⇒ Object (readonly)
Returns the value of attribute persisters.
21 22 23 |
# File 'lib/valkyrie/persistence/composite_persister.rb', line 21 def persisters @persisters end |
Instance Method Details
#delete(resource:) ⇒ Object
Delete a resource.
50 51 52 |
# File 'lib/valkyrie/persistence/composite_persister.rb', line 50 def delete(resource:) persisters.inject(resource) { |m, persister| persister.delete(resource: m) } end |
#save(resource:, external_resource: false) ⇒ Valkyrie::Resource
Save a single resource.
27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/valkyrie/persistence/composite_persister.rb', line 27 def save(resource:, external_resource: false) # Assume the first persister is the canonical data store; that's the optlock we want first, *rest = *persisters cached_resource = first.save(resource: resource, external_resource: external_resource) # Don't pass opt lock tokens to other persisters internal_resource = cached_resource.dup internal_resource.clear_optimistic_lock_token! rest.inject(internal_resource) { |m, persister| persister.save(resource: m, external_resource: true) } # return the one with the desired opt lock token cached_resource end |
#save_all(resources:) ⇒ Array<Valkyrie::Resource>
Save a batch of resources.
40 41 42 43 44 45 46 47 |
# File 'lib/valkyrie/persistence/composite_persister.rb', line 40 def save_all(resources:) resources.map do |resource| save(resource: resource) end rescue Valkyrie::Persistence::StaleObjectError # clear out any IDs returned to reduce potential confusion raise Valkyrie::Persistence::StaleObjectError, "One or more resources have been updated by another process." end |
#wipe! ⇒ Object
54 55 56 |
# File 'lib/valkyrie/persistence/composite_persister.rb', line 54 def wipe! persisters.each_entry(&:wipe!) end |