Class: Hyrax::CustomQueries::FindFileMetadata

Inherits:
Object
  • Object
show all
Defined in:
app/services/hyrax/custom_queries/find_file_metadata.rb

Overview

Provide custom queries for finding Hyrax::FileMetadata

Examples:

Hyrax.custom_queries.(id: valkyrie_id)
Hyrax.custom_queries.(alternate_identifier: alt_id)
Hyrax.custom_queries.(ids: [valkyrie_id, valkyrie_id])

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(query_service:) ⇒ FindFileMetadata

Returns a new instance of FindFileMetadata.



19
20
21
# File 'app/services/hyrax/custom_queries/find_file_metadata.rb', line 19

def initialize(query_service:)
  @query_service = query_service
end

Instance Attribute Details

#query_serviceObject (readonly)

Returns the value of attribute query_service.



23
24
25
# File 'app/services/hyrax/custom_queries/find_file_metadata.rb', line 23

def query_service
  @query_service
end

Class Method Details

.queriesObject



12
13
14
15
16
17
# File 'app/services/hyrax/custom_queries/find_file_metadata.rb', line 12

def self.queries
  [:find_file_metadata_by,
   :find_file_metadata_by_alternate_identifier,
   :find_many_file_metadata_by_ids,
   :find_many_file_metadata_by_use]
end

Instance Method Details

#find_file_metadata_by(id:) ⇒ Hyrax::FileMetadata

Find a file metadata using a Valkyrie ID, and map it to a Hyrax::FileMetadata

Parameters:

  • id (Valkyrie::ID, String)

Returns:

Raises:



30
31
32
33
34
35
36
37
# File 'app/services/hyrax/custom_queries/find_file_metadata.rb', line 30

def (id:)
  result = query_service.find_by(id: id)
  unless result.is_a? Hyrax::FileMetadata
    raise ::Valkyrie::Persistence::ObjectNotFoundError,
          "Result type #{result.internal_resource} for id #{id} is not a `Hyrax::FileMetadata`"
  end
  result
end

#find_file_metadata_by_alternate_identifier(alternate_identifier:) ⇒ Hyrax::FileMetadata

Find a file metadata using an alternate ID, and map it to a Hyrax::FileMetadata

Parameters:

  • alternate_identifier (Valkyrie::ID, String)

Returns:

Raises:



43
44
45
46
47
48
49
50
# File 'app/services/hyrax/custom_queries/find_file_metadata.rb', line 43

def (alternate_identifier:)
  result = query_service.find_by_alternate_identifier(alternate_identifier: alternate_identifier)
  unless result.is_a? Hyrax::FileMetadata
    raise ::Valkyrie::Persistence::ObjectNotFoundError,
          "Result type #{result.internal_resource} for alternate_identifier #{alternate_identifier} is not a `Hyrax::FileMetadata`"
  end
  result
end

#find_many_file_metadata_by_ids(ids:) ⇒ Array<Hyrax::FileMetadata>

Find an array of file metadata using Valkyrie IDs, and map them to Hyrax::FileMetadata maintaining order based on given ids NOTE: Ignores non-existent ids and ids for non-file metadata resources.

Parameters:

  • ids (Array<Valkyrie::ID, String>)

Returns:

  • (Array<Hyrax::FileMetadata>)

    or empty array if there are no ids or none of the ids map to Hyrax::FileMetadata



56
57
58
59
# File 'app/services/hyrax/custom_queries/find_file_metadata.rb', line 56

def (ids:)
  results = query_service.find_many_by_ids(ids: ids)
  results.select { |resource| resource.is_a? Hyrax::FileMetadata }
end

#find_many_file_metadata_by_use(resource:, use:) ⇒ Array<Hyrax::FileMetadata] or empty array if there are no files with the requested use

Find file metadata for files within a resource that have the requested use.

Examples:

Hyrax.query_service.(use: ::RDF::URI("http://pcdm.org/ExtractedText"))

Parameters:

  • use (RDF::URI)

    uri for the desired use Type

Returns:

  • (Array<Hyrax::FileMetadata] or empty array if there are no files with the requested use)

    Array<Hyrax::FileMetadata] or empty array if there are no files with the requested use



66
67
68
69
70
71
# File 'app/services/hyrax/custom_queries/find_file_metadata.rb', line 66

def (resource:, use:)
  return [] if resource.try(:file_ids).blank?

  results = (ids: resource.file_ids)
  results.select { |fm| fm.pcdm_use.include?(use) }
end