Class: Paginate::Collection

Inherits:
Array show all
Defined in:
lib/doozer/plugins/paginate/lib/paginate/collection.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Array

#extract_options!

Constructor Details

#initialize(page, per_page, total = nil) ⇒ Collection

Returns a new instance of Collection.

Raises:



12
13
14
15
16
17
18
19
# File 'lib/doozer/plugins/paginate/lib/paginate/collection.rb', line 12

def initialize(page, per_page, total = nil)
  @current_page = page.to_i
  raise InvalidPage.new(page, @current_page) if @current_page < 1
  @per_page = per_page.to_i
  raise ArgumentError, "`per_page` setting cannot be less than 1 (#{@per_page} given)" if @per_page < 1
  
  self.total_entries = total if total
end

Instance Attribute Details

#current_pageObject (readonly)

Returns the value of attribute current_page.



10
11
12
# File 'lib/doozer/plugins/paginate/lib/paginate/collection.rb', line 10

def current_page
  @current_page
end

#per_pageObject (readonly)

Returns the value of attribute per_page.



10
11
12
# File 'lib/doozer/plugins/paginate/lib/paginate/collection.rb', line 10

def per_page
  @per_page
end

#total_entriesObject

Returns the value of attribute total_entries.



10
11
12
# File 'lib/doozer/plugins/paginate/lib/paginate/collection.rb', line 10

def total_entries
  @total_entries
end

#total_pagesObject (readonly)

Returns the value of attribute total_pages.



10
11
12
# File 'lib/doozer/plugins/paginate/lib/paginate/collection.rb', line 10

def total_pages
  @total_pages
end

Class Method Details

.create(page, per_page, total = nil) {|pager| ... } ⇒ Object

Yields:

  • (pager)


21
22
23
24
25
# File 'lib/doozer/plugins/paginate/lib/paginate/collection.rb', line 21

def self.create(page, per_page, total = nil)
  pager = new(page, per_page, total)
  yield pager
  pager
end

Instance Method Details

#next_pageObject



39
40
41
# File 'lib/doozer/plugins/paginate/lib/paginate/collection.rb', line 39

def next_page
  current_page < total_pages ? (current_page + 1) : nil
end

#offsetObject



31
32
33
# File 'lib/doozer/plugins/paginate/lib/paginate/collection.rb', line 31

def offset
  (current_page - 1) * per_page
end

#out_of_bounds?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/doozer/plugins/paginate/lib/paginate/collection.rb', line 27

def out_of_bounds?
  current_page > total_pages
end

#previous_pageObject



35
36
37
# File 'lib/doozer/plugins/paginate/lib/paginate/collection.rb', line 35

def previous_page
  current_page > 1 ? (current_page - 1) : nil
end

#replace(array) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/doozer/plugins/paginate/lib/paginate/collection.rb', line 48

def replace(array)
  result = super
  
  # The collection is shorter then page limit? Rejoice, because
  # then we know that we are on the last page!
  if total_entries.nil? and length < per_page and (current_page == 1 or length > 0)
    self.total_entries = offset + length
  end

  result
end