Sha256: f57964d8c3ff7bc36e88dfbb8a926aba4ee609f4f61fc8f19937461db8fada02

Contents?: true

Size: 1.69 KB

Versions: 3

Compression:

Stored size: 1.69 KB

Contents

# frozen_string_literal: true
module Valkyrie::Sequel
  class ResourceFactory::ORMConverter
    attr_reader :object, :resource_factory
    def initialize(object, resource_factory:)
      @object = object
      @resource_factory = resource_factory
    end

    def convert!
      @resource ||= resource
    end

    private

    # Construct a new Valkyrie Resource using the attributes retrieved from the database
    # @return [Valkyrie::Resource]
    def resource
      resource_klass.new(
        attributes.merge(
          new_record: false,
          Valkyrie::Persistence::Attributes::OPTIMISTIC_LOCK => lock_token
        )
      )
    end

    # Construct the optimistic lock token using the adapter and lock version for the Resource
    # @return [Valkyrie::Persistence::OptimisticLockToken]
    def lock_token
      return nil if object[:lock_version].blank?
      @lock_token ||=
        Valkyrie::Persistence::OptimisticLockToken.new(
          adapter_id: resource_factory.adapter_id,
          token: object[:lock_version]
        )
    end

    # Retrieve the Class used to construct the Valkyrie Resource
    # @return [Class]
    def resource_klass
      internal_resource.constantize
    end

    # Access the String for the Valkyrie Resource type within the attributes
    # @return [String]
    def internal_resource
      attributes[:internal_resource]
    end

    def attributes
      @attributes ||= object.except(:metadata).merge(rdf_metadata).symbolize_keys
    end

    # Generate a Hash derived from Valkyrie Resource metadata encoded in the RDF
    # @return [Hash]
    def rdf_metadata
      @rdf_metadata ||= Valkyrie::Persistence::Shared::JSONValueMapper.new(object[:metadata]).result
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
valkyrie-sequel-3.0.0.pre.beta.1 lib/valkyrie/sequel/resource_factory/orm_converter.rb
valkyrie-sequel-2.2.1 lib/valkyrie/sequel/resource_factory/orm_converter.rb
valkyrie-sequel-2.2.0 lib/valkyrie/sequel/resource_factory/orm_converter.rb