Class: GearedPagination::Portion

Inherits:
Object
  • Object
show all
Defined in:
lib/geared_pagination/portion.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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_numberObject (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

#ratiosObject (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

#from(scope) ⇒ Object



11
12
13
# File 'lib/geared_pagination/portion.rb', line 11

def from(scope)
  scope.limit(limit).offset(offset)
end

#limitObject



15
16
17
# File 'lib/geared_pagination/portion.rb', line 15

def limit
  ratios[page_number]
end

#offsetObject



19
20
21
# File 'lib/geared_pagination/portion.rb', line 19

def offset
  (page_number - 1).times.sum { |index| ratios[index + 1] }
end