Class: Paginate::Collection
- Defined in:
- lib/doozer/plugins/paginate/lib/paginate/collection.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_entries ⇒ Object
Returns the value of attribute total_entries.
-
#total_pages ⇒ Object
readonly
Returns the value of attribute total_pages.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(page, per_page, total = nil) ⇒ Collection
constructor
A new instance of Collection.
- #next_page ⇒ Object
- #offset ⇒ Object
- #out_of_bounds? ⇒ Boolean
- #previous_page ⇒ Object
- #replace(array) ⇒ Object
Methods inherited from Array
Constructor Details
#initialize(page, per_page, total = nil) ⇒ Collection
Returns a new instance of Collection.
12 13 14 15 16 17 18 19 |
# File 'lib/doozer/plugins/paginate/lib/paginate/collection.rb', line 12 def initialize(page, per_page, total = nil) @current_page = page.to_i raise InvalidPage.new(page, @current_page) if @current_page < 1 @per_page = per_page.to_i raise ArgumentError, "`per_page` setting cannot be less than 1 (#{@per_page} given)" if @per_page < 1 self.total_entries = total if total end |
Instance Attribute Details
#current_page ⇒ Object (readonly)
Returns the value of attribute current_page.
10 11 12 |
# File 'lib/doozer/plugins/paginate/lib/paginate/collection.rb', line 10 def current_page @current_page end |
#per_page ⇒ Object (readonly)
Returns the value of attribute per_page.
10 11 12 |
# File 'lib/doozer/plugins/paginate/lib/paginate/collection.rb', line 10 def per_page @per_page end |
#total_entries ⇒ Object
Returns the value of attribute total_entries.
10 11 12 |
# File 'lib/doozer/plugins/paginate/lib/paginate/collection.rb', line 10 def total_entries @total_entries end |
#total_pages ⇒ Object (readonly)
Returns the value of attribute total_pages.
10 11 12 |
# File 'lib/doozer/plugins/paginate/lib/paginate/collection.rb', line 10 def total_pages @total_pages end |
Class Method Details
.create(page, per_page, total = nil) {|pager| ... } ⇒ Object
21 22 23 24 25 |
# File 'lib/doozer/plugins/paginate/lib/paginate/collection.rb', line 21 def self.create(page, per_page, total = nil) pager = new(page, per_page, total) yield pager pager end |
Instance Method Details
#next_page ⇒ Object
39 40 41 |
# File 'lib/doozer/plugins/paginate/lib/paginate/collection.rb', line 39 def next_page current_page < total_pages ? (current_page + 1) : nil end |
#offset ⇒ Object
31 32 33 |
# File 'lib/doozer/plugins/paginate/lib/paginate/collection.rb', line 31 def offset (current_page - 1) * per_page end |
#out_of_bounds? ⇒ Boolean
27 28 29 |
# File 'lib/doozer/plugins/paginate/lib/paginate/collection.rb', line 27 def out_of_bounds? current_page > total_pages end |
#previous_page ⇒ Object
35 36 37 |
# File 'lib/doozer/plugins/paginate/lib/paginate/collection.rb', line 35 def previous_page current_page > 1 ? (current_page - 1) : nil end |
#replace(array) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/doozer/plugins/paginate/lib/paginate/collection.rb', line 48 def replace(array) result = super # The collection is shorter then page limit? Rejoice, because # then we know that we are on the last page! if total_entries.nil? and length < per_page and (current_page == 1 or length > 0) self.total_entries = offset + length end result end |