Class: SmartPaginate::PaginatingArray

Inherits:
Array
  • Object
show all
Defined in:
lib/smart_paginate/paginating_array.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#current_pageObject

Returns the value of attribute current_page.



5
6
7
# File 'lib/smart_paginate/paginating_array.rb', line 5

def current_page
  @current_page
end

#per_pageObject

Returns the value of attribute per_page.



5
6
7
# File 'lib/smart_paginate/paginating_array.rb', line 5

def per_page
  @per_page
end

#total_entriesObject

Returns the value of attribute total_entries.



5
6
7
# File 'lib/smart_paginate/paginating_array.rb', line 5

def total_entries
  @total_entries
end

Instance Method Details

#next_pageObject



31
32
33
# File 'lib/smart_paginate/paginating_array.rb', line 31

def next_page
  current_page + 1 if next_page?
end

#next_page?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/smart_paginate/paginating_array.rb', line 23

def next_page?
  (current_page * per_page) < total_entries
end

#paginate(options = {}) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/smart_paginate/paginating_array.rb', line 7

def paginate(options = {})
  page = SmartPaginate::Paginate.new(options.fetch(:page), options[:per_page])

  if page.offset <= length
    array = self.slice(page.offset, page.per_page)
  else
    # out of bounds, just create an empty array
    array = PaginatingArray.new
  end

  array.current_page = page.current_page
  array.per_page = page.per_page
  array.total_entries = length
  array
end

#previous_pageObject



35
36
37
# File 'lib/smart_paginate/paginating_array.rb', line 35

def previous_page
  current_page - 1 if previous_page?
end

#previous_page?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/smart_paginate/paginating_array.rb', line 27

def previous_page?
  current_page > 1
end

#total_pagesObject



39
40
41
# File 'lib/smart_paginate/paginating_array.rb', line 39

def total_pages
  (total_entries / per_page.to_f).ceil
end