class ActiveResource::PaginatedCollection

Attributes

from[RW]

Internal variable used to construct queries. @return [Integer] @private

last_page_params[R]

Internal variable used to construct queries. @return [Hash] @private

next_page_params[R]

Internal variable used to construct queries. @return [Hash] @private

previous_page_params[R]

Internal variable used to construct queries. @return [Hash] @private

Public Class Methods

new(elements = []) click to toggle source

@private

Calls superclass method
# File lib/esp/extensions/active_resource/paginated_collection.rb, line 15
def initialize(elements = [])
  # 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

Public Instance Methods

current_page_number() click to toggle source

The current page number of data.

@return [String]

# 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_page() click to toggle source

Returns the first page of results.

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

@return [PaginatedCollection, self] @example

alerts.current_page_number # => 5
first_page = alerts.first_page
alerts.current_page_number # => 5
first_page.current_page_number # => 1
# 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!() click to toggle source

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

@return (see first_page) @example

alerts.current_page_number # => 5
alerts.first_page!
alerts.current_page_number # => 1
# File lib/esp/extensions/active_resource/paginated_collection.rb, line 46
def first_page!
  first_page.tap { |page| update_self(page) }
end
last_page() click to toggle source

Returns the last page of results.

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

@return [PaginatedCollection, self] @example

alerts.current_page_number # => 5
last_page = alerts.last_page
alerts.current_page_number # => 5
last_page.current_page_number # => 25
# 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!() click to toggle source

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

@return (see last_page) @example

alerts.current_page_number # => 5
alerts.last_page!
alerts.current_page_number # => 25
# File lib/esp/extensions/active_resource/paginated_collection.rb, line 121
def last_page!
  last_page.tap { |page| update_self(page) }
end
last_page?() click to toggle source

Returns whether or not the collection is on the last page.

@return [Boolean]

# File lib/esp/extensions/active_resource/paginated_collection.rb, line 201
def last_page?
  last_page_number.nil?
end
last_page_number() click to toggle source

The last page number of data.

@return [String, nil]

# 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_page() click to toggle source

Returns the next page of results.

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

@return [PaginatedCollection, self] @example

alerts.current_page_number # => 5
next_page = alerts.next_page
alerts.current_page_number # => 5
next_page.current_page_number # => 6
# 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!() click to toggle source

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

@return (see next_page) @example

alerts.current_page_number # => 5
alerts.next_page!
alerts.current_page_number # => 6
# File lib/esp/extensions/active_resource/paginated_collection.rb, line 96
def next_page!
  next_page.tap { |page| update_self(page) }
end
next_page?() click to toggle source

Returns whether or not there is a next page of data in the collection.

@return [Boolean]

# File lib/esp/extensions/active_resource/paginated_collection.rb, line 194
def next_page?
  !next_page_number.nil?
end
next_page_number() click to toggle source

The next page number of data.

@return [String, nil]

# 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) click to toggle source

Returns the page_number page of data.

Returns self when page_number == #current_page_number

@param page_number [Integer] The page number of the data wanted. Must be between 1 and #last_page_number. @return [PaginatedCollection, self] @raise [ArgumentError] if no page number or an out-of-bounds page number is supplied. @example

alerts.current_page_number # => 5
page = alerts.page(2)
alerts.current_page_number # => 5
page.current_page_number # => 2
# 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) click to toggle source

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

@param (see page) @return (see page) @example

alerts.current_page_number # => 5
alerts.page!(2)
alerts.current_page_number # => 2
# 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_page() click to toggle source

Returns the previous page of results.

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

@return [PaginatedCollection, self] @example

alerts.current_page_number # => 5
previous_page = alerts.previous_page
alerts.current_page_number # => 5
previous_page.current_page_number # => 4
# 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!() click to toggle source

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

@return (see previous_page) @example

alerts.current_page_number # => 5
alerts.previous_page!
alerts.current_page_number # => 4
# File lib/esp/extensions/active_resource/paginated_collection.rb, line 71
def previous_page!
  previous_page.tap { |page| update_self(page) }
end
previous_page?() click to toggle source

Returns whether or not there is a previous page of data in the collection.

@return [Boolean]

# File lib/esp/extensions/active_resource/paginated_collection.rb, line 187
def previous_page?
  !previous_page_number.nil?
end
previous_page_number() click to toggle source

The previous page number of data.

@return [String, nil]

# 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

Private Instance Methods

update_self(page) click to toggle source
# File lib/esp/extensions/active_resource/paginated_collection.rb, line 215
def update_self(page)
  @elements = page.elements
  @next_page_params = page.next_page_params
  @previous_page_params = page.previous_page_params
  @last_page_params = page.last_page_params
end
updated_collection(params) click to toggle source

Start a new collection.

@param params [Hash] @return [PaginatedCollection]

# File lib/esp/extensions/active_resource/paginated_collection.rb, line 211
def updated_collection(params)
  resource_class.where(original_params.merge(params))
end