Class: GearedPagination::Portion
- Inherits:
-
Object
- Object
- GearedPagination::Portion
- Defined in:
- lib/geared_pagination/portion.rb
Instance Attribute Summary collapse
-
#page_number ⇒ Object
readonly
Returns the value of attribute page_number.
-
#ratios ⇒ Object
readonly
Returns the value of attribute ratios.
Instance Method Summary collapse
- #cache_key ⇒ Object
- #from(scope) ⇒ Object
-
#initialize(page_number: 1, per_page: GearedPagination::Ratios.new) ⇒ Portion
constructor
A new instance of Portion.
- #limit ⇒ Object
- #offset ⇒ Object
Constructor Details
#initialize(page_number: 1, per_page: GearedPagination::Ratios.new) ⇒ Portion
Returns a new instance of Portion.
7 8 9 |
# File 'lib/geared_pagination/portion.rb', line 7 def initialize(page_number: 1, per_page: GearedPagination::Ratios.new) @page_number, @ratios = page_number, per_page end |
Instance Attribute Details
#page_number ⇒ Object (readonly)
Returns the value of attribute page_number.
5 6 7 |
# File 'lib/geared_pagination/portion.rb', line 5 def page_number @page_number end |
#ratios ⇒ Object (readonly)
Returns the value of attribute ratios.
5 6 7 |
# File 'lib/geared_pagination/portion.rb', line 5 def ratios @ratios end |
Instance Method Details
#cache_key ⇒ Object
24 25 26 |
# File 'lib/geared_pagination/portion.rb', line 24 def cache_key "#{page_number}:#{ratios.cache_key}" end |
#from(scope) ⇒ Object
11 12 13 |
# File 'lib/geared_pagination/portion.rb', line 11 def from(scope) scope.limit(limit).offset(offset) end |
#limit ⇒ Object
15 16 17 |
# File 'lib/geared_pagination/portion.rb', line 15 def limit ratios[page_number] end |
#offset ⇒ Object
19 20 21 |
# File 'lib/geared_pagination/portion.rb', line 19 def offset (page_number - 1).times.sum { |index| ratios[index + 1] } end |