Class: Valkyrie::Persistence::Postgres::ResourceConverter

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

Overview

Responsible for converting a Resource into a ORM::Resource

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource, resource_factory:) ⇒ ResourceConverter

Returns a new instance of ResourceConverter.

Parameters:



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

def initialize(resource, resource_factory:)
  @resource = resource
  @resource_factory = resource_factory
end

Instance Attribute Details

#resourceObject (readonly)

Returns the value of attribute resource.



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

def resource
  @resource
end

#resource_factoryObject (readonly)

Returns the value of attribute resource_factory.



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

def resource_factory
  @resource_factory
end

Instance Method Details

#attributesHash

Convert attributes to all be arrays to better enable querying and “changing of minds” later on.

Returns:

  • (Hash)


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

def attributes
  Hash[
    resource.attributes.except(:id, :internal_resource, :created_at, :updated_at).compact.map do |k, v|
      [k, Array.wrap(v)]
    end
  ]
end

#convert!ORM::Resource

Converts the Valkyrie Resource into an ActiveRecord object

Returns:



18
19
20
21
22
23
24
25
# File 'lib/valkyrie/persistence/postgres/resource_converter.rb', line 18

def convert!
  orm_class.find_or_initialize_by(id: resource.id && resource.id.to_s).tap do |orm_object|
    orm_object.internal_resource = resource.internal_resource
    process_lock_token(orm_object)
    orm_object.disable_optimistic_locking! unless resource.optimistic_locking_enabled?
    orm_object. = attributes
  end
end

#process_lock_token(orm_object) ⇒ Object

Retrieves the optimistic lock token from the Valkyrie attribute value and

sets it to the lock_version on ORM resource


31
32
33
34
35
36
37
38
# File 'lib/valkyrie/persistence/postgres/resource_converter.rb', line 31

def process_lock_token(orm_object)
  return unless resource.respond_to?(Valkyrie::Persistence::Attributes::OPTIMISTIC_LOCK)
  postgres_token = resource[Valkyrie::Persistence::Attributes::OPTIMISTIC_LOCK].find do |token|
    token.adapter_id == resource_factory.adapter_id
  end
  return unless postgres_token
  orm_object.lock_version = postgres_token.token
end