Class: Karafka::Web::Ui::Lib::Paginations::Paginators::Arrays

Inherits:
Base
  • Object
show all
Defined in:
lib/karafka/web/ui/lib/paginations/paginators/arrays.rb

Overview

A simple wrapper for paginating array related data structures We call this with plural (same with Sets) to avoid confusion with Ruby classes

Class Method Summary collapse

Methods inherited from Base

per_page

Class Method Details

.call(array, current_page) ⇒ Array<Array, Boolean>

Returns Array with two elements: first is the array with data of the given page and second is a boolean flag with info if the elements we got are from the last page.

Parameters:

  • array (Array)

    array we want to paginate

  • current_page (Integer)

    page we want to be on

Returns:

  • (Array<Array, Boolean>)

    Array with two elements: first is the array with data of the given page and second is a boolean flag with info if the elements we got are from the last page



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/karafka/web/ui/lib/paginations/paginators/arrays.rb', line 19

def call(array, current_page)
  slices = array.each_slice(per_page).to_a
  current_data = slices[current_page - 1] || []
  # We are on the last page when there is no slice after the current one. The
  # previous check keyed off "is the current page full?", which wrongly reported
  # a next page whenever the final page was exactly `per_page` long (total size
  # an exact multiple of per_page), surfacing a "Next" link to an empty page.
  last_page = current_page >= slices.length

  [current_data, last_page]
end