Class: Mirah::Collection
- Inherits:
-
Object
- Object
- Mirah::Collection
- 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.
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
The Mirah client, used for sending additional paged requests.
-
#page_info ⇒ Object
readonly
The information on the current page as returned by the server.
-
#paging ⇒ Object
readonly
The current set of paging parameters.
-
#query ⇒ Object
readonly
The original query, stored to allow us to retrigger a query with a new page.
-
#results ⇒ Object
readonly
The current results of the query.
Instance Method Summary collapse
-
#initialize(results:, page_info:, client:, query:) ⇒ Collection
constructor
A new instance of Collection.
- #length ⇒ Object
- #next_page ⇒ Object
- #next_page? ⇒ Boolean
- #prev_page ⇒ Object
- #prev_page? ⇒ Boolean
- #refresh_with_new_paging(paging) ⇒ Object
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
#client ⇒ Object (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_info ⇒ Object (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 |
#paging ⇒ Object (readonly)
The current set of paging parameters
32 33 34 |
# File 'lib/mirah/collection.rb', line 32 def paging @paging end |
#query ⇒ Object (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 |
#results ⇒ Object (readonly)
The current results of the query
26 27 28 |
# File 'lib/mirah/collection.rb', line 26 def results @results end |
Instance Method Details
#length ⇒ Object
37 38 39 |
# File 'lib/mirah/collection.rb', line 37 def length results&.length end |
#next_page ⇒ Object
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
41 42 43 |
# File 'lib/mirah/collection.rb', line 41 def next_page? page_info.next_page? end |
#prev_page ⇒ Object
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
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 |