Class: WordpressClient::PaginatedCollection
- Inherits:
-
Array
- Object
- Array
- WordpressClient::PaginatedCollection
- Defined in:
- lib/wordpress_client/paginated_collection.rb
Overview
This class has the full Array
interface by using DelegateClass(Array). Methods do not show up in the documentation unless when manually documented.
Represents a paginated list of resources.
Instance Attribute Summary collapse
-
#current_page ⇒ Fixnum
readonly
The current page number, where
1
is the first page. -
#per_page ⇒ Fixnum
readonly
The current page size setting, for example
30
. -
#total ⇒ Fixnum
(also: #total_entries)
readonly
The total hits in the full collection.
will_paginate protocol collapse
-
#next_page ⇒ Fixnum?
The next page number or
nil
if on last page. -
#offset ⇒ Object
Returns the offset of the current page.
-
#out_of_bounds? ⇒ Boolean
If the current page is out of bounds, e.g.
-
#previous_page ⇒ Fixnum?
The previous page number or
nil
if on first page. - #total_pages ⇒ Fixnum
Instance Method Summary collapse
-
#initialize(entries, total:, current_page:, per_page:) ⇒ PaginatedCollection
constructor
Create a new collection using the passed array
entries
. -
#size ⇒ Fixnum
The number of records actually in this “page”.
Constructor Details
#initialize(entries, total:, current_page:, per_page:) ⇒ PaginatedCollection
Create a new collection using the passed array entries
.
29 30 31 32 33 34 |
# 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 |
Instance Attribute Details
#current_page ⇒ Fixnum (readonly)
Returns the current page number, where 1
is the first page.
14 15 16 |
# File 'lib/wordpress_client/paginated_collection.rb', line 14 def current_page @current_page end |
#per_page ⇒ Fixnum (readonly)
Returns the current page size setting, for example 30
.
14 15 16 |
# File 'lib/wordpress_client/paginated_collection.rb', line 14 def per_page @per_page end |
#total ⇒ Fixnum (readonly) Also known as: total_entries
Returns the total hits in the full collection.
14 15 16 |
# File 'lib/wordpress_client/paginated_collection.rb', line 14 def total @total end |
Instance Method Details
#next_page ⇒ Fixnum?
This method is used by will_paginate
. By implementing this interface, you can use a WordpressClient::PaginatedCollection in place of a WillPaginate::Collection
to render pagination details.
Returns the next page number or nil
if on last page.
57 58 59 60 61 |
# File 'lib/wordpress_client/paginated_collection.rb', line 57 def next_page if current_page < total_pages current_page + 1 end end |
#offset ⇒ Object
This method is used by will_paginate
. By implementing this interface, you can use a WordpressClient::PaginatedCollection in place of a WillPaginate::Collection
to render pagination details.
will_paginate < 3.0 has this method, but it’s no longer present in newer will_paginate.
Returns the offset of the current page.
100 101 102 103 104 105 106 |
# 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? ⇒ Boolean
This method is used by will_paginate
. By implementing this interface, you can use a WordpressClient::PaginatedCollection in place of a WillPaginate::Collection
to render pagination details.
Returns if the current page is out of bounds, e.g. less than 1 or higher than #total_pages.
78 79 80 |
# File 'lib/wordpress_client/paginated_collection.rb', line 78 def out_of_bounds? current_page < 1 || current_page > total_pages end |
#previous_page ⇒ Fixnum?
This method is used by will_paginate
. By implementing this interface, you can use a WordpressClient::PaginatedCollection in place of a WillPaginate::Collection
to render pagination details.
Returns the previous page number or nil
if on first page.
67 68 69 70 71 |
# File 'lib/wordpress_client/paginated_collection.rb', line 67 def previous_page if current_page > 1 current_page - 1 end end |
#size ⇒ Fixnum
Returns the number of records actually in this “page”.
14 |
# File 'lib/wordpress_client/paginated_collection.rb', line 14 attr_reader :total, :current_page, :per_page |
#total_pages ⇒ Fixnum
This method is used by will_paginate
. By implementing this interface, you can use a WordpressClient::PaginatedCollection in place of a WillPaginate::Collection
to render pagination details.
Returns the total number of pages that can show the #total entries with #per_page records per page. 0
if no entries.
45 46 47 48 49 50 51 |
# 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 |