Class: WordpressClient::PaginatedCollection

Inherits:
Array
  • Object
show all
Defined in:
lib/wordpress_client/paginated_collection.rb

Overview

Note:

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

will_paginate protocol collapse

Instance Method Summary collapse

Constructor Details

#initialize(entries, total:, current_page:, per_page:) ⇒ PaginatedCollection

Create a new collection using the passed array entries.

Parameters:

  • entries (Array)

    the original “page” array



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_pageFixnum (readonly)

Returns the current page number, where 1 is the first page.

Returns:

  • (Fixnum)

    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_pageFixnum (readonly)

Returns the current page size setting, for example 30.

Returns:

  • (Fixnum)

    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

#totalFixnum (readonly) Also known as: total_entries

Returns the total hits in the full collection.

Returns:

  • (Fixnum)

    the total hits in the full collection.

See Also:



14
15
16
# File 'lib/wordpress_client/paginated_collection.rb', line 14

def total
  @total
end

Instance Method Details

#next_pageFixnum?

Note:

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.

Returns:

  • (Fixnum, nil)

    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

#offsetObject

Note:

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.

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.

Examples:

First page offset

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

Later offset

collection.per_page # => 20
collection.current_page # => 3
collection.offset #=> 40


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

Note:

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.

Returns:

  • (Boolean)

    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_pageFixnum?

Note:

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.

Returns:

  • (Fixnum, nil)

    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

#sizeFixnum

Returns the number of records actually in this “page”.

Returns:

  • (Fixnum)

    the number of records actually in this “page”.

See Also:

  • Array#size


14
# File 'lib/wordpress_client/paginated_collection.rb', line 14

attr_reader :total, :current_page, :per_page

#total_pagesFixnum

Note:

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.

Returns:

  • (Fixnum)

    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