Class: Valkyrie::Persistence::Solr::Queries::FindMembersQuery

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

Overview

Responsible for returning all members of a given resource as Resources

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource:, connection:, resource_factory:, model:) ⇒ FindMembersQuery

Returns a new instance of FindMembersQuery.

Parameters:



12
13
14
15
16
17
# File 'lib/valkyrie/persistence/solr/queries/find_members_query.rb', line 12

def initialize(resource:, connection:, resource_factory:, model:)
  @resource = resource
  @connection = connection
  @resource_factory = resource_factory
  @model = model
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



6
7
8
# File 'lib/valkyrie/persistence/solr/queries/find_members_query.rb', line 6

def connection
  @connection
end

#modelObject (readonly)

Returns the value of attribute model.



6
7
8
# File 'lib/valkyrie/persistence/solr/queries/find_members_query.rb', line 6

def model
  @model
end

#resourceObject (readonly)

Returns the value of attribute resource.



6
7
8
# File 'lib/valkyrie/persistence/solr/queries/find_members_query.rb', line 6

def resource
  @resource
end

#resource_factoryObject (readonly)

Returns the value of attribute resource_factory.



6
7
8
# File 'lib/valkyrie/persistence/solr/queries/find_members_query.rb', line 6

def resource_factory
  @resource_factory
end

Instance Method Details

#docsArray<Hash>

Query Solr for all members of the Valkyrie Resource If a model is specified, this is used to filter the results

Returns:

  • (Array<Hash>)


47
48
49
50
51
52
53
# File 'lib/valkyrie/persistence/solr/queries/find_members_query.rb', line 47

def docs
  options = { q: query, rows: 1_000_000_000 }
  options[:fq] = "{!raw f=internal_resource_ssim}#{model}" if model
  options[:defType] = 'lucene'
  result = connection.get("select", params: options)
  result["response"]["docs"]
end

#each {|Valkyrie::Resource| ... } ⇒ Object

Queries for all member Documents in the Solr index For each Document, it yields the Valkyrie Resource which was converted from it Results are ordered by the member IDs specified in the Valkyrie Resource attribute

Yields:



29
30
31
32
33
34
# File 'lib/valkyrie/persistence/solr/queries/find_members_query.rb', line 29

def each
  return [] if resource.id.blank?
  member_ids.map { |id| unordered_members.find { |member| member.id == id } }.reject(&:nil?).each do |member|
    yield member
  end
end

#idString

Retrieve the string value for the ID

Returns:

  • (String)


70
71
72
# File 'lib/valkyrie/persistence/solr/queries/find_members_query.rb', line 70

def id
  resource.id.to_s
end

#member_idsArray<Valkyrie::ID>

Access the IDs of the members for the Valkyrie Resource

Returns:



57
58
59
# File 'lib/valkyrie/persistence/solr/queries/find_members_query.rb', line 57

def member_ids
  resource.respond_to?(:member_ids) ? Array.wrap(resource.member_ids) : []
end

#queryString

Generate the Solr join query using the id_ssi field



64
65
66
# File 'lib/valkyrie/persistence/solr/queries/find_members_query.rb', line 64

def query
  "{!join from=#{MEMBER_IDS} to=join_id_ssi}id:#{id}"
end

#runArray<Valkyrie::Resource>

Iterate over each Solr Document and convert each Document into a Valkyrie Resource

Returns:



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

def run
  enum_for(:each)
end

#unordered_membersArray<Valkyrie::Resource>

Retrieving the Solr Documents for the member resources, construct Valkyrie Resources for each

Returns:



38
39
40
41
42
# File 'lib/valkyrie/persistence/solr/queries/find_members_query.rb', line 38

def unordered_members
  @unordered_members ||= docs.map do |doc|
    resource_factory.to_resource(object: doc)
  end
end