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:



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

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

#save(resource:) ⇒ 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
# File 'lib/valkyrie/persistence/postgres/persister.rb', line 20

def save(resource:)
  orm_object = resource_factory.from_resource(resource: resource)
  orm_object.transaction do
    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:



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

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


62
63
64
# File 'lib/valkyrie/persistence/postgres/persister.rb', line 62

def wipe!
  resource_factory.orm_class.delete_all
end