Module: Sequel::KeysetPagination

Defined in:
lib/sequel/extensions/keyset_pagination.rb

Defined Under Namespace

Modules: Utils

Instance Method Summary collapse

Instance Method Details

#seek(before: nil, after: nil) ⇒ Object

Raises:

  • (ArgumentError)


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/sequel/extensions/keyset_pagination.rb', line 38

def seek(before: nil, after: nil)
  raise ArgumentError, "`before` or `after` is required" unless before || after

  if opts[:order].nil?
    raise StandardError, "cannot call #seek on a dataset with no order"
  end

  cursor_size = opts[:order].count

  if before
    before = [before] unless before.is_a? Array
    raise StandardError, "The `before` cursor has the wrong number of values. Expected #{cursor_size}, received #{before.count}." unless before.count == cursor_size
  end

  if after
    after = [after] unless after.is_a? Array
    raise StandardError, "The `after` cursor has the wrong number of values. Expected #{cursor_size}, received #{after.count}." unless after.count == cursor_size
  end

  columns = opts[:order].map { |o| Utils.qualify_order(o) }
  conditions = []

  if after
    conditions << Utils.cursor_conditions(columns, after)
  end

  if before
    conditions << Utils.cursor_conditions(columns, before, reverse: true)
  end

  where(Sequel.&(*conditions))
end