Class: Valkyrie::Persistence::Memory::Persister

Inherits:
Object
  • Object
show all
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

DeleteTrackingBuffer::Persister

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:


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

def initialize(adapter)
  @adapter = adapter
end

Instance Attribute Details

#adapterObject (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.

Parameters:


54
55
56
# File 'lib/valkyrie/persistence/memory/persister.rb', line 54

def delete(resource:)
  cache.delete(resource.id)
end

#save(resource:) ⇒ Valkyrie::Resource

Save a single resource.

Parameters:

Returns:

  • (Valkyrie::Resource)

    The resource with an `#id` value generated by the persistence backend.

Raises:


22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/valkyrie/persistence/memory/persister.rb', line 22

def save(resource:)
  raise Valkyrie::Persistence::StaleObjectError, "The object #{resource.id} has been updated by another process." unless valid_lock?(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.

Parameters:

Returns:

  • (Array<Valkyrie::Resource>)

    List of resources with an `#id` value generated by the persistence backend.

Raises:


42
43
44
45
46
47
48
49
# File 'lib/valkyrie/persistence/memory/persister.rb', line 42

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

#wipe!Object

Removes all data from the persistence backend.


59
60
61
# File 'lib/valkyrie/persistence/memory/persister.rb', line 59

def wipe!
  cache.clear
end