Class: MarkMapper::Pagination::Paginator

Inherits:
Object
  • Object
show all
Defined in:
lib/mark_mapper/pagination/paginator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(total, page, per_page = nil) ⇒ Paginator

Public


8
9
10
11
12
# File 'lib/mark_mapper/pagination/paginator.rb', line 8

def initialize(total, page, per_page=nil)
  @total_entries = total.to_i
  @current_page  = [page.to_i, 1].max
  @per_page      = (per_page || 25).to_i
end

Instance Attribute Details

#current_pageObject (readonly)

Returns the value of attribute current_page


5
6
7
# File 'lib/mark_mapper/pagination/paginator.rb', line 5

def current_page
  @current_page
end

#per_pageObject (readonly) Also known as: limit

Returns the value of attribute per_page


5
6
7
# File 'lib/mark_mapper/pagination/paginator.rb', line 5

def per_page
  @per_page
end

#total_entriesObject (readonly)

Returns the value of attribute total_entries


5
6
7
# File 'lib/mark_mapper/pagination/paginator.rb', line 5

def total_entries
  @total_entries
end

Instance Method Details

#next_pageObject

Public


30
31
32
# File 'lib/mark_mapper/pagination/paginator.rb', line 30

def next_page
  @current_page < total_pages ? (@current_page + 1) : nil
end

#out_of_bounds?Boolean

Public


20
21
22
# File 'lib/mark_mapper/pagination/paginator.rb', line 20

def out_of_bounds?
  @current_page > total_pages
end

#previous_pageObject

Public


25
26
27
# File 'lib/mark_mapper/pagination/paginator.rb', line 25

def previous_page
  @current_page > 1 ? (@current_page - 1) : nil
end

#skipObject Also known as: offset

Public


35
36
37
# File 'lib/mark_mapper/pagination/paginator.rb', line 35

def skip
  (@current_page - 1) * @per_page
end

#total_pagesObject

Public


15
16
17
# File 'lib/mark_mapper/pagination/paginator.rb', line 15

def total_pages
  (@total_entries / @per_page.to_f).ceil
end