Class: Pagy::Keyset

Inherits:
Pagy
  • Object
show all
Defined in:
lib/pagy/classes/keyset/keyset.rb,
lib/pagy/classes/keyset/keynav.rb,
lib/pagy/classes/keyset/adapters/sequel.rb,
lib/pagy/classes/keyset/adapters/active_record.rb

Overview

Implement wicked-fast keyset pagination for big data

Direct Known Subclasses

ActiveRecord, Keynav, Sequel

Defined Under Namespace

Modules: Adapters Classes: ActiveRecord, Keynav, Sequel, TypeError

Constant Summary

Constants inherited from Pagy

A_TAG, DEFAULT, DEFAULT_DATA_KEYS, DEFAULT_HEADERS_MAP, LABEL_TOKEN, LIMIT_TOKEN, Method, PAGE_TOKEN, ROOT, SERIES_SLOTS, VERSION

Instance Attribute Summary

Attributes inherited from Pagy

#in, #limit, #options, #page

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Pagy

#data_hash, #headers_hash, #info_tag, #input_nav_js, #limit_tag_js, #next_tag, options, #page_url, #previous_tag, #series_nav, #series_nav_js, #urls_hash

Methods included from Configurable

#dev_tools, #sync_javascript, #translate_with_the_slower_i18n_gem!

Constructor Details

#initialize(set) ⇒ Keyset

Returns a new instance of Keyset.

Raises:



55
56
57
58
59
60
61
62
63
64
# File 'lib/pagy/classes/keyset/keyset.rb', line 55

def initialize(set, **)
  assign_options(**)
  assign_and_check(limit: 1)
  @set    = set
  @keyset = @options[:keyset] || extract_keyset
  raise InternalError, 'the set must be ordered' if @keyset.empty?

  assign_page
  self.next
end

Class Method Details

.mix_in_adapter(orm_name) ⇒ Object

Helper to lazy-include the adapter module



50
51
52
53
# File 'lib/pagy/classes/keyset/keyset.rb', line 50

def self.mix_in_adapter(orm_name)
  adapter_module = Pagy::Keyset::Adapters.const_get(orm_name)
  include(adapter_module) unless self < adapter_module
end

.new(set) ⇒ Object

Factory method: detects the set type, configures the subclass, and instantiates



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/pagy/classes/keyset/keyset.rb', line 25

def self.new(set, **)
  # 1. Handle direct subclass usage (e.g. Pagy::Keyset::ActiveRecord.new)
  if /::(?:ActiveRecord|Sequel)$/.match?(name)
    # Ensure the adapter is mixed in (lazy load)
    mix_in_adapter(name.split('::').last)
    return allocate.tap { |instance| instance.send(:initialize, set, **) }
  end

  # 2. Handle Factory usage (Pagy::Keyset.new)
  orm_name = if defined?(::ActiveRecord) && set.is_a?(::ActiveRecord::Relation)
               :ActiveRecord
             elsif defined?(::Sequel) && set.is_a?(::Sequel::Dataset)
               :Sequel
             else
               raise TypeError, "expected an ActiveRecord::Relation or Sequel::Dataset; got #{set.class}"
             end

  # Get the specific subclass (self::ActiveRecord)
  subclass = const_get(orm_name)
  # Ensure the adapter is mixed in (lazy load)
  subclass.mix_in_adapter(orm_name)
  subclass.new(set, **)
end

Instance Method Details

#nextObject

Return the next page (i.e., the cutoff of the current page)



75
76
77
78
79
80
# File 'lib/pagy/classes/keyset/keyset.rb', line 75

def next
  records
  return unless @more

  @next ||= B64.urlsafe_encode(extract_cutoff.to_json)
end

#recordsObject

Return the array of records for the current page



67
68
69
70
71
72
# File 'lib/pagy/classes/keyset/keyset.rb', line 67

def records
  @records ||= begin
                 ensure_select
                 fetch_records
               end
end