Class: Plucky::Pagination::Paginator

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Paginator.



7
8
9
10
11
# File 'lib/plucky/pagination/paginator.rb', line 7

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/plucky/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/plucky/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/plucky/pagination/paginator.rb', line 5

def total_entries
  @total_entries
end

Instance Method Details

#next_pageObject



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

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

#out_of_bounds?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/plucky/pagination/paginator.rb', line 17

def out_of_bounds?
  current_page > total_pages
end

#previous_pageObject



21
22
23
# File 'lib/plucky/pagination/paginator.rb', line 21

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

#skipObject Also known as: offset



29
30
31
# File 'lib/plucky/pagination/paginator.rb', line 29

def skip
  (current_page - 1) * per_page
end

#total_pagesObject



13
14
15
# File 'lib/plucky/pagination/paginator.rb', line 13

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