Class: Restforce::Collection
- Inherits:
-
Object
- Object
- Restforce::Collection
- Includes:
- Enumerable
- Defined in:
- lib/restforce/collection.rb
Instance Method Summary collapse
-
#current_page ⇒ Object
Return array of the elements on the current page.
-
#each ⇒ Object
Yield each value on each page.
-
#has_next_page? ⇒ Boolean
Returns true if there is a pointer to the next page.
-
#initialize(hash, client) ⇒ Collection
constructor
Given a hash and client, will create an Enumerator that will lazily request Salesforce for the next page of results.
-
#next_page ⇒ Object
Returns the next page as a Restforce::Collection if it’s available, nil otherwise.
-
#page_size ⇒ Object
Return the size of each page in the collection.
-
#pages ⇒ Object
Return the current and all of the following pages.
-
#size ⇒ Object
(also: #length)
Return the size of the Collection without making any additional requests.
Constructor Details
#initialize(hash, client) ⇒ Collection
Given a hash and client, will create an Enumerator that will lazily request Salesforce for the next page of results.
7 8 9 10 |
# File 'lib/restforce/collection.rb', line 7 def initialize(hash, client) @client = client @raw_page = hash end |
Instance Method Details
#current_page ⇒ Object
Return array of the elements on the current page
35 36 37 |
# File 'lib/restforce/collection.rb', line 35 def current_page first(@raw_page['records'].size) end |
#each ⇒ Object
Yield each value on each page.
13 14 15 16 17 18 19 20 21 |
# File 'lib/restforce/collection.rb', line 13 def each @raw_page['records'].each { |record| yield Restforce::Mash.build(record, @client) } np = next_page while np np.current_page.each { |record| yield record } np = np.next_page end end |
#has_next_page? ⇒ Boolean
Returns true if there is a pointer to the next page.
45 46 47 |
# File 'lib/restforce/collection.rb', line 45 def has_next_page? !@raw_page['nextRecordsUrl'].nil? end |
#next_page ⇒ Object
Returns the next page as a Restforce::Collection if it’s available, nil otherwise.
50 51 52 |
# File 'lib/restforce/collection.rb', line 50 def next_page @client.get(@raw_page['nextRecordsUrl']).body if has_next_page? end |
#page_size ⇒ Object
Return the size of each page in the collection
24 25 26 |
# File 'lib/restforce/collection.rb', line 24 def page_size @raw_page['records'].size end |
#pages ⇒ Object
Return the current and all of the following pages.
40 41 42 |
# File 'lib/restforce/collection.rb', line 40 def pages [self] + (has_next_page? ? next_page.pages : []) end |
#size ⇒ Object Also known as: length
Return the size of the Collection without making any additional requests.
29 30 31 |
# File 'lib/restforce/collection.rb', line 29 def size @raw_page['totalSize'] end |