Class: ActiveResource::PaginatedCollection

Inherits:
Collection
  • Object
show all
Defined in:
lib/esp/extensions/active_resource/paginated_collection.rb

Instance Method Summary collapse

Instance Method Details

#current_page_numberString

The current page number of data.

Returns:

  • (String)


159
160
161
# File 'lib/esp/extensions/active_resource/paginated_collection.rb', line 159

def current_page_number
  (previous_page_number.to_i + 1).to_s
end

#first_pagePaginatedCollection, self

Returns the first page of results.

Returns self (and no API call is made) when already on the first page.

Examples:

alerts.current_page_number # => 5
first_page = alerts.first_page
alerts.current_page_number # => 5
first_page.current_page_number # => 1

Returns:



35
36
37
# File 'lib/esp/extensions/active_resource/paginated_collection.rb', line 35

def first_page
  previous_page? ? updated_collection(from: from, page: { number: 1 }) : self
end

#first_page!PaginatedCollection, self

Updates the existing PaginatedCollection object with the first page of data when not on the first page.

Examples:

alerts.current_page_number # => 5
alerts.first_page!
alerts.current_page_number # => 1

Returns:



46
47
48
# File 'lib/esp/extensions/active_resource/paginated_collection.rb', line 46

def first_page!
  first_page.tap { |page| update_self(page) }
end

#last_pagePaginatedCollection, self

Returns the last page of results.

Returns self (and no API call is made) when already on the last page.

Examples:

alerts.current_page_number # => 5
last_page = alerts.last_page
alerts.current_page_number # => 5
last_page.current_page_number # => 25

Returns:



110
111
112
# File 'lib/esp/extensions/active_resource/paginated_collection.rb', line 110

def last_page
  !last_page? ? updated_collection(last_page_params.merge(from: from)) : self
end

#last_page!PaginatedCollection, self

Updates the existing PaginatedCollection object with the last page of data when not on the last page.

Examples:

alerts.current_page_number # => 5
alerts.last_page!
alerts.current_page_number # => 25

Returns:



121
122
123
# File 'lib/esp/extensions/active_resource/paginated_collection.rb', line 121

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.

Returns:

  • (Boolean)


201
202
203
# File 'lib/esp/extensions/active_resource/paginated_collection.rb', line 201

def last_page?
  last_page_number.nil?
end

#last_page_numberString?

The last page number of data.

Returns:

  • (String, nil)


180
181
182
# File 'lib/esp/extensions/active_resource/paginated_collection.rb', line 180

def last_page_number
  Hash(last_page_params).fetch('page', {}).fetch('number', nil)
end

#next_pagePaginatedCollection, self

Returns the next page of results.

Returns self (and no API call is made) when already on the last page.

Examples:

alerts.current_page_number # => 5
next_page = alerts.next_page
alerts.current_page_number # => 5
next_page.current_page_number # => 6

Returns:



85
86
87
# File 'lib/esp/extensions/active_resource/paginated_collection.rb', line 85

def next_page
  next_page? ? updated_collection(next_page_params.merge(from: from)) : self
end

#next_page!PaginatedCollection, self

Updates the existing PaginatedCollection object with the last page of data when not on the last page.

Examples:

alerts.current_page_number # => 5
alerts.next_page!
alerts.current_page_number # => 6

Returns:



96
97
98
# File 'lib/esp/extensions/active_resource/paginated_collection.rb', line 96

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.

Returns:

  • (Boolean)


194
195
196
# File 'lib/esp/extensions/active_resource/paginated_collection.rb', line 194

def next_page?
  !next_page_number.nil?
end

#next_page_numberString?

The next page number of data.

Returns:

  • (String, nil)


173
174
175
# File 'lib/esp/extensions/active_resource/paginated_collection.rb', line 173

def next_page_number
  Hash(next_page_params).fetch('page', {}).fetch('number', nil)
end

#page(page_number = nil) ⇒ PaginatedCollection, self

Returns the page_number page of data.

Returns self when page_number == #current_page_number

Examples:

alerts.current_page_number # => 5
page = alerts.page(2)
alerts.current_page_number # => 5
page.current_page_number # => 2

Parameters:

  • page_number (Integer) (defaults to: nil)

    The page number of the data wanted. Must be between 1 and #last_page_number.

Returns:

Raises:

  • (ArgumentError)

    if no page number or an out-of-bounds page number is supplied.



137
138
139
140
141
142
# File 'lib/esp/extensions/active_resource/paginated_collection.rb', line 137

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 ? updated_collection(from: from, page: { number: page_number, size: (next_page_params || previous_page_params)['page']['size'] }) : self
end

#page!(page_number) ⇒ PaginatedCollection, self

Returns a new PaginatedCollection with the page_number page of data when not already on page page_number.

Examples:

alerts.current_page_number # => 5
alerts.page!(2)
alerts.current_page_number # => 2

Parameters:

  • page_number (Integer)

    The page number of the data wanted. Must be between 1 and #last_page_number.

Returns:



152
153
154
# File 'lib/esp/extensions/active_resource/paginated_collection.rb', line 152

def page!(page_number)
  page(page_number).tap { |page| update_self(page) }
end

#previous_pagePaginatedCollection, self

Returns the previous page of results.

Returns self (and no API call is made) when already on the first page.

Examples:

alerts.current_page_number # => 5
previous_page = alerts.previous_page
alerts.current_page_number # => 5
previous_page.current_page_number # => 4

Returns:



60
61
62
# File 'lib/esp/extensions/active_resource/paginated_collection.rb', line 60

def previous_page
  previous_page? ? updated_collection(previous_page_params.merge(from: from)) : self
end

#previous_page!PaginatedCollection, self

Updates the existing PaginatedCollection object with the previous page of data when not on the first page.

Examples:

alerts.current_page_number # => 5
alerts.previous_page!
alerts.current_page_number # => 4

Returns:



71
72
73
# File 'lib/esp/extensions/active_resource/paginated_collection.rb', line 71

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.

Returns:

  • (Boolean)


187
188
189
# File 'lib/esp/extensions/active_resource/paginated_collection.rb', line 187

def previous_page?
  !previous_page_number.nil?
end

#previous_page_numberString?

The previous page number of data.

Returns:

  • (String, nil)


166
167
168
# File 'lib/esp/extensions/active_resource/paginated_collection.rb', line 166

def previous_page_number
  Hash(previous_page_params).fetch('page', {}).fetch('number', nil)
end