Class: Mirah::Collection

Inherits:
Object
  • Object
show all
Defined in:
lib/mirah/collection.rb

Overview

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

Examples:

Iterate through a collection of multiple pages

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(results:, page_info:, client:, query:) ⇒ Collection

Returns a new instance of Collection.



15
16
17
18
19
20
# 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

Instance Attribute Details

#clientObject (readonly)

The Mirah client, used for sending additional paged requests.



23
24
25
# File 'lib/mirah/collection.rb', line 23

def client
  @client
end

#page_infoObject (readonly)

The information on the current page as returned by the server



29
30
31
# File 'lib/mirah/collection.rb', line 29

def page_info
  @page_info
end

#pagingObject (readonly)

The current set of paging parameters



32
33
34
# File 'lib/mirah/collection.rb', line 32

def paging
  @paging
end

#queryObject (readonly)

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



35
36
37
# File 'lib/mirah/collection.rb', line 35

def query
  @query
end

#resultsObject (readonly)

The current results of the query



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

def results
  @results
end

Instance Method Details

#lengthObject



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

def length
  results&.length
end

#next_pageObject



60
61
62
63
64
65
66
# 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?Boolean

Returns:

  • (Boolean)


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

def next_page?
  page_info.next_page?
end

#prev_pageObject



68
69
70
71
72
73
74
# 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?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/mirah/collection.rb', line 45

def prev_page?
  page_info.prev_page?
end

#refresh_with_new_paging(paging) ⇒ Object



49
50
51
52
53
54
55
56
57
58
# 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