File: paginator.rb

package info (click to toggle)
ruby-sidekiq 5.2.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 828 kB
  • sloc: ruby: 4,065; makefile: 24; sh: 6
file content (43 lines) | stat: -rw-r--r-- 1,119 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# frozen_string_literal: true
module Sidekiq
  module Paginator

    def page(key, pageidx=1, page_size=25, opts=nil)
      current_page = pageidx.to_i < 1 ? 1 : pageidx.to_i
      pageidx = current_page - 1
      total_size = 0
      items = []
      starting = pageidx * page_size
      ending = starting + page_size - 1

      Sidekiq.redis do |conn|
        type = conn.type(key)

        case type
        when 'zset'
          rev = opts && opts[:reverse]
          total_size, items = conn.multi do
            conn.zcard(key)
            if rev
              conn.zrevrange(key, starting, ending, :with_scores => true)
            else
              conn.zrange(key, starting, ending, :with_scores => true)
            end
          end
          [current_page, total_size, items]
        when 'list'
          total_size, items = conn.multi do
            conn.llen(key)
            conn.lrange(key, starting, ending)
          end
          [current_page, total_size, items]
        when 'none'
          [1, 0, []]
        else
          raise "can't page a #{type}"
        end
      end
    end

  end
end