Class: GearedPagination::PortionAtOffset
- Inherits:
-
Object
- Object
- GearedPagination::PortionAtOffset
- Defined in:
- lib/geared_pagination/portions/portion_at_offset.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: Ratios.new) ⇒ PortionAtOffset
constructor
A new instance of PortionAtOffset.
- #limit ⇒ Object
- #next_param ⇒ Object
- #offset ⇒ Object
Constructor Details
#initialize(page_number: 1, per_page: Ratios.new) ⇒ PortionAtOffset
Returns a new instance of PortionAtOffset.
7 8 9 |
# File 'lib/geared_pagination/portions/portion_at_offset.rb', line 7 def initialize(page_number: 1, per_page: 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/portions/portion_at_offset.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/portions/portion_at_offset.rb', line 5 def ratios @ratios end |
Instance Method Details
#cache_key ⇒ Object
31 32 33 |
# File 'lib/geared_pagination/portions/portion_at_offset.rb', line 31 def cache_key "#{page_number}:#{ratios.cache_key}" end |
#from(scope) ⇒ Object
11 12 13 |
# File 'lib/geared_pagination/portions/portion_at_offset.rb', line 11 def from(scope) scope.limit(limit).offset(offset) end |
#limit ⇒ Object
15 16 17 |
# File 'lib/geared_pagination/portions/portion_at_offset.rb', line 15 def limit ratios[page_number] end |
#next_param ⇒ Object
26 27 28 |
# File 'lib/geared_pagination/portions/portion_at_offset.rb', line 26 def next_param(*) page_number + 1 end |
#offset ⇒ Object
19 20 21 22 23 24 |
# File 'lib/geared_pagination/portions/portion_at_offset.rb', line 19 def offset variable = [(page_number - 1), ratios.size - 1].min.times.sum { |index| ratios[index + 1] } fixed = [page_number - ratios.size, 0].max * ratios.fixed variable + fixed end |