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.

Raises:

  • (ArgumentError)


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

def initialize(collection, page_number: 1, per_page: 22, padding: 0)
  raise ArgumentError,
        'you can paginate only Fish0::Repository' unless collection.is_a?(Fish0::Repository)
  @collection = collection
  @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



43
44
45
# File 'lib/fish0/paginator.rb', line 43

def current_page
  @page
end

#last_page?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/fish0/paginator.rb', line 51

def last_page?
  current_page >= pages
end

#padding(value) ⇒ Object



32
33
34
35
36
37
# File 'lib/fish0/paginator.rb', line 32

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

#page(value = @page) ⇒ Object



19
20
21
22
23
# File 'lib/fish0/paginator.rb', line 19

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

#pagesObject



47
48
49
# File 'lib/fish0/paginator.rb', line 47

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

#per(value = @per) ⇒ Object



25
26
27
28
29
30
# File 'lib/fish0/paginator.rb', line 25

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

#totalObject



39
40
41
# File 'lib/fish0/paginator.rb', line 39

def total
  collection.count
end