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



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

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

#save(resource:, perform_af_validation: false) ⇒ Valkyrie::Resource

Persists a resource using ActiveFedora

Parameters:

  • resource (Valkyrie::Resource)
  • perform_af_validation (Boolean) (defaults to: false)

Returns:

  • (Valkyrie::Resource)

    the persisted/updated resource



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

def save(resource:, perform_af_validation: false)
  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
  # we disable validation on the Active Fedora object, only if validations have already been run and passed
  af_object.save!(validate: perform_af_validation) ||
    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



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

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



68
69
70
71
72
# File 'lib/wings/valkyrie/persister.rb', line 68

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