Class: Restforce::Collection

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/restforce/collection.rb

Instance Method Summary collapse

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.



9
10
11
12
# File 'lib/restforce/collection.rb', line 9

def initialize(hash, client)
  @client = client
  @raw_page = hash
end

Instance Method Details

#current_pageObject

Return array of the elements on the current page



37
38
39
# File 'lib/restforce/collection.rb', line 37

def current_page
  first(@raw_page['records'].size)
end

#eachObject

Yield each value on each page.



15
16
17
18
19
20
21
22
23
# File 'lib/restforce/collection.rb', line 15

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.

Returns:

  • (Boolean)


47
48
49
# File 'lib/restforce/collection.rb', line 47

def has_next_page?
  !@raw_page['nextRecordsUrl'].nil?
end

#next_pageObject

Returns the next page as a Restforce::Collection if it’s available, nil otherwise.



52
53
54
# File 'lib/restforce/collection.rb', line 52

def next_page
  @client.get(@raw_page['nextRecordsUrl']).body if has_next_page?
end

#page_sizeObject

Return the size of each page in the collection



26
27
28
# File 'lib/restforce/collection.rb', line 26

def page_size
  @raw_page['records'].size
end

#pagesObject

Return the current and all of the following pages.



42
43
44
# File 'lib/restforce/collection.rb', line 42

def pages
  [self] + (has_next_page? ? next_page.pages : [])
end

#sizeObject Also known as: length

Return the size of the Collection without making any additional requests.



31
32
33
# File 'lib/restforce/collection.rb', line 31

def size
  @raw_page['totalSize']
end