Class: Karafka::Web::Ui::Lib::PaginateArray

Inherits:
Object
  • Object
show all
Defined in:
lib/karafka/web/ui/lib/paginate_array.rb

Overview

A simple wrapper for paginating array related data structures

Instance Method Summary collapse

Instance Method Details

#call(array, current_page) ⇒ Array<Array, <Integer, nil>>

Returns Array with two elements: first is the array with data of the given page and second is the next page number of nil in case there is no next page (end of data).

Parameters:

  • array (Array)

    array we want to paginate

  • current_page (Integer)

    page we want to be on

Returns:

  • (Array<Array, <Integer, nil>>)

    Array with two elements: first is the array with data of the given page and second is the next page number of nil in case there is no next page (end of data)



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/karafka/web/ui/lib/paginate_array.rb', line 14

def call(array, current_page)
  slices = array.each_slice(per_page).to_a

  current_data = slices[current_page - 1] || []

  if slices.count >= current_page - 1 && current_data.size >= per_page
    next_page = current_page + 1
  else
    next_page = nil
  end

  [current_data, next_page]
end