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

#count(*args) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/restforce/collection.rb', line 42

def count(*args)
  # By default, `Enumerable`'s `#count` uses `#each`, which means going through all
  # of the pages of results, one by one. Instead, we can use `#size` which we have
  # already overridden to work in a smarter, more efficient way. This only works for
  # the simple version of `#count` with no arguments. When called with an argument or
  # a block, you need to know what the items in the collection actually are, so we
  # call `super` and end up iterating through each item in the collection.
  return size unless block_given? || !args.empty?

  super
end

#current_pageObject

Return array of the elements on the current page



60
61
62
# File 'lib/restforce/collection.rb', line 60

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

#each(&block) ⇒ Object

Yield each value on each page.



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

def each(&block)
  @raw_page['records'].each { |record| yield Restforce::Mash.build(record, @client) }

  np = next_page
  while np
    np.current_page.each(&block)
    np = np.next_page
  end
end

#empty?Boolean

Returns true if the size of the Collection is zero.

Returns:

  • (Boolean)


55
56
57
# File 'lib/restforce/collection.rb', line 55

def empty?
  size.zero?
end

#has_next_page?Boolean

Returns true if there is a pointer to the next page.

Returns:

  • (Boolean)


70
71
72
# File 'lib/restforce/collection.rb', line 70

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.



75
76
77
# File 'lib/restforce/collection.rb', line 75

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.



65
66
67
# File 'lib/restforce/collection.rb', line 65

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

#sizeObject Also known as: length

Return the number of items in the Collection without making any additional requests and going through all of the pages of results, one by one. Instead, we can rely on the total count of results which Salesforce returns.

Most of the Salesforce API returns this in the ‘totalSize` attribute. For some reason, the [List View Results](developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/resources_listviewresults.htm) endpoint (and maybe others?!) uses the `size` attribute.



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

def size
  @raw_page['totalSize'] || @raw_page['size']
end