Class: Asari::Collection
- Inherits:
- BasicObject
- Defined in:
- lib/asari/collection.rb
Overview
Public: The Asari::Collection object represents a page of data returned from CloudSearch. It very closely delegates to an array containing the intended results, but provides a few extra methods containing metadata about the current pagination state: current_page, page_size, total_entries, offset, and total_pages.
Asari::Collection is compatible with will_paginate collections, and the two can be used interchangeably for the purposes of pagination.
Instance Attribute Summary collapse
-
#current_page ⇒ Object
readonly
Returns the value of attribute current_page.
-
#page_size ⇒ Object
readonly
Returns the value of attribute page_size.
-
#total_entries ⇒ Object
readonly
Returns the value of attribute total_entries.
-
#total_pages ⇒ Object
readonly
Returns the value of attribute total_pages.
Class Method Summary collapse
-
.sandbox_fake ⇒ Object
Internal: method for returning a sandbox-friendly empty search result.
Instance Method Summary collapse
- #class ⇒ Object
-
#initialize(httparty_response, page_size) ⇒ Collection
constructor
Internal: This object should really only ever be instantiated from within Asari code.
- #method_missing(method, *args, &block) ⇒ Object
- #offset ⇒ Object
-
#replace(array) ⇒ Object
Public: replace the current data collection with a new data collection, without losing pagination information.
- #respond_to?(method, include_all = false) ⇒ Boolean
Constructor Details
#initialize(httparty_response, page_size) ⇒ Collection
Internal: This object should really only ever be instantiated from within Asari code. The Asari Collection knows how to build itself from an HTTParty::Response object representing a search query result from CloudSearch.
We also have to pass the page size in directly, because the CloudSearch response doesn’t have any data about page size. It’s cool, though. I guess.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/asari/collection.rb', line 32 def initialize(httparty_response, page_size) resp = httparty_response.parsed_response @total_entries = resp["hits"]["found"] @page_size = page_size complete_pages = (@total_entries / @page_size) @total_pages = (@total_entries % @page_size > 0) ? complete_pages + 1 : complete_pages # There's always one page, even for no results @total_pages = 1 if @total_pages == 0 start = resp["hits"]["start"] @current_page = (start / page_size) + 1 if resp["hits"]["hit"].first && resp["hits"]["hit"].first["data"] @data = {} resp["hits"]["hit"].each { |hit| @data[hit["id"]] = hit["data"]} else @data = resp["hits"]["hit"].map { |hit| hit["id"] } end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
80 81 82 |
# File 'lib/asari/collection.rb', line 80 def method_missing(method, *args, &block) @data.public_send(method, *args, &block) end |
Instance Attribute Details
#current_page ⇒ Object (readonly)
Returns the value of attribute current_page.
12 13 14 |
# File 'lib/asari/collection.rb', line 12 def current_page @current_page end |
#page_size ⇒ Object (readonly)
Returns the value of attribute page_size.
13 14 15 |
# File 'lib/asari/collection.rb', line 13 def page_size @page_size end |
#total_entries ⇒ Object (readonly)
Returns the value of attribute total_entries.
14 15 16 |
# File 'lib/asari/collection.rb', line 14 def total_entries @total_entries end |
#total_pages ⇒ Object (readonly)
Returns the value of attribute total_pages.
15 16 17 |
# File 'lib/asari/collection.rb', line 15 def total_pages @total_pages end |
Class Method Details
.sandbox_fake ⇒ Object
Internal: method for returning a sandbox-friendly empty search result.
19 20 21 |
# File 'lib/asari/collection.rb', line 19 def self.sandbox_fake Collection.new(::OpenStruct.new(:parsed_response => {"hits" => { "found" => 0, "start" => 0, "hit" => []}}), 10) end |
Instance Method Details
#class ⇒ Object
72 73 74 |
# File 'lib/asari/collection.rb', line 72 def class ::Asari::Collection end |
#offset ⇒ Object
52 53 54 |
# File 'lib/asari/collection.rb', line 52 def offset (@current_page - 1) * @page_size end |
#replace(array) ⇒ Object
Public: replace the current data collection with a new data collection, without losing pagination information. Useful for mapping results, etc.
Examples:
results = @asari.find("test") #=> ["1", "3", "10", "28"]
results.replace(results.map { |id| User.find(id)}) #=> [<User...>,<User...>,<User...>]
Returns: self. #replace is a chainable method.
66 67 68 69 70 |
# File 'lib/asari/collection.rb', line 66 def replace(array) @data = array self end |
#respond_to?(method, include_all = false) ⇒ Boolean
76 77 78 |
# File 'lib/asari/collection.rb', line 76 def respond_to?(method, include_all = false) @data.respond_to?(method, false) end |