Class: Wings::Valkyrie::Persister

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/wings/valkyrie/persister.rb

Overview

A valkyrie persister that aims for data consistency/backwards compatibility with ActiveFedora.

The guiding principle of design for this persister is that resources persisted with it should be directly readable by ‘Hydra::Works`-style ActiveFedora models. It aims to be as complete as possible as a Valkyrie Persister, given that limitation.

Defined Under Namespace

Classes: FailedSaveError, MissingOrUnsavedFileError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(adapter:) ⇒ Persister

Note:

Many persister methods are part of Valkyrie’s public API, but instantiation itself is not

Returns a new instance of Persister.

Parameters:



19
20
21
# File 'lib/wings/valkyrie/persister.rb', line 19

def initialize(adapter:)
  @adapter = adapter
end

Instance Attribute Details

#adapterObject (readonly)

Returns the value of attribute adapter.



13
14
15
# File 'lib/wings/valkyrie/persister.rb', line 13

def adapter
  @adapter
end

Instance Method Details

#delete(resource:) ⇒ Valkyrie::Resource

Deletes a resource persisted using ActiveFedora

Parameters:

  • resource (Valkyrie::Resource)

Returns:

  • (Valkyrie::Resource)

    the deleted resource



58
59
60
61
62
63
# File 'lib/wings/valkyrie/persister.rb', line 58

def delete(resource:)
  af_object = ActiveFedora::Base.new
  af_object.id = resource.id
  af_object.delete
  resource
end

#save(resource:) ⇒ Valkyrie::Resource

Persists a resource using ActiveFedora

Parameters:

  • resource (Valkyrie::Resource)

Returns:

  • (Valkyrie::Resource)

    the persisted/updated resource



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/wings/valkyrie/persister.rb', line 26

def save(resource:)
  af_object = resource_factory.from_resource(resource: resource)

  check_lock_tokens(af_object: af_object, resource: resource)

  # the #save! api differs between ActiveFedora::Base and ActiveFedora::File objects,
  # if we get a falsey response, we expect we have a File that has failed to save due
  # to empty content
  af_object.save! ||
    raise(FailedSaveError.new("#{af_object.class}#save! returned non-true. It might be missing required content.", obj: af_object))

  resource_factory.to_resource(object: af_object)
rescue ActiveFedora::RecordInvalid, RuntimeError => err
  raise MissingOrUnsavedFileError.new(err.message, obj: af_object) if
    err.message == 'Save the file first'

  raise FailedSaveError.new(err.message, obj: af_object)
end

#save_all(resources:) ⇒ Valkyrie::Resource

Persists a resource using ActiveFedora

Parameters:

  • resource (Valkyrie::Resource)

Returns:

  • (Valkyrie::Resource)

    the persisted/updated resource



48
49
50
51
52
53
# File 'lib/wings/valkyrie/persister.rb', line 48

def save_all(resources:)
  resources.map { |resource| save(resource: resource) }
rescue ::Valkyrie::Persistence::StaleObjectError => _err
  raise(::Valkyrie::Persistence::StaleObjectError,
        "One or more resources have been updated by another process.")
end

#wipe!Object

Deletes all resources from Fedora and Solr



66
67
68
69
70
# File 'lib/wings/valkyrie/persister.rb', line 66

def wipe!
  Hyrax::SolrService.delete_by_query("*:*")
  Hyrax::SolrService.commit
  ActiveFedora::Cleaner.clean!
end