Class: ApiOnlyPagination::Paginate
- Inherits:
-
Object
- Object
- ApiOnlyPagination::Paginate
- Defined in:
- lib/api_only_pagination/paginate.rb
Instance Attribute Summary collapse
-
#count ⇒ Object
readonly
Returns the value of attribute count.
-
#page ⇒ Object
readonly
Returns the value of attribute page.
-
#per_page ⇒ Object
readonly
Returns the value of attribute per_page.
Instance Method Summary collapse
- #first_page? ⇒ Boolean
-
#initialize(page, per_page, count) ⇒ Paginate
constructor
A new instance of Paginate.
- #last_page? ⇒ Boolean
- #next_page ⇒ Object
- #next_page? ⇒ Boolean
- #offset ⇒ Object
- #previous_page ⇒ Object
- #previous_page? ⇒ Boolean
- #total_pages ⇒ Object
- #validate_params ⇒ Object
Constructor Details
#initialize(page, per_page, count) ⇒ Paginate
Returns a new instance of Paginate.
7 8 9 10 11 12 13 14 |
# File 'lib/api_only_pagination/paginate.rb', line 7 def initialize(page, per_page, count) @page = (page || ApiOnlyPagination.configuration.default_page).to_i @count = count @per_page = [(per_page.to_i || ApiOnlyPagination.configuration.default_page_size), ApiOnlyPagination.configuration.max_per_page].min validate_params end |
Instance Attribute Details
#count ⇒ Object (readonly)
Returns the value of attribute count.
5 6 7 |
# File 'lib/api_only_pagination/paginate.rb', line 5 def count @count end |
#page ⇒ Object (readonly)
Returns the value of attribute page.
5 6 7 |
# File 'lib/api_only_pagination/paginate.rb', line 5 def page @page end |
#per_page ⇒ Object (readonly)
Returns the value of attribute per_page.
5 6 7 |
# File 'lib/api_only_pagination/paginate.rb', line 5 def per_page @per_page end |
Instance Method Details
#first_page? ⇒ Boolean
47 48 49 |
# File 'lib/api_only_pagination/paginate.rb', line 47 def first_page? page == 1 end |
#last_page? ⇒ Boolean
43 44 45 |
# File 'lib/api_only_pagination/paginate.rb', line 43 def last_page? page == total_pages end |
#next_page ⇒ Object
27 28 29 |
# File 'lib/api_only_pagination/paginate.rb', line 27 def next_page page + 1 unless last_page? end |
#next_page? ⇒ Boolean
31 32 33 |
# File 'lib/api_only_pagination/paginate.rb', line 31 def next_page? page < total_pages end |
#offset ⇒ Object
21 22 23 24 25 |
# File 'lib/api_only_pagination/paginate.rb', line 21 def offset return 0 if page == 1 per_page * (page.to_i - 1) end |
#previous_page ⇒ Object
35 36 37 |
# File 'lib/api_only_pagination/paginate.rb', line 35 def previous_page page - 1 unless first_page? end |
#previous_page? ⇒ Boolean
39 40 41 |
# File 'lib/api_only_pagination/paginate.rb', line 39 def previous_page? page > 1 end |
#total_pages ⇒ Object
51 52 53 |
# File 'lib/api_only_pagination/paginate.rb', line 51 def total_pages (count / per_page.to_f).ceil end |
#validate_params ⇒ Object
16 17 18 19 |
# File 'lib/api_only_pagination/paginate.rb', line 16 def validate_params @page = @page.zero? ? ApiOnlyPagination.configuration.default_page : @page @per_page = @per_page.zero? ? ApiOnlyPagination.configuration.default_page_size : @per_page end |