Class: Nazrin::PaginatedArray
- Inherits:
-
Array
- Object
- Array
- Nazrin::PaginatedArray
- Defined in:
- lib/nazrin/paginated_array.rb
Instance Attribute Summary collapse
-
#current_page ⇒ Object
readonly
Returns the value of attribute current_page.
-
#per_page ⇒ Object
readonly
Returns the value of attribute per_page.
-
#total_count ⇒ Object
readonly
Returns the value of attribute total_count.
Instance Method Summary collapse
-
#first_page? ⇒ Boolean
first page of the collections?.
-
#initialize(collection, page, per_page, total_count) ⇒ PaginatedArray
constructor
A new instance of PaginatedArray.
-
#last_page? ⇒ Boolean
last page of the collections?.
-
#next_page ⇒ Object
next page number in the collections.
-
#out_of_bounds? ⇒ Boolean
out of bounds of the collections?.
-
#previous_page ⇒ Object
previous page number in the collections.
-
#total_pages ⇒ Object
total number of pages.
Constructor Details
#initialize(collection, page, per_page, total_count) ⇒ PaginatedArray
Returns a new instance of PaginatedArray.
5 6 7 8 9 10 |
# File 'lib/nazrin/paginated_array.rb', line 5 def initialize(collection, page, per_page, total_count) @current_page = page @per_page = per_page @total_count = total_count replace collection end |
Instance Attribute Details
#current_page ⇒ Object (readonly)
Returns the value of attribute current_page.
3 4 5 |
# File 'lib/nazrin/paginated_array.rb', line 3 def current_page @current_page end |
#per_page ⇒ Object (readonly)
Returns the value of attribute per_page.
3 4 5 |
# File 'lib/nazrin/paginated_array.rb', line 3 def per_page @per_page end |
#total_count ⇒ Object (readonly)
Returns the value of attribute total_count.
3 4 5 |
# File 'lib/nazrin/paginated_array.rb', line 3 def total_count @total_count end |
Instance Method Details
#first_page? ⇒ Boolean
first page of the collections?
13 14 15 |
# File 'lib/nazrin/paginated_array.rb', line 13 def first_page? current_page == 1 end |
#last_page? ⇒ Boolean
last page of the collections?
18 19 20 |
# File 'lib/nazrin/paginated_array.rb', line 18 def last_page? current_page >= total_pages end |
#next_page ⇒ Object
next page number in the collections
33 34 35 |
# File 'lib/nazrin/paginated_array.rb', line 33 def next_page current_page + 1 unless last_page? || out_of_bounds? end |
#out_of_bounds? ⇒ Boolean
out of bounds of the collections?
38 39 40 |
# File 'lib/nazrin/paginated_array.rb', line 38 def out_of_bounds? current_page > total_pages end |
#previous_page ⇒ Object
previous page number in the collections
28 29 30 |
# File 'lib/nazrin/paginated_array.rb', line 28 def previous_page current_page - 1 unless first_page? || out_of_bounds? end |
#total_pages ⇒ Object
total number of pages
23 24 25 |
# File 'lib/nazrin/paginated_array.rb', line 23 def total_pages (total_count.to_f / per_page).ceil end |