Class: Valkyrie::Persistence::CompositePersister

Inherits:
Object
  • Object
show all
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.

Examples:

persister = Valkyrie.config.
index_persister = Valkyrie::MetadataAdapter.find(:index_solr)
Valkyrie::MetadataAdapter.register(
  Valkyrie::Persistence::CompositePersister.new(persister, index_persister),
  :my_composite_persister
)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*persisters) ⇒ CompositePersister

Returns a new instance of CompositePersister.



16
17
18
# File 'lib/valkyrie/persistence/composite_persister.rb', line 16

def initialize(*persisters)
  @persisters = persisters
end

Instance Attribute Details

#persistersObject (readonly)

Returns the value of attribute persisters.



15
16
17
# File 'lib/valkyrie/persistence/composite_persister.rb', line 15

def persisters
  @persisters
end

Instance Method Details

#delete(resource:) ⇒ Object

Delete a resource.

Parameters:



44
45
46
# File 'lib/valkyrie/persistence/composite_persister.rb', line 44

def delete(resource:)
  persisters.inject(resource) { |m, persister| persister.delete(resource: m) }
end

#save(resource:) ⇒ Valkyrie::Resource

Save a single resource.

Parameters:

Returns:

  • (Valkyrie::Resource)

    The resource with an ‘#id` value generated by the persistence backend.

Raises:



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/valkyrie/persistence/composite_persister.rb', line 21

def save(resource:)
  # Assume the first persister is the canonical data store; that's the optlock we want
  first, *rest = *persisters
  cached_resource = first.save(resource: resource)
  # Don't pass opt lock tokens to other persisters
  internal_resource = cached_resource.dup
  internal_resource.send("#{Valkyrie::Persistence::Attributes::OPTIMISTIC_LOCK}=", []) if internal_resource.optimistic_locking_enabled?
  rest.inject(internal_resource) { |m, persister| persister.save(resource: m) }
  # return the one with the desired opt lock token
  cached_resource
end

#save_all(resources:) ⇒ Array<Valkyrie::Resource>

Save a batch of resources.

Parameters:

Returns:

  • (Array<Valkyrie::Resource>)

    List of resources with an ‘#id` value generated by the persistence backend.

Raises:



34
35
36
37
38
39
40
41
# File 'lib/valkyrie/persistence/composite_persister.rb', line 34

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



48
49
50
# File 'lib/valkyrie/persistence/composite_persister.rb', line 48

def wipe!
  persisters.each_entry(&:wipe!)
end