Class: Endeca::DocumentCollection

Inherits:
Object
  • Object
show all
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 Readers

included

Constructor Details

#initialize(raw, document_klass = Document) ⇒ DocumentCollection

Returns a new instance of DocumentCollection.



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

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

Instance Attribute Details

#rawObject (readonly)

Returns the value of attribute raw.



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

def raw
  @raw
end

Instance Method Details

#aggregate?Boolean

Returns:

  • (Boolean)


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

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

#attributesObject



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

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

The collection of Breadcrumb objects for the collection.



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

def breadcrumbs
  @breadcrumbs ||= (@raw['Breadcrumbs'] || []).map{|data| Breadcrumb.create(data)}
end

#documentsObject

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



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

def documents
  if @raw['Records']
    @documents ||= @raw['Records'].map{|record| @document_klass.new(record)}
  elsif aggregate?
    @documents ||= @raw['AggrRecords'].map{|aggregate| aggregate['Records'].first}.map{|record| @document_klass.new(record)}
  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)


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

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



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

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.



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

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

#refinement_by_name(name) ⇒ Object

Return the refinement by name



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

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

#refinementsObject

The collection of Refinement objects for the collection.



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

def refinements
  @refinements ||= (@raw['Refinements'] || []).map{|data| Refinement.new(data)}
end