Sha256: 1c0a7f855fd4e0db93281f7def16fb559e0d0d4d9c30d69edc318daaaa6d0120

Contents?: true

Size: 1.83 KB

Versions: 1

Compression:

Stored size: 1.83 KB

Contents

# frozen_string_literal: true
module Valkyrie::Persistence
  # Wraps up multiple persisters under a common interface, to transparently
  # persist to multiple places at once.
  #
  # @example
  #   persister = Valkyrie.config.metadata_adapter
  #   index_persister = Valkyrie::MetadataAdapter.find(:index_solr)
  #   Valkyrie::MetadataAdapter.register(
  #     Valkyrie::Persistence::CompositePersister.new(persister, index_persister),
  #     :my_composite_persister
  #   )
  #
  class CompositePersister
    attr_reader :persisters
    def initialize(*persisters)
      @persisters = persisters
    end

    # (see Valkyrie::Persistence::Memory::Persister#save)
    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

    # (see Valkyrie::Persistence::Memory::Persister#save_all)
    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
    end

    # (see Valkyrie::Persistence::Memory::Persister#delete)
    def delete(resource:)
      persisters.inject(resource) { |m, persister| persister.delete(resource: m) }
    end

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
valkyrie-1.2.0.rc1 lib/valkyrie/persistence/composite_persister.rb