Class: GearedPagination::Recordset

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(records, per_page: nil) ⇒ Recordset

Returns a new instance of Recordset.



8
9
10
11
# File 'lib/geared_pagination/recordset.rb', line 8

def initialize(records, per_page: nil)
  @records = records
  @ratios  = GearedPagination::Ratios.new(per_page)
end

Instance Attribute Details

#ratiosObject (readonly)

Returns the value of attribute ratios.



6
7
8
# File 'lib/geared_pagination/recordset.rb', line 6

def ratios
  @ratios
end

#recordsObject (readonly)

Returns the value of attribute records.



6
7
8
# File 'lib/geared_pagination/recordset.rb', line 6

def records
  @records
end

Instance Method Details

#page(number) ⇒ Object



13
14
15
# File 'lib/geared_pagination/recordset.rb', line 13

def page(number)
  GearedPagination::Page.new(number, from: self)
end

#page_countObject



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/geared_pagination/recordset.rb', line 17

def page_count
  @page_count ||= begin
    count    = 0
    residual = records_count

    while residual > 0
      count   += 1
      residual = residual - ratios[count]
    end

    count
  end
end

#records_countObject



31
32
33
# File 'lib/geared_pagination/recordset.rb', line 31

def records_count
  @records_count ||= records.unscope(:limit).unscope(:offset).count
end