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, ordered_by: nil, per_page: nil) ⇒ Recordset

Returns a new instance of Recordset.



10
11
12
13
14
# File 'lib/geared_pagination/recordset.rb', line 10

def initialize(records, ordered_by: nil, per_page: nil)
  @records = records
  @orders  = Order.wrap_many(ordered_by)
  @ratios  = Ratios.new(per_page)
end

Instance Attribute Details

#ordersObject (readonly)

Returns the value of attribute orders.



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

def orders
  @orders
end

#ratiosObject (readonly)

Returns the value of attribute ratios.



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

def ratios
  @ratios
end

#recordsObject (readonly)

Returns the value of attribute records.



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

def records
  @records
end

Instance Method Details

#page(param) ⇒ Object



16
17
18
# File 'lib/geared_pagination/recordset.rb', line 16

def page(param)
  Page.new portion_for(param), from: self
end

#page_countObject



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/geared_pagination/recordset.rb', line 20

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

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

    count > 0 ? count : 1
  end
end

#records_countObject



34
35
36
# File 'lib/geared_pagination/recordset.rb', line 34

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