module Sequel::KeysetPagination

Public Instance Methods

seek(before: nil, after: nil) click to toggle source
# 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 = []

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

  where(Sequel.&(*conditions))
end