Class: Noiseless::Pagination::PaginatedArray
- Inherits:
-
Array
- Object
- Array
- Noiseless::Pagination::PaginatedArray
- Defined in:
- lib/noiseless/pagination.rb
Overview
Simple paginated array wrapper - no external dependencies
Instance Attribute Summary collapse
-
#current_page ⇒ Object
Returns the value of attribute current_page.
-
#per_page ⇒ Object
Returns the value of attribute per_page.
-
#total_count ⇒ Object
Returns the value of attribute total_count.
Instance Method Summary collapse
- #first_page? ⇒ Boolean
-
#initialize(records, current_page:, per_page:, total_count:) ⇒ PaginatedArray
constructor
A new instance of PaginatedArray.
- #last_page? ⇒ Boolean
- #limit_value ⇒ Object
- #next_page ⇒ Object
- #offset_value ⇒ Object
- #out_of_range? ⇒ Boolean
-
#pagination_metadata ⇒ Object
JSON serialization for API responses.
- #prev_page ⇒ Object
- #total_pages ⇒ Object
Constructor Details
#initialize(records, current_page:, per_page:, total_count:) ⇒ PaginatedArray
Returns a new instance of PaginatedArray.
12 13 14 15 16 17 |
# File 'lib/noiseless/pagination.rb', line 12 def initialize(records, current_page:, per_page:, total_count:) super(records) @current_page = current_page @per_page = per_page @total_count = total_count end |
Instance Attribute Details
#current_page ⇒ Object
Returns the value of attribute current_page.
10 11 12 |
# File 'lib/noiseless/pagination.rb', line 10 def current_page @current_page end |
#per_page ⇒ Object
Returns the value of attribute per_page.
10 11 12 |
# File 'lib/noiseless/pagination.rb', line 10 def per_page @per_page end |
#total_count ⇒ Object
Returns the value of attribute total_count.
10 11 12 |
# File 'lib/noiseless/pagination.rb', line 10 def total_count @total_count end |
Instance Method Details
#first_page? ⇒ Boolean
33 34 35 |
# File 'lib/noiseless/pagination.rb', line 33 def first_page? current_page == 1 end |
#last_page? ⇒ Boolean
37 38 39 |
# File 'lib/noiseless/pagination.rb', line 37 def last_page? current_page >= total_pages end |
#limit_value ⇒ Object
49 50 51 |
# File 'lib/noiseless/pagination.rb', line 49 def limit_value per_page end |
#next_page ⇒ Object
25 26 27 |
# File 'lib/noiseless/pagination.rb', line 25 def next_page current_page < total_pages ? current_page + 1 : nil end |
#offset_value ⇒ Object
45 46 47 |
# File 'lib/noiseless/pagination.rb', line 45 def offset_value (current_page - 1) * per_page end |
#out_of_range? ⇒ Boolean
41 42 43 |
# File 'lib/noiseless/pagination.rb', line 41 def out_of_range? current_page > total_pages end |
#pagination_metadata ⇒ Object
JSON serialization for API responses
54 55 56 57 58 59 60 61 62 63 |
# File 'lib/noiseless/pagination.rb', line 54 def { current_page: current_page, per_page: per_page, total_count: total_count, total_pages: total_pages, next_page: next_page, prev_page: prev_page } end |
#prev_page ⇒ Object
29 30 31 |
# File 'lib/noiseless/pagination.rb', line 29 def prev_page current_page > 1 ? current_page - 1 : nil end |
#total_pages ⇒ Object
19 20 21 22 23 |
# File 'lib/noiseless/pagination.rb', line 19 def total_pages return 1 if total_count.zero? || per_page.zero? (total_count.to_f / per_page).ceil end |