Module: Wurk::Web::Paginator

Included in:
Extension::Action
Defined in:
lib/wurk/web/paginator.rb

Overview

page / page_items, the pagination extensions call from their routes and that Sidekiq::Paginator names. The dashboard paginates through its JSON API, so nothing but extensions reaches this.

Instance Method Summary collapse

Instance Method Details

#page(key, pageidx = 1, page_count = 25, opts = nil) ⇒ Array(Integer, Integer, Array)

Returns the page served, the collection's size and the items. A sorted set yields [member, score] pairs; a list yields members. A missing key is an empty first page.

Returns:

  • (Array(Integer, Integer, Array)) —

    the page served, the collection's size and the items. A sorted set yields [member, score] pairs; a list yields members. A missing key is an empty first page.



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/wurk/web/paginator.rb', line 12

def page(key, pageidx = 1, page_count = 25, opts = nil)
  current = [pageidx.to_i, 1].max
  first = (current - 1) * page_count
  last = first + page_count - 1
  ::Wurk.redis do |conn|
    case conn.call('TYPE', key)
    when 'zset' then [current, conn.call('ZCARD', key), sorted_page(conn, key, first, last, opts)]
    when 'list' then [current, conn.call('LLEN', key), list_page(conn, key, first, last, opts)]
    when 'none' then [1, 0, []]
    else raise ::ArgumentError, "can't paginate #{key}: it is neither a sorted set nor a list"
    end
  end
end

#page_items(items, pageidx = 1, page_count = 25) ⇒ Object

Paginates an in-memory collection.



27
28
29
30
31
# File 'lib/wurk/web/paginator.rb', line 27

def page_items(items, pageidx = 1, page_count = 25)
  current = [pageidx.to_i, 1].max
  rows = items.to_a
  [current, rows.size, rows.slice((current - 1) * page_count, page_count) || []]
end