Class: Nazrin::PaginatedArray

Inherits:
Array
  • Object
show all
Defined in:
lib/nazrin/paginated_array.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(collection, page, per_page, total_count) ⇒ PaginatedArray

Returns a new instance of PaginatedArray.



5
6
7
8
9
10
# File 'lib/nazrin/paginated_array.rb', line 5

def initialize(collection, page, per_page, total_count)
  @current_page = page
  @per_page = per_page
  @total_count = total_count
  replace collection
end

Instance Attribute Details

#current_pageObject (readonly)

Returns the value of attribute current_page.



3
4
5
# File 'lib/nazrin/paginated_array.rb', line 3

def current_page
  @current_page
end

#per_pageObject (readonly)

Returns the value of attribute per_page.



3
4
5
# File 'lib/nazrin/paginated_array.rb', line 3

def per_page
  @per_page
end

#total_countObject (readonly)

Returns the value of attribute total_count.



3
4
5
# File 'lib/nazrin/paginated_array.rb', line 3

def total_count
  @total_count
end

Instance Method Details

#first_page?Boolean

first page of the collections?

Returns:

  • (Boolean)


13
14
15
# File 'lib/nazrin/paginated_array.rb', line 13

def first_page?
  current_page == 1
end

#last_page?Boolean

last page of the collections?

Returns:

  • (Boolean)


18
19
20
# File 'lib/nazrin/paginated_array.rb', line 18

def last_page?
  current_page >= total_pages
end

#next_pageObject

next page number in the collections



33
34
35
# File 'lib/nazrin/paginated_array.rb', line 33

def next_page
  current_page + 1 unless last_page? || out_of_bounds?
end

#out_of_bounds?Boolean

out of bounds of the collections?

Returns:

  • (Boolean)


38
39
40
# File 'lib/nazrin/paginated_array.rb', line 38

def out_of_bounds?
  current_page > total_pages
end

#previous_pageObject

previous page number in the collections



28
29
30
# File 'lib/nazrin/paginated_array.rb', line 28

def previous_page
  current_page - 1 unless first_page? || out_of_bounds?
end

#total_pagesObject

total number of pages



23
24
25
# File 'lib/nazrin/paginated_array.rb', line 23

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