Class: Valkyrie::Persistence::Postgres::Persister

Inherits:
Object
  • Object
show all
Defined in:
lib/valkyrie/persistence/postgres/persister.rb

Overview

Persister for Postgres MetadataAdapter.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(adapter:) ⇒ Persister

Returns a new instance of Persister.

Parameters:



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

def initialize(adapter:)
  @adapter = adapter
end

Instance Attribute Details

#adapterObject (readonly)

Returns the value of attribute adapter.



7
8
9
# File 'lib/valkyrie/persistence/postgres/persister.rb', line 7

def adapter
  @adapter
end

Instance Method Details

#delete(resource:) ⇒ Valkyrie::Resource

Deletes a resource persisted within the database

Parameters:

Returns:



57
58
59
60
61
# File 'lib/valkyrie/persistence/postgres/persister.rb', line 57

def delete(resource:)
  orm_object = resource_factory.from_resource(resource: resource)
  orm_object.delete
  resource
end

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

Persists a resource within the database

Parameters:

Returns:

Raises:



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

def save(resource:, external_resource: false)
  orm_object = resource_factory.from_resource(resource: resource)
  orm_object.transaction do
    if !external_resource && resource.persisted? && !orm_object.persisted?
      raise Valkyrie::Persistence::ObjectNotFoundError, "The object #{resource.id} is previously persisted but not found at save time."
    end
    orm_object.save!
    if resource.id && resource.id.to_s != orm_object.id
      raise Valkyrie::Persistence::UnsupportedDatatype,
            "Postgres' primary key column can not save with the given ID #{resource.id}. " \
            "To avoid this error, set the ID to be nil via `resource.id = nil` before you save it. \n" \
            "Called from #{Gem.location_of_caller.join(':')}"
    end
  end
  resource_factory.to_resource(object: orm_object)
rescue ActiveRecord::StaleObjectError
  raise Valkyrie::Persistence::StaleObjectError, "The object #{resource.id} has been updated by another process."
end

#save_all(resources:) ⇒ Array<Valkyrie::Resource>

Persists a set of resources within the database

Parameters:

Returns:

Raises:



44
45
46
47
48
49
50
51
52
# File 'lib/valkyrie/persistence/postgres/persister.rb', line 44

def save_all(resources:)
  resource_factory.orm_class.transaction do
    resources.map do |resource|
      save(resource: resource)
    end
  end
rescue Valkyrie::Persistence::StaleObjectError
  raise Valkyrie::Persistence::StaleObjectError, "One or more resources have been updated by another process."
end

#wipe!Object

Deletes all resources of a specific Valkyrie Resource type persisted in

the database


65
66
67
# File 'lib/valkyrie/persistence/postgres/persister.rb', line 65

def wipe!
  resource_factory.orm_class.delete_all
end