Class: Endeca::DocumentCollection

Inherits:
Object
  • Object
show all
Extended by:
ClassToProc
Includes:
Readers
Defined in:
lib/endeca/document_collection.rb

Overview

Endeca DocumentCollections wrap a collection of Endeca Documents to provide access to metadata returned by the Endeca query. They behave like a simple Array in most cases (e.g. iteration) but also provide access to refinements.

Attribute Readers

DocumentCollection provides attribute readers for collection metadata in an interface that is compatible with WillPaginate::Collection for use in views.

Method Delegation

DocumentCollections delegate array-like behavior to their embedded document collection, (documents). In most cases a DocumentCollection can be used as if it were an array of Document objects. (Array delegation pattern borrowed from Rake::FileList)

Constant Summary collapse

ARRAY_METHODS =

List of array methods (that are not in Object) that need to be delegated to documents.

(Array.instance_methods - Object.instance_methods).map { |n| n.to_s }
MUST_DEFINE =

List of additional methods that must be delegated to documents.

%w[to_a to_ary inspect]

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ClassToProc

to_proc

Methods included from Readers

included

Constructor Details

#initialize(raw, document_klass = Document) ⇒ DocumentCollection

Returns a new instance of DocumentCollection.



24
25
26
27
# File 'lib/endeca/document_collection.rb', line 24

def initialize(raw, document_klass=Document)
  @raw = raw
  @document_klass = document_klass
end

Instance Attribute Details

#rawObject (readonly)

Returns the value of attribute raw.



23
24
25
# File 'lib/endeca/document_collection.rb', line 23

def raw
  @raw
end

Instance Method Details

#aggregate?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/endeca/document_collection.rb', line 70

def aggregate?
  @raw['AggrRecords'] ? true : false
end

#attributesObject



29
30
31
# File 'lib/endeca/document_collection.rb', line 29

def attributes
  @raw['MetaInfo'] || {}
end

The collection of Breadcrumb objects for the collection.



80
81
82
# File 'lib/endeca/document_collection.rb', line 80

def breadcrumbs
  @breadcrumbs ||= (@raw['Breadcrumbs'] || []).map(&Breadcrumb)
end

#documentsObject

The internal collection of Document objects. Array methods are delegated here.



60
61
62
63
64
65
66
67
68
# File 'lib/endeca/document_collection.rb', line 60

def documents
  if @raw['Records']
    @documents ||= @raw['Records'].map(&@document_klass)
  elsif aggregate?
    @documents ||= @raw['AggrRecords'].map{|aggregate| aggregate['Records'].first}.map(&@document_klass)
  else
    []
  end
end

#is_a?(klass) ⇒ Boolean Also known as: kind_of?

Lie about our class. Borrowed from Rake::FileList Note: Does not work for case equality (===)

Returns:

  • (Boolean)


106
107
108
# File 'lib/endeca/document_collection.rb', line 106

def is_a?(klass)
  klass == Array || super(klass)
end

#next_pageObject

The next page number. Returns nil if there is no next page. Borrowed from WillPaginate for compatibility



55
56
57
# File 'lib/endeca/document_collection.rb', line 55

def next_page
  current_page < total_pages ? (current_page + 1) : nil
end

#previous_pageObject

The previous page number. Returns nil if there is no previous page. Borrowed from WillPaginate for compatibility.



48
49
50
# File 'lib/endeca/document_collection.rb', line 48

def previous_page
  current_page > 1 ? (current_page - 1) : nil
end

#refinement_by_name(name) ⇒ Object

Return the refinement by name



85
86
87
# File 'lib/endeca/document_collection.rb', line 85

def refinement_by_name(name)
  refinements.find{|ref| ref.name.downcase == name.downcase}
end

#refinementsObject

The collection of Refinement objects for the collection.



75
76
77
# File 'lib/endeca/document_collection.rb', line 75

def refinements
  @refinements ||= (@raw['Refinements'] || []).map(&Refinement)
end