Class: Endeca::DocumentCollection
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 todocuments
. (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
-
#raw ⇒ Object
readonly
Returns the value of attribute raw.
Instance Method Summary collapse
- #aggregate? ⇒ Boolean
- #attributes ⇒ Object
-
#breadcrumbs ⇒ Object
The collection of Breadcrumb objects for the collection.
-
#documents ⇒ Object
The internal collection of Document objects.
-
#initialize(raw, document_klass = Document) ⇒ DocumentCollection
constructor
A new instance of DocumentCollection.
-
#is_a?(klass) ⇒ Boolean
(also: #kind_of?)
Lie about our class.
-
#next_page ⇒ Object
The next page number.
-
#previous_page ⇒ Object
The previous page number.
-
#refinement_by_name(name) ⇒ Object
Return the refinement by name.
-
#refinements ⇒ Object
The collection of Refinement objects for the collection.
Methods included from ClassToProc
Methods included from Readers
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
#raw ⇒ Object (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
70 71 72 |
# File 'lib/endeca/document_collection.rb', line 70 def aggregate? @raw['AggrRecords'] ? true : false end |
#attributes ⇒ Object
29 30 31 |
# File 'lib/endeca/document_collection.rb', line 29 def attributes @raw['MetaInfo'] || {} end |
#breadcrumbs ⇒ Object
The collection of Breadcrumb objects for the collection.
80 81 82 |
# File 'lib/endeca/document_collection.rb', line 80 def @breadcrumbs ||= (@raw['Breadcrumbs'] || []).map(&Breadcrumb) end |
#documents ⇒ Object
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 (===
)
106 107 108 |
# File 'lib/endeca/document_collection.rb', line 106 def is_a?(klass) klass == Array || super(klass) end |
#next_page ⇒ Object
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_page ⇒ Object
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 |
#refinements ⇒ Object
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 |