Class: Valkyrie::Persistence::Memory::Persister
- Inherits:
-
Object
- Object
- Valkyrie::Persistence::Memory::Persister
- Defined in:
- lib/valkyrie/persistence/memory/persister.rb
Overview
Note:
Documentation for persisters in general is maintained here.
Persister for the memory metadata adapter.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#adapter ⇒ Object
readonly
Returns the value of attribute adapter.
Instance Method Summary collapse
-
#delete(resource:) ⇒ Object
Delete a resource.
-
#initialize(adapter) ⇒ Persister
constructor
A new instance of Persister.
-
#save(resource:, external_resource: false) ⇒ Valkyrie::Resource
Save a single resource.
-
#save_all(resources:) ⇒ Array<Valkyrie::Resource>
Save a batch of resources.
-
#valid_for_save?(resource) ⇒ Boolean
return true if resource is persisted and found or not persisted.
-
#wipe! ⇒ Object
Removes all data from the persistence backend.
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.
13 14 15 |
# File 'lib/valkyrie/persistence/memory/persister.rb', line 13 def initialize(adapter) @adapter = adapter end |
Instance Attribute Details
#adapter ⇒ Object (readonly)
Returns the value of attribute adapter.
7 8 9 |
# File 'lib/valkyrie/persistence/memory/persister.rb', line 7 def adapter @adapter end |
Instance Method Details
#delete(resource:) ⇒ Object
Delete a resource.
70 71 72 |
# File 'lib/valkyrie/persistence/memory/persister.rb', line 70 def delete(resource:) cache.delete(resource.id) end |
#save(resource:, external_resource: false) ⇒ Valkyrie::Resource
Save a single resource.
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/valkyrie/persistence/memory/persister.rb', line 28 def save(resource:, external_resource: false) raise Valkyrie::Persistence::StaleObjectError, "The object #{resource.id} has been updated by another process." unless valid_lock?(resource) raise Valkyrie::Persistence::ObjectNotFoundError, "The object #{resource.id} is previously persisted but not found at save time." unless external_resource || valid_for_save?(resource) # duplicate the resource so we are not creating side effects on the caller's resource internal_resource = resource.dup internal_resource = generate_id(internal_resource) if internal_resource.id.blank? internal_resource.created_at ||= Time.current internal_resource.updated_at = Time.current internal_resource.new_record = false generate_lock_token(internal_resource) normalize_dates!(internal_resource) cache[internal_resource.id] = internal_resource end |
#save_all(resources:) ⇒ Array<Valkyrie::Resource>
Save a batch of resources.
58 59 60 61 62 63 64 65 |
# File 'lib/valkyrie/persistence/memory/persister.rb', line 58 def save_all(resources:) resources.map do |resource| save(resource: resource) end rescue Valkyrie::Persistence::StaleObjectError # Re-raising with no error message to prevent confusion raise Valkyrie::Persistence::StaleObjectError, "One or more resources have been updated by another process." end |
#valid_for_save?(resource) ⇒ Boolean
return true if resource is persisted and found or not persisted
48 49 50 51 |
# File 'lib/valkyrie/persistence/memory/persister.rb', line 48 def valid_for_save?(resource) return true unless resource.persisted? # a new resource query_service.find_by(id: resource.id).present? # a persisted resource must be found end |
#wipe! ⇒ Object
Removes all data from the persistence backend.
75 76 77 |
# File 'lib/valkyrie/persistence/memory/persister.rb', line 75 def wipe! cache.clear end |