Class: Valkyrie::Persistence::Solr::Queries::FindByIdQuery

Inherits:
Object
  • Object
show all
Defined in:
lib/valkyrie/persistence/solr/queries/find_by_id_query.rb

Overview

Responsible for returning a single resource identified by an ID.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, connection:, resource_factory:) ⇒ FindByIdQuery

Returns a new instance of FindByIdQuery.

Parameters:



11
12
13
14
15
# File 'lib/valkyrie/persistence/solr/queries/find_by_id_query.rb', line 11

def initialize(id, connection:, resource_factory:)
  @id = id
  @connection = connection
  @resource_factory = resource_factory
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



5
6
7
# File 'lib/valkyrie/persistence/solr/queries/find_by_id_query.rb', line 5

def connection
  @connection
end

#idString

Retrieve the string value for the ID

Returns:

  • (String)


27
28
29
# File 'lib/valkyrie/persistence/solr/queries/find_by_id_query.rb', line 27

def id
  @id.to_s
end

#resource_factoryObject (readonly)

Returns the value of attribute resource_factory.



5
6
7
# File 'lib/valkyrie/persistence/solr/queries/find_by_id_query.rb', line 5

def resource_factory
  @resource_factory
end

Instance Method Details

#resourceHash

Query Solr for for the first document with the ID in a field

Returns:

  • (Hash)


33
34
35
# File 'lib/valkyrie/persistence/solr/queries/find_by_id_query.rb', line 33

def resource
  @resource ||= connection.get("select", params: { q: "id:\"#{id}\"", fl: "*", rows: 1 })["response"]["docs"].first
end

#runValkyrie::Resource

Constructs a Valkyrie Resource found using the ID



20
21
22
23
# File 'lib/valkyrie/persistence/solr/queries/find_by_id_query.rb', line 20

def run
  raise ::Valkyrie::Persistence::ObjectNotFoundError unless resource
  resource_factory.to_resource(object: resource)
end