class Pagy::Keyset::Sequel

Keyset adapter for sequel

def apply_select

Append the missing keyset keys if the set is restricted by select
def apply_select
  selected = @set.opts[:select]
  @set.select_append(*@keyset.keys.reject { |c| selected.include?(c) })
end

def extract_keyset

Extract the keyset from the set
def extract_keyset
  return {} unless @set.opts[:order]
  @set.opts[:order].each_with_object({}) do |item, keyset|
    case item
    when Symbol
      keyset[item] = :asc
    when ::Sequel::SQL::OrderedExpression
      keyset[item.expression] = item.descending ? :desc : :asc
    else
      raise TypeError, "#{item.class.inspect} is not a supported Sequel::SQL::OrderedExpression"
    end
  end
end

def filter_newest = @set.where(::Sequel.lit(filter_newest_query, **@latest))

Filter the newest records
def filter_newest = @set.where(::Sequel.lit(filter_newest_query, **@latest))

def keyset_attributes_from(record) = record.to_hash.slice(*@keyset.keys)

Get the keyset attributes from the record
def keyset_attributes_from(record) = record.to_hash.slice(*@keyset.keys)

def select? = !@set.opts[:select].nil?

Set with selected columns?
def select? = !@set.opts[:select].nil?

def typecast_latest(latest)

Typecast the latest attributes
def typecast_latest(latest)
  model = @set.opts[:model]
  model.unrestrict_primary_key if (restricted_pk = model.restrict_primary_key?)
  latest = model.new(latest).to_hash.slice(*latest.keys.map(&:to_sym))
  model.restrict_primary_key if restricted_pk
  latest
end