Class: Kiva::PagedArray
- Inherits:
-
Array
- Object
- Array
- Kiva::PagedArray
- Defined in:
- lib/ruby-kiva/paged_array.rb
Overview
Adds pagination information to array Pagination attributes/methods have the same names as the wonderful will_paginate
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_entries ⇒ Object
readonly
Returns the value of attribute total_entries.
-
#total_pages ⇒ Object
readonly
Returns the value of attribute total_pages.
Instance Method Summary collapse
- #from_entry ⇒ Object
-
#initialize(data, options = {}) ⇒ PagedArray
constructor
A new instance of PagedArray.
- #inspect ⇒ Object
- #next_page ⇒ Object
- #next_page? ⇒ Boolean
- #offset ⇒ Object
- #out_of_bounds? ⇒ Boolean
- #previous_page ⇒ Object
- #previous_page? ⇒ Boolean
- #to_entry ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(data, options = {}) ⇒ PagedArray
Returns a new instance of PagedArray.
7 8 9 10 11 12 13 |
# File 'lib/ruby-kiva/paged_array.rb', line 7 def initialize(data, = {}) super data @current_page = ['page'] || 1 @per_page = ['page_size'] || data.size @total_pages = ['pages'] || 1 @total_entries = ['total'] || data.size end |
Instance Attribute Details
#current_page ⇒ Object (readonly)
Returns the value of attribute current_page.
5 6 7 |
# File 'lib/ruby-kiva/paged_array.rb', line 5 def current_page @current_page end |
#per_page ⇒ Object (readonly)
Returns the value of attribute per_page.
5 6 7 |
# File 'lib/ruby-kiva/paged_array.rb', line 5 def per_page @per_page end |
#total_entries ⇒ Object (readonly)
Returns the value of attribute total_entries.
5 6 7 |
# File 'lib/ruby-kiva/paged_array.rb', line 5 def total_entries @total_entries end |
#total_pages ⇒ Object (readonly)
Returns the value of attribute total_pages.
5 6 7 |
# File 'lib/ruby-kiva/paged_array.rb', line 5 def total_pages @total_pages end |
Instance Method Details
#from_entry ⇒ Object
35 36 37 |
# File 'lib/ruby-kiva/paged_array.rb', line 35 def from_entry self.offset + 1 unless self.out_of_bounds? end |
#inspect ⇒ Object
47 48 49 |
# File 'lib/ruby-kiva/paged_array.rb', line 47 def inspect "#{pagination_info}\n#{super}" end |
#next_page ⇒ Object
23 24 25 |
# File 'lib/ruby-kiva/paged_array.rb', line 23 def next_page self.current_page + 1 if next_page? end |
#next_page? ⇒ Boolean
19 20 21 |
# File 'lib/ruby-kiva/paged_array.rb', line 19 def next_page?() self.current_page < self.total_pages end |
#offset ⇒ Object
15 16 17 |
# File 'lib/ruby-kiva/paged_array.rb', line 15 def offset (self.current_page - 1) * self.per_page end |
#out_of_bounds? ⇒ Boolean
39 40 41 |
# File 'lib/ruby-kiva/paged_array.rb', line 39 def out_of_bounds? self.current_page < 1 || self.current_page > self.total_pages end |
#previous_page ⇒ Object
27 28 29 |
# File 'lib/ruby-kiva/paged_array.rb', line 27 def previous_page self.current_page - 1 if previous_page? end |
#previous_page? ⇒ Boolean
31 32 33 |
# File 'lib/ruby-kiva/paged_array.rb', line 31 def previous_page?() self.current_page > 1 end |
#to_entry ⇒ Object
43 44 45 |
# File 'lib/ruby-kiva/paged_array.rb', line 43 def to_entry [self.offset + self.per_page, self.total_entries].min unless self.out_of_bounds? end |
#to_s ⇒ Object
51 52 53 |
# File 'lib/ruby-kiva/paged_array.rb', line 51 def to_s self.inspect end |