Class: ActiveResource::PaginatedCollection
- Inherits:
-
Collection
- Object
- Collection
- ActiveResource::PaginatedCollection
- Defined in:
- lib/esp/extensions/active_resource/paginated_collection.rb
Overview
Provides a mean to call the Evident.io API to easily retrieve paginated data
Instance Attribute Summary collapse
-
#from ⇒ Object
:nodoc:.
-
#last_page_params ⇒ Object
readonly
:nodoc:.
-
#next_page_params ⇒ Object
readonly
:nodoc:.
-
#previous_page_params ⇒ Object
readonly
:nodoc:.
Instance Method Summary collapse
-
#current_page_number ⇒ Object
The current page number of data.
-
#first_page ⇒ Object
Returns a new PaginatedCollection with the first page of results.
-
#first_page! ⇒ Object
Updates the existing PaginatedCollection object with the first page of data when not on the first page.
-
#initialize(elements = []) ⇒ PaginatedCollection
constructor
:nodoc:.
-
#last_page ⇒ Object
Returns a new PaginatedCollection with the last page of results.
-
#last_page! ⇒ Object
Updates the existing PaginatedCollection object with the last page of data when not on the last page.
-
#last_page? ⇒ Boolean
Returns whether or not the collection is on the last page.
-
#last_page_number ⇒ Object
The last page number of data.
-
#next_page ⇒ Object
Returns a new PaginatedCollection with the next page of results.
-
#next_page! ⇒ Object
Updates the existing PaginatedCollection object with the last page of data when not on the last page.
-
#next_page? ⇒ Boolean
Returns whether or not there is a next page of data in the collection.
-
#next_page_number ⇒ Object
The next page number of data.
-
#page(page_number = nil) ⇒ Object
Returns a new PaginatedCollection with the
page_numberpage of data. -
#page!(page_number) ⇒ Object
Returns a new PaginatedCollection with the
page_numberpage of data when not already on pagepage_number. -
#previous_page ⇒ Object
Returns a new PaginatedCollection with the previous page of results.
-
#previous_page! ⇒ Object
Updates the existing PaginatedCollection object with the previous page of data when not on the first page.
-
#previous_page? ⇒ Boolean
Returns whether or not there is a previous page of data in the collection.
-
#previous_page_number ⇒ Object
The previous page number of data.
Constructor Details
#initialize(elements = []) ⇒ PaginatedCollection
:nodoc:
9 10 11 12 13 14 15 16 17 |
# File 'lib/esp/extensions/active_resource/paginated_collection.rb', line 9 def initialize(elements = []) #:nodoc: # If a collection is sent without the pagination links, then elements will just be an array. if elements.is_a? Hash super(elements['data']) parse_pagination_links(elements['links']) else super(elements) end end |
Instance Attribute Details
#from ⇒ Object
:nodoc:
7 8 9 |
# File 'lib/esp/extensions/active_resource/paginated_collection.rb', line 7 def from @from end |
#last_page_params ⇒ Object (readonly)
:nodoc:
6 7 8 |
# File 'lib/esp/extensions/active_resource/paginated_collection.rb', line 6 def last_page_params @last_page_params end |
#next_page_params ⇒ Object (readonly)
:nodoc:
6 7 8 |
# File 'lib/esp/extensions/active_resource/paginated_collection.rb', line 6 def next_page_params @next_page_params end |
#previous_page_params ⇒ Object (readonly)
:nodoc:
6 7 8 |
# File 'lib/esp/extensions/active_resource/paginated_collection.rb', line 6 def previous_page_params @previous_page_params end |
Instance Method Details
#current_page_number ⇒ Object
The current page number of data.
146 147 148 |
# File 'lib/esp/extensions/active_resource/paginated_collection.rb', line 146 def current_page_number (previous_page_number.to_i + 1).to_s end |
#first_page ⇒ Object
Returns a new PaginatedCollection with the first page of results.
Returns self when on the first page and no API call is made.
Example
alerts.current_page_number # => 5
first_page = alerts.first_page
alerts.current_page_number # => 5
first_page.current_page_number # => 1
28 29 30 |
# File 'lib/esp/extensions/active_resource/paginated_collection.rb', line 28 def first_page previous_page? ? resource_class.where(original_params.merge(from: from, page: { number: 1 })) : self end |
#first_page! ⇒ Object
Updates the existing PaginatedCollection object with the first page of data when not on the first page.
Example
alerts.current_page_number # => 5
alerts.first_page!
alerts.current_page_number # => 1
38 39 40 |
# File 'lib/esp/extensions/active_resource/paginated_collection.rb', line 38 def first_page! first_page.tap { |page| update_self(page) } end |
#last_page ⇒ Object
Returns a new PaginatedCollection with the last page of results.
Returns self when on the last page and no API call is made.
Example
alerts.current_page_number # => 5
last_page = alerts.last_page
alerts.current_page_number # => 5
last_page.current_page_number # => 25
97 98 99 |
# File 'lib/esp/extensions/active_resource/paginated_collection.rb', line 97 def last_page !last_page? ? resource_class.where(original_params.merge(last_page_params.merge(from: from))) : self end |
#last_page! ⇒ Object
Updates the existing PaginatedCollection object with the last page of data when not on the last page.
Example
alerts.current_page_number # => 5
alerts.last_page!
alerts.current_page_number # => 25
107 108 109 |
# File 'lib/esp/extensions/active_resource/paginated_collection.rb', line 107 def last_page! last_page.tap { |page| update_self(page) } end |
#last_page? ⇒ Boolean
Returns whether or not the collection is on the last page.
176 177 178 |
# File 'lib/esp/extensions/active_resource/paginated_collection.rb', line 176 def last_page? last_page_number.nil? end |
#last_page_number ⇒ Object
The last page number of data.
161 162 163 |
# File 'lib/esp/extensions/active_resource/paginated_collection.rb', line 161 def last_page_number Hash(last_page_params).fetch('page', {}).fetch('number', nil) end |
#next_page ⇒ Object
Returns a new PaginatedCollection with the next page of results.
Returns self when on the last page and no API call is made.
Example
alerts.current_page_number # => 5
next_page = alerts.next_page
alerts.current_page_number # => 5
next_page.current_page_number # => 6
74 75 76 |
# File 'lib/esp/extensions/active_resource/paginated_collection.rb', line 74 def next_page next_page? ? resource_class.where(original_params.merge(next_page_params.merge(from: from))) : self end |
#next_page! ⇒ Object
Updates the existing PaginatedCollection object with the last page of data when not on the last page.
Example
alerts.current_page_number # => 5
alerts.next_page!
alerts.current_page_number # => 6
84 85 86 |
# File 'lib/esp/extensions/active_resource/paginated_collection.rb', line 84 def next_page! next_page.tap { |page| update_self(page) } end |
#next_page? ⇒ Boolean
Returns whether or not there is a next page of data in the collection.
171 172 173 |
# File 'lib/esp/extensions/active_resource/paginated_collection.rb', line 171 def next_page? !next_page_number.nil? end |
#next_page_number ⇒ Object
The next page number of data.
156 157 158 |
# File 'lib/esp/extensions/active_resource/paginated_collection.rb', line 156 def next_page_number Hash(next_page_params).fetch('page', {}).fetch('number', nil) end |
#page(page_number = nil) ⇒ Object
Returns a new PaginatedCollection with the page_number page of data.
Returns self when page_number == #current_page_number
Attribute
page_number - The page number of the data wanted. page_number must be between 1 and #last_page_number.
Example
alerts.current_page_number # => 5
page = alerts.page(2)
alerts.current_page_number # => 5
page.current_page_number # => 2
124 125 126 127 128 129 |
# File 'lib/esp/extensions/active_resource/paginated_collection.rb', line 124 def page(page_number = nil) fail ArgumentError, "You must supply a page number." unless page_number.present? fail ArgumentError, "Page number cannot be less than 1." if page_number.to_i < 1 fail ArgumentError, "Page number cannot be greater than the last page number." if page_number.to_i > last_page_number.to_i page_number.to_i != current_page_number.to_i ? resource_class.where(original_params.merge(from: from, page: { number: page_number, size: (next_page_params || previous_page_params)['page']['size'] })) : self end |
#page!(page_number) ⇒ Object
Returns a new PaginatedCollection with the page_number page of data when not already on page page_number.
Attribute
page_number - The page number of the data wanted. page_number must be between 1 and #last_page_number.
Example
alerts.current_page_number # => 5
alerts.page!(2)
alerts.current_page_number # => 2
141 142 143 |
# File 'lib/esp/extensions/active_resource/paginated_collection.rb', line 141 def page!(page_number) page(page_number).tap { |page| update_self(page) } end |
#previous_page ⇒ Object
Returns a new PaginatedCollection with the previous page of results.
Returns self when on the first page and no API call is made.
Example
alerts.current_page_number # => 5
previous_page = alerts.previous_page
alerts.current_page_number # => 5
previous_page.current_page_number # => 4
51 52 53 |
# File 'lib/esp/extensions/active_resource/paginated_collection.rb', line 51 def previous_page previous_page? ? resource_class.where(original_params.merge(previous_page_params.merge(from: from))) : self end |
#previous_page! ⇒ Object
Updates the existing PaginatedCollection object with the previous page of data when not on the first page.
Example
alerts.current_page_number # => 5
alerts.previous_page!
alerts.current_page_number # => 4
61 62 63 |
# File 'lib/esp/extensions/active_resource/paginated_collection.rb', line 61 def previous_page! previous_page.tap { |page| update_self(page) } end |
#previous_page? ⇒ Boolean
Returns whether or not there is a previous page of data in the collection.
166 167 168 |
# File 'lib/esp/extensions/active_resource/paginated_collection.rb', line 166 def previous_page? !previous_page_number.nil? end |
#previous_page_number ⇒ Object
The previous page number of data.
151 152 153 |
# File 'lib/esp/extensions/active_resource/paginated_collection.rb', line 151 def previous_page_number Hash(previous_page_params).fetch('page', {}).fetch('number', nil) end |