Class: Paginator

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(collection, options = {}) ⇒ Paginator

Returns a new instance of Paginator.



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

def initialize(collection, options={})
  @collection = collection
  @page = options[:page] ? options[:page].to_i : 1
  @size = options[:size] ? options[:size].to_i : 10
end

Instance Attribute Details

#pageObject (readonly)

Returns the value of attribute page.



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

def page
  @page
end

#sizeObject (readonly)

Returns the value of attribute size.



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

def size
  @size
end

Instance Method Details

#each(&block) ⇒ Object



13
14
15
16
# File 'lib/core_extended/paginator.rb', line 13

def each(&block)
  from = size * (page - 1)
  (@collection.slice(from, size) || []).each(&block)
end

#first?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/core_extended/paginator.rb', line 26

def first?
  page == 1
end

#last?Boolean

Returns:

  • (Boolean)


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

def last?
  page == pages
end

#pagesObject



22
23
24
# File 'lib/core_extended/paginator.rb', line 22

def pages
  (total / size.to_f).ceil
end

#totalObject



18
19
20
# File 'lib/core_extended/paginator.rb', line 18

def total
  @collection.count
end