Class: SimplyPaginate::Page

Inherits:
Object
  • Object
show all
Defined in:
lib/simply_paginate/page.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(page, collection, size = Paginator.per_page) ⇒ Page

Returns a new instance of Page.



5
6
7
8
9
10
11
12
13
14
# File 'lib/simply_paginate/page.rb', line 5

def initialize(page, collection, size = Paginator.per_page)
  @index = page
  @collection = collection
  @size = size

  @move_page = lambda do |number|
    new_index = index + number
    Page.new(new_index, collection, size) unless (new_index <= 0) || (new_index > (collection.count.to_f / size.to_f).ceil)
  end
end

Instance Attribute Details

#collectionObject (readonly)

Returns the value of attribute collection.



3
4
5
# File 'lib/simply_paginate/page.rb', line 3

def collection
  @collection
end

#indexObject (readonly)

Returns the value of attribute index.



3
4
5
# File 'lib/simply_paginate/page.rb', line 3

def index
  @index
end

#sizeObject (readonly)

Returns the value of attribute size.



3
4
5
# File 'lib/simply_paginate/page.rb', line 3

def size
  @size
end

Instance Method Details

#==(other) ⇒ Object



31
32
33
34
35
# File 'lib/simply_paginate/page.rb', line 31

def ==(other)
  return false unless other.respond_to?(:index) && other.respond_to?(:elements)

  (index == other.index) && (elements == other.elements)
end

#elementsObject



24
25
26
27
28
29
# File 'lib/simply_paginate/page.rb', line 24

def elements
  first = (@index - 1) * @size
  last = first + size - 1

  collection[first..last]
end

#nextObject



16
17
18
# File 'lib/simply_paginate/page.rb', line 16

def next
  @move_page.call(1)
end

#previousObject



20
21
22
# File 'lib/simply_paginate/page.rb', line 20

def previous
  @move_page.call(-1)
end