class WordpressClient::PaginatedCollection

Represents a paginated list of resources.

@note This class has the full Array interface by using

+DelegateClass(Array)+. Methods do not show up in the documentation
unless when manually documented.

Attributes

current_page[R]

@!method size

@return [Fixnum] the number of records actually in this "page".
@see Array#size
per_page[R]

@!method size

@return [Fixnum] the number of records actually in this "page".
@see Array#size
total[R]

@!method size

@return [Fixnum] the number of records actually in this "page".
@see Array#size
total_entries[R]

@!method size

@return [Fixnum] the number of records actually in this "page".
@see Array#size

Public Class Methods

new(entries, total:, current_page:, per_page:) click to toggle source

Create a new collection using the passed array entries.

@param entries [Array] the original “page” array

Calls superclass method
# File lib/wordpress_client/paginated_collection.rb, line 29
def initialize(entries, total:, current_page:, per_page:)
  super(entries)
  @total = total
  @current_page = current_page
  @per_page = per_page
end

Public Instance Methods

next_page() click to toggle source

@note This method is used by will_paginate. By implementing this

interface, you can use a {PaginatedCollection} in place of a
+WillPaginate::Collection+ to render pagination details.

@return [Fixnum, nil] the next page number or nil if on last page.

# File lib/wordpress_client/paginated_collection.rb, line 57
def next_page
  if current_page < total_pages
    current_page + 1
  end
end
offset() click to toggle source

@note This method is used by will_paginate. By implementing this

interface, you can use a {PaginatedCollection} in place of a
+WillPaginate::Collection+ to render pagination details.

@note will_paginate < 3.0 has this method, but it's no longer present in

newer will_paginate.

Returns the offset of the current page.

@example First page offset

collection.per_page # => 20
collection.current_page # => 1
collection.offset #=> 0

@example Later offset

collection.per_page # => 20
collection.current_page # => 3
collection.offset #=> 40
# File lib/wordpress_client/paginated_collection.rb, line 100
def offset
  if current_page > 0
    (current_page - 1) * per_page
  else
    0
  end
end
out_of_bounds?() click to toggle source

@note This method is used by will_paginate. By implementing this

interface, you can use a {PaginatedCollection} in place of a
+WillPaginate::Collection+ to render pagination details.

@return [Boolean] if the current page is out of bounds, e.g. less than 1

or higher than {#total_pages}.
# File lib/wordpress_client/paginated_collection.rb, line 78
def out_of_bounds?
  current_page < 1 || current_page > total_pages
end
previous_page() click to toggle source

@note This method is used by will_paginate. By implementing this

interface, you can use a {PaginatedCollection} in place of a
+WillPaginate::Collection+ to render pagination details.

@return [Fixnum, nil] the previous page number or nil if on first page.

# File lib/wordpress_client/paginated_collection.rb, line 67
def previous_page
  if current_page > 1
    current_page - 1
  end
end
total_pages() click to toggle source

@note This method is used by will_paginate. By implementing this

interface, you can use a {PaginatedCollection} in place of a
+WillPaginate::Collection+ to render pagination details.

@return [Fixnum] the total number of pages that can show the {#total}

entries with {#per_page} records per page. +0+ if no entries.
# File lib/wordpress_client/paginated_collection.rb, line 45
def total_pages
  if total.zero? || per_page.zero?
    0
  else
    (total / per_page.to_f).ceil
  end
end