class SimplyPaginate::Paginator

Attributes

collection[R]

Public Class Methods

new(collection, per_page = self.class.per_page) click to toggle source
# File lib/simply_paginate/paginator.rb, line 17
def initialize(collection, per_page = self.class.per_page)
  @collection = collection
  @per_page = per_page
end
per_page() click to toggle source
# File lib/simply_paginate/paginator.rb, line 13
def self.per_page
  @@per_page
end
per_page=(amount_per_page) click to toggle source
# File lib/simply_paginate/paginator.rb, line 7
def self.per_page=(amount_per_page)
  raise NotInRangeError.new("Amount per page should be greater than 0") unless amount_per_page > 0

  @@per_page = amount_per_page
end

Public Instance Methods

[](index) click to toggle source
# File lib/simply_paginate/paginator.rb, line 36
def [](index)
  Page.new(index, @collection, @per_page) unless collection.empty? || index <= 0 || index > total_pages
end
current() click to toggle source
# File lib/simply_paginate/paginator.rb, line 55
def current
  @current
end
each() { |current| ... } click to toggle source
# File lib/simply_paginate/paginator.rb, line 59
def each
  start

  while next? do
    yield current

    next!
  end
end
first() click to toggle source

Basic Operations

# File lib/simply_paginate/paginator.rb, line 24
def first
  self[FIRST_PAGE_INDEX]
end
last() click to toggle source
# File lib/simply_paginate/paginator.rb, line 28
def last
  self[total_pages]
end
next!() click to toggle source

Iteration

# File lib/simply_paginate/paginator.rb, line 42
def next!
  raise NoMethodError.new("You need to start before iterating") unless current
  @current = current.next
end
next?() click to toggle source
# File lib/simply_paginate/paginator.rb, line 47
def next?
  current != nil
end
start() click to toggle source
# File lib/simply_paginate/paginator.rb, line 51
def start
  @current = first
end
total_pages() click to toggle source
# File lib/simply_paginate/paginator.rb, line 32
def total_pages
  (@collection.count.to_f / @per_page.to_f).ceil
end