Class: Valkyrie::Persistence::Solr::Queries::FindByAlternateIdentifierQuery

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

Overview

Responsible for returning a single resource identified by an ID.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(alternate_identifier, connection:, resource_factory:) ⇒ FindByAlternateIdentifierQuery

Returns a new instance of FindByAlternateIdentifierQuery.

Parameters:



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

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

Instance Attribute Details

#alternate_identifierString

Retrieve the string value for the alternate ID

Returns:

  • (String)


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

def alternate_identifier
  @alternate_identifier.to_s
end

#connectionObject (readonly)

Returns the value of attribute connection.



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

def connection
  @connection
end

#resource_factoryObject (readonly)

Returns the value of attribute resource_factory.



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

def resource_factory
  @resource_factory
end

Instance Method Details

#resourceHash

Note:

the field used here is alternate_ids_ssim and the value is prefixed by “id-”

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

Returns:

  • (Hash)


34
35
36
# File 'lib/valkyrie/persistence/solr/queries/find_by_alternate_identifier_query.rb', line 34

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

#runValkyrie::Resource

Constructs a Valkyrie Resource found using the alternate ID



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

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