Class: Backframe::Response::Collection
- Inherits:
-
Object
- Object
- Backframe::Response::Collection
- Defined in:
- lib/backframe/response/collection.rb
Instance Method Summary collapse
- #current_page ⇒ Object
-
#initialize(collection, page, per_page) ⇒ Collection
constructor
A new instance of Collection.
- #limit ⇒ Object
- #offset ⇒ Object
- #records ⇒ Object
- #total_pages ⇒ Object
- #total_records ⇒ Object
Constructor Details
#initialize(collection, page, per_page) ⇒ Collection
9 10 11 12 13 |
# File 'lib/backframe/response/collection.rb', line 9 def initialize(collection, page, per_page) @collection = collection @page = page @per_page = per_page end |
Instance Method Details
#current_page ⇒ Object
23 24 25 |
# File 'lib/backframe/response/collection.rb', line 23 def current_page @page ? [total_pages, @page.to_i].min : 1 end |
#limit ⇒ Object
27 28 29 |
# File 'lib/backframe/response/collection.rb', line 27 def limit @page ? @per_page : total_records end |
#offset ⇒ Object
31 32 33 |
# File 'lib/backframe/response/collection.rb', line 31 def offset @page ? limit * (@page - 1) : 0 end |
#records ⇒ Object
35 36 37 |
# File 'lib/backframe/response/collection.rb', line 35 def records @page ? @collection.limit(limit).offset(offset) : @collection.all end |
#total_pages ⇒ Object
19 20 21 |
# File 'lib/backframe/response/collection.rb', line 19 def total_pages @page ? (total_records / @per_page.to_f).ceil : 1 end |
#total_records ⇒ Object
15 16 17 |
# File 'lib/backframe/response/collection.rb', line 15 def total_records @collection.count end |