class Restforce::Collection
def count(*args)
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
def current_page
def current_page first(@raw_page['records'].size) end
def each(&block)
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
def empty?
def empty? size.zero? end
def has_next_page?
def has_next_page? !@raw_page['nextRecordsUrl'].nil? end
def initialize(hash, client)
Given a hash and client, will create an Enumerator that will lazily
def initialize(hash, client) @client = client @raw_page = hash end
def next_page
def next_page @client.get(@raw_page['nextRecordsUrl']).body if has_next_page? end
def page_size
def page_size @raw_page['records'].size end
def pages
def pages [self] + (has_next_page? ? next_page.pages : []) end
def size
some reason, the [List View Results](https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/resources_listviewresults.htm)
Most of the Salesforce API returns this in the `totalSize` attribute. For
we can rely on the total count of results which Salesforce returns.
requests and going through all of the pages of results, one by one. Instead,
Return the number of items in the Collection without making any additional
def size @raw_page['totalSize'] || @raw_page['size'] end