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:, standardize_query_result:) ⇒ FindMembersQuery



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

def initialize(resource:, connection:, resource_factory:, model:, standardize_query_result:)
  @resource = resource
  @connection = connection
  @resource_factory = resource_factory
  @model = model
  @standardize_query_result = standardize_query_result
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

#standardize_query_resultObject (readonly)

Returns the value of attribute standardize_query_result.



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

def standardize_query_result
  @standardize_query_result
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



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

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:



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/valkyrie/persistence/solr/queries/find_members_query.rb', line 30

def each
  return [] unless resource.id.present?
  if standardize_query_result
    member_ids.map { |id| unordered_members.find { |member| member.id == id } }.reject(&:nil?).each do |member|
      yield member
    end
  else
    unordered_members.sort_by { |x| member_ids.index(x.id) }.each do |member|
      yield member
    end
  end
end

#idString

Retrieve the string value for the ID



77
78
79
# File 'lib/valkyrie/persistence/solr/queries/find_members_query.rb', line 77

def id
  resource.id.to_s
end

#member_idsArray<Valkyrie::ID>

Access the IDs of the members for the Valkyrie Resource



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

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



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

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



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

def run
  enum_for(:each)
end

#unordered_membersArray<Valkyrie::Resource>

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



45
46
47
48
49
# File 'lib/valkyrie/persistence/solr/queries/find_members_query.rb', line 45

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