class Mirah::Collection

Collections represent a pageable view into a given collection. They automatically let you iterate through record sets in a stable manner.

@example Iterate through a collection of multiple pages

while(collection.next_page?)
  collection = collection.next_page
  collection.records.each do |record|
    # process record
  end
end

Attributes

client[R]

The Mirah client, used for sending additional paged requests.

page_info[R]

The information on the current page as returned by the server

paging[R]

The current set of paging parameters

query[R]

The original query, stored to allow us to retrigger a query with a new page.

results[R]

The current results of the query

Public Class Methods

new(results:, page_info:, client:, query:) click to toggle source
# File lib/mirah/collection.rb, line 15
def initialize(results:, page_info:, client:, query:)
  @results = results
  @page_info = page_info
  @client = client
  @query = query
end

Public Instance Methods

length() click to toggle source
# File lib/mirah/collection.rb, line 37
def length
  results&.length
end
next_page() click to toggle source
# File lib/mirah/collection.rb, line 60
def next_page
  raise Errors::InvalidPage, 'This collection does not have a next page' unless next_page?

  return @next_page if @next_page

  @next_page = refresh_with_new_paging(next_page_params)
end
next_page?() click to toggle source
# File lib/mirah/collection.rb, line 41
def next_page?
  page_info.next_page?
end
prev_page() click to toggle source
# File lib/mirah/collection.rb, line 68
def prev_page
  raise Errors::InvalidPage, 'This collection does not have a previous page' unless prev_page?

  return @prev_page if @prev_page

  @prev_page = refresh_with_new_paging(prev_page_params)
end
prev_page?() click to toggle source
# File lib/mirah/collection.rb, line 45
def prev_page?
  page_info.prev_page?
end
refresh_with_new_paging(paging) click to toggle source
# File lib/mirah/collection.rb, line 49
def refresh_with_new_paging(paging)
  # Update everything as before, but set the new paging.
  client.query_connection(
    query[:query],
    query[:input],
    paging,
    query[:data_klass],
    query[:path]
  )
end

Private Instance Methods

next_page_params() click to toggle source
# File lib/mirah/collection.rb, line 78
def next_page_params
  last = query[:paging]
  Filters::Paging.new(
    after: page_info.end_cursor,
    before: nil,
    first: last.first || last.last,
    last: nil
  )
end
prev_page_params() click to toggle source
# File lib/mirah/collection.rb, line 88
def prev_page_params
  last = query[:paging]
  Filters::Paging.new(
    before: page_info.start_cursor,
    after: nil,
    last: last.last || last.first,
    first: nil
  )
end