Class: Wings::Valkyrie::Persister
- Inherits:
-
Object
- Object
- Wings::Valkyrie::Persister
- 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
-
#adapter ⇒ Object
readonly
Returns the value of attribute adapter.
Instance Method Summary collapse
-
#delete(resource:) ⇒ Valkyrie::Resource
Deletes a resource persisted using ActiveFedora.
-
#initialize(adapter:) ⇒ Persister
constructor
A new instance of Persister.
-
#save(resource:) ⇒ Valkyrie::Resource
Persists a resource using ActiveFedora.
-
#save_all(resources:) ⇒ Valkyrie::Resource
Persists a resource using ActiveFedora.
-
#wipe! ⇒ Object
Deletes all resources from Fedora and Solr.
Constructor Details
#initialize(adapter:) ⇒ Persister
Many persister methods are part of Valkyrie’s public API, but instantiation itself is not
Returns a new instance of Persister.
19 20 21 |
# File 'lib/wings/valkyrie/persister.rb', line 19 def initialize(adapter:) @adapter = adapter end |
Instance Attribute Details
#adapter ⇒ Object (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
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
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., obj: af_object) if err. == 'Save the file first' raise FailedSaveError.new(err., obj: af_object) end |
#save_all(resources:) ⇒ Valkyrie::Resource
Persists a resource using ActiveFedora
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 |