Sha256: e6a2999826fb689274228940a7d639e0085e30f7780b534b6c085386ee165a0c

Contents?: true

Size: 1.12 KB

Versions: 18

Compression:

Stored size: 1.12 KB

Contents

# frozen_string_literal: true
module Valkyrie::Persistence::Solr::Queries
  # Responsible for returning a single resource identified by an ID.
  class FindByIdQuery
    attr_reader :connection, :resource_factory
    attr_writer :id

    # @param [Valkyrie::ID] id
    # @param [RSolr::Client] connection
    # @param [ResourceFactory] resource_factory
    def initialize(id, connection:, resource_factory:)
      @id = id
      @connection = connection
      @resource_factory = resource_factory
    end

    # Constructs a Valkyrie Resource found using the ID
    # @raise [Valkyrie::Persistence::ObjectNotFoundError]
    # @return [Valkyrie::Resource]
    def run
      raise ::Valkyrie::Persistence::ObjectNotFoundError unless resource
      resource_factory.to_resource(object: resource)
    end

    # Retrieve the string value for the ID
    # @return [String]
    def id
      @id.to_s
    end

    # Query Solr for for the first document with the ID in a field
    # @return [Hash]
    def resource
      @resource ||= connection.get("select", params: { q: "id:\"#{id}\"", fl: "*", rows: 1 })["response"]["docs"].first
    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
valkyrie-3.5.0 lib/valkyrie/persistence/solr/queries/find_by_id_query.rb
valkyrie-3.4.0 lib/valkyrie/persistence/solr/queries/find_by_id_query.rb
valkyrie-3.3.0 lib/valkyrie/persistence/solr/queries/find_by_id_query.rb
valkyrie-3.1.5 lib/valkyrie/persistence/solr/queries/find_by_id_query.rb
valkyrie-3.1.4 lib/valkyrie/persistence/solr/queries/find_by_id_query.rb
valkyrie-3.1.3 lib/valkyrie/persistence/solr/queries/find_by_id_query.rb
valkyrie-3.1.2 lib/valkyrie/persistence/solr/queries/find_by_id_query.rb
valkyrie-3.1.1 lib/valkyrie/persistence/solr/queries/find_by_id_query.rb
valkyrie-3.1.0 lib/valkyrie/persistence/solr/queries/find_by_id_query.rb
valkyrie-3.0.3 lib/valkyrie/persistence/solr/queries/find_by_id_query.rb
valkyrie-3.0.2 lib/valkyrie/persistence/solr/queries/find_by_id_query.rb
valkyrie-3.0.1 lib/valkyrie/persistence/solr/queries/find_by_id_query.rb
valkyrie-3.0.0 lib/valkyrie/persistence/solr/queries/find_by_id_query.rb
valkyrie-3.0.0.pre.rc.1 lib/valkyrie/persistence/solr/queries/find_by_id_query.rb
valkyrie-3.0.0.pre.beta.3 lib/valkyrie/persistence/solr/queries/find_by_id_query.rb
valkyrie-3.0.0.pre.beta.2 lib/valkyrie/persistence/solr/queries/find_by_id_query.rb
valkyrie-3.0.0.pre.beta.1 lib/valkyrie/persistence/solr/queries/find_by_id_query.rb
valkyrie-2.2.0 lib/valkyrie/persistence/solr/queries/find_by_id_query.rb