Class: Solr::DocumentCollection

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/solr/document_collection.rb

Direct Known Subclasses

GroupedDocumentCollection

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(documents:, total_count:) ⇒ DocumentCollection

Returns a new instance of DocumentCollection.



15
16
17
18
# File 'lib/solr/document_collection.rb', line 15

def initialize(documents:, total_count:)
  @documents = documents
  @total_count = total_count
end

Instance Attribute Details

#documentsObject (readonly)

Returns the value of attribute documents.



4
5
6
# File 'lib/solr/document_collection.rb', line 4

def documents
  @documents
end

#total_countObject (readonly)

Returns the value of attribute total_count.



4
5
6
# File 'lib/solr/document_collection.rb', line 4

def total_count
  @total_count
end

Class Method Details

.emptyObject



11
12
13
# File 'lib/solr/document_collection.rb', line 11

def self.empty
  new(documents: [], total_count: 0)
end

.from_ids(ids, model_name:) ⇒ Object



6
7
8
9
# File 'lib/solr/document_collection.rb', line 6

def self.from_ids(ids, model_name:)
  documents = ids.map { |id| Solr::Document.new(id: id, model_name: model_name) }
  new(documents: documents, total_count: documents.count)
end

Instance Method Details

#+(other) ⇒ Object



39
40
41
42
43
44
# File 'lib/solr/document_collection.rb', line 39

def +(other)
  self.class.new(
    documents: documents + other.documents,
    total_count: total_count + other.total_count
  )
end

#each(&b) ⇒ Object



20
21
22
23
# File 'lib/solr/document_collection.rb', line 20

def each(&b)
  return enum_for(:each) unless block_given?
  documents.each(&b)
end

#find(id) ⇒ Object



51
52
53
# File 'lib/solr/document_collection.rb', line 51

def find(id)
  documents.find { |doc| doc.id == id }
end

#first(n) ⇒ Object



25
26
27
28
# File 'lib/solr/document_collection.rb', line 25

def first(n)
  new_documents = documents.first(n)
  self.class.new(documents: new_documents, total_count: new_documents.count)
end

#slice(range) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/solr/document_collection.rb', line 30

def slice(range)
  new_documents = documents[range]
  if new_documents
    self.class.new(documents: new_documents, total_count: new_documents.count)
  else
    self.class.empty
  end
end

#unshift(document) ⇒ Object



46
47
48
49
# File 'lib/solr/document_collection.rb', line 46

def unshift(document)
  @documents.unshift(document)
  @total_count += 1
end