Class: Force::Collection

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/force/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.



7
8
9
10
# File 'lib/force/collection.rb', line 7

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

Instance Method Details

#current_pageObject

Return array of the elements on the current page



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

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

#eachObject

Yield each value on each page.



13
14
15
16
17
# File 'lib/force/collection.rb', line 13

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

  next_page.each { |record| yield record } if has_next_page?
end

#has_next_page?Boolean

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

Returns:

  • (Boolean)


36
37
38
# File 'lib/force/collection.rb', line 36

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

#next_pageObject

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



41
42
43
# File 'lib/force/collection.rb', line 41

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

#pagesObject

Return the current and all of the following pages.



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

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.



20
21
22
# File 'lib/force/collection.rb', line 20

def size
  @raw_page['totalSize']
end