1 module Databasedotcom
2 # A collection of Sobject or Record objects that holds a single page of results, and understands how to
3 # retrieve the next page, if any. Inherits from Array, thus, behaves as an Enumerable.
4
5 class Collection < Array
6 attr_reader :total_size, :next_page_url, :previous_page_url, :current_page_url, :client
7
8 # Creates a paginatable collection. You should never need to call this.
9 def initialize(client, total_size, next_page_url=nil, previous_page_url=nil, current_page_url=nil) #:nodoc:
10 @client = client
11 @total_size = total_size
12 @next_page_url = next_page_url
13 @previous_page_url = previous_page_url
14 @current_page_url = current_page_url
15 end
16
17 # Does this collection have a next page?
18 def next_page?
19 !!self.next_page_url
20 end
21
22 # Retrieve the next page of this collection. Returns the new collection, which is an empty collection if no next page exists
23 def next_page
24 self.next_page? ? @client.next_page(@next_page_url) : Databasedotcom::Collection.new(self.client, 0)
25 end
26
27 # Does this collection have a previous page?
28 def previous_page?
29 !!self.previous_page_url
30 end
31
32 # Retrieve the previous page of this collection. Returns the new collection, which is an empty collection if no previous page exists
33 def previous_page
34 self.previous_page? ? @client.previous_page(@previous_page_url) : Databasedotcom::Collection.new(self.client, 0)
35 end
36 end
37 en