Class: Noiseless::Pagination::PaginatedArray

Inherits:
Array
  • Object
show all
Defined in:
lib/noiseless/pagination.rb

Overview

Simple paginated array wrapper - no external dependencies

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(records, current_page:, per_page:, total_count:) ⇒ PaginatedArray

Returns a new instance of PaginatedArray.



12
13
14
15
16
17
# File 'lib/noiseless/pagination.rb', line 12

def initialize(records, current_page:, per_page:, total_count:)
  super(records)
  @current_page = current_page
  @per_page = per_page
  @total_count = total_count
end

Instance Attribute Details

#current_pageObject

Returns the value of attribute current_page.



10
11
12
# File 'lib/noiseless/pagination.rb', line 10

def current_page
  @current_page
end

#per_pageObject

Returns the value of attribute per_page.



10
11
12
# File 'lib/noiseless/pagination.rb', line 10

def per_page
  @per_page
end

#total_countObject

Returns the value of attribute total_count.



10
11
12
# File 'lib/noiseless/pagination.rb', line 10

def total_count
  @total_count
end

Instance Method Details

#first_page?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/noiseless/pagination.rb', line 33

def first_page?
  current_page == 1
end

#last_page?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/noiseless/pagination.rb', line 37

def last_page?
  current_page >= total_pages
end

#limit_valueObject



49
50
51
# File 'lib/noiseless/pagination.rb', line 49

def limit_value
  per_page
end

#next_pageObject



25
26
27
# File 'lib/noiseless/pagination.rb', line 25

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

#offset_valueObject



45
46
47
# File 'lib/noiseless/pagination.rb', line 45

def offset_value
  (current_page - 1) * per_page
end

#out_of_range?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/noiseless/pagination.rb', line 41

def out_of_range?
  current_page > total_pages
end

#pagination_metadataObject

JSON serialization for API responses



54
55
56
57
58
59
60
61
62
63
# File 'lib/noiseless/pagination.rb', line 54

def 
  {
    current_page: current_page,
    per_page: per_page,
    total_count: total_count,
    total_pages: total_pages,
    next_page: next_page,
    prev_page: prev_page
  }
end

#prev_pageObject



29
30
31
# File 'lib/noiseless/pagination.rb', line 29

def prev_page
  current_page > 1 ? current_page - 1 : nil
end

#total_pagesObject



19
20
21
22
23
# File 'lib/noiseless/pagination.rb', line 19

def total_pages
  return 1 if total_count.zero? || per_page.zero?

  (total_count.to_f / per_page).ceil
end