Class: Fish0::Paginator

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/fish0/paginator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(collection, page_number: 1, per_page: 22, padding: 0) ⇒ Paginator

Returns a new instance of Paginator.



8
9
10
11
12
13
14
15
# File 'lib/fish0/paginator.rb', line 8

def initialize(collection, page_number: 1, per_page: 22, padding: 0)
  @collection = collection || (raise ArgumentError)
  @per = per_page
  @page = page_number
  @padding = padding
  per(per_page)
  page(page_number)
end

Instance Attribute Details

#collectionObject (readonly)

Returns the value of attribute collection.



6
7
8
# File 'lib/fish0/paginator.rb', line 6

def collection
  @collection
end

Instance Method Details

#current_pageObject



41
42
43
# File 'lib/fish0/paginator.rb', line 41

def current_page
  @page
end

#last_page?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/fish0/paginator.rb', line 49

def last_page?
  current_page >= pages
end

#padding(value) ⇒ Object



30
31
32
33
34
35
# File 'lib/fish0/paginator.rb', line 30

def padding(value)
  @padding = value
  page
  skip
  self
end

#page(value = @page) ⇒ Object



17
18
19
20
21
# File 'lib/fish0/paginator.rb', line 17

def page(value = @page)
  @page = value
  skip((@page - 1) * @per + @padding)
  self
end

#pagesObject



45
46
47
# File 'lib/fish0/paginator.rb', line 45

def pages
  (total.to_f / @per).ceil
end

#per(value = @per) ⇒ Object



23
24
25
26
27
28
# File 'lib/fish0/paginator.rb', line 23

def per(value = @per)
  @per = value
  skip((@page - 1) * @per + @padding)
  limit(@per)
  self
end

#totalObject



37
38
39
# File 'lib/fish0/paginator.rb', line 37

def total
  collection.count
end