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

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:


17
18
19
# File 'lib/wings/valkyrie/persister.rb', line 17

def initialize(adapter:)
  @adapter = adapter
end

Instance Attribute Details

#adapterObject (readonly)

Returns the value of attribute adapter


11
12
13
# File 'lib/wings/valkyrie/persister.rb', line 11

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


53
54
55
56
57
58
# File 'lib/wings/valkyrie/persister.rb', line 53

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


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/wings/valkyrie/persister.rb', line 24

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 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


43
44
45
46
47
48
# File 'lib/wings/valkyrie/persister.rb', line 43

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


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

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