Class: ActiverecordCursorPagination::Cursor

Inherits:
Object
  • Object
show all
Defined in:
lib/activerecord_cursor_pagination/cursor.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass_or_name, sql_or_signed_sql, per_page, start_id, end_id) ⇒ Cursor

Initialize a cursor

Parameters:

  • klass_or_name (Class, String)

    The model class

  • sql_or_signed_sql (ActiveRecord::Relation, String)

    The active record SQL relation

  • per_page (Integer)

    The number of records per page

  • start_id (Integer)

    The ID of the first record in the page

  • end_id (Integer)

    The ID of the last record in the page



13
14
15
16
17
18
19
# File 'lib/activerecord_cursor_pagination/cursor.rb', line 13

def initialize(klass_or_name, sql_or_signed_sql, per_page, start_id, end_id)
  @signed_sql = sql_or_signed_sql.is_a?(String) ? sql_or_signed_sql : sql_signer.sign(sql_or_signed_sql)
  @klass_name = class_formatter.format klass_or_name
  @per_page = per_page
  @start_id = start_id
  @end_id = end_id
end

Instance Attribute Details

#end_idObject (readonly)

Returns the value of attribute end_id.



3
4
5
# File 'lib/activerecord_cursor_pagination/cursor.rb', line 3

def end_id
  @end_id
end

#klass_nameObject (readonly)

Returns the value of attribute klass_name.



3
4
5
# File 'lib/activerecord_cursor_pagination/cursor.rb', line 3

def klass_name
  @klass_name
end

#per_pageObject (readonly)

Returns the value of attribute per_page.



3
4
5
# File 'lib/activerecord_cursor_pagination/cursor.rb', line 3

def per_page
  @per_page
end

#signed_sqlObject (readonly)

Returns the value of attribute signed_sql.



3
4
5
# File 'lib/activerecord_cursor_pagination/cursor.rb', line 3

def signed_sql
  @signed_sql
end

#start_idObject (readonly)

Returns the value of attribute start_id.



3
4
5
# File 'lib/activerecord_cursor_pagination/cursor.rb', line 3

def start_id
  @start_id
end

Class Method Details

.class_formatterClassFormatter

Get class formatter

Returns:



99
100
101
# File 'lib/activerecord_cursor_pagination/cursor.rb', line 99

def class_formatter
  ClassFormatter.new
end

.parse(str) ⇒ Cursor, EmptyCursor

Parse the cursor string

Parameters:

  • str (String)

    Cursor serialized string.

Returns:



117
118
119
120
121
122
123
124
125
126
127
# File 'lib/activerecord_cursor_pagination/cursor.rb', line 117

def parse(str)
  return EmptyCursor.new if str.nil? || str.empty?

  hash = serializer.deserialize str

  new hash[:model],
      hash[:sql],
      hash[:per_page],
      hash[:start],
      hash[:end]
end

.serializerSerializer

Get cursor serializer instance

Returns:



107
108
109
# File 'lib/activerecord_cursor_pagination/cursor.rb', line 107

def serializer
  ActiverecordCursorPagination.configuration.serializer_instance
end

.sql_signerSqlSigner

Get sql signer instance

Returns:



91
92
93
# File 'lib/activerecord_cursor_pagination/cursor.rb', line 91

def sql_signer
  SqlSigner.new
end

.to_param(klass_or_name, sql_or_signed_sql, per_page, start_id, end_id) ⇒ String

Serialize the cursor

Parameters:

  • klass_or_name (Class, String)

    The model class

  • sql_or_signed_sql (ActiveRecord::Relation, String)

    The active record SQL relation

  • per_page (Integer)

    The number of records per page

  • start_id (Integer)

    The ID of the first record in the page

  • end_id (Integer)

    The ID of the last record in the page

Returns:

  • (String)

    The serialized cursor string



139
140
141
# File 'lib/activerecord_cursor_pagination/cursor.rb', line 139

def to_param(klass_or_name, sql_or_signed_sql, per_page, start_id, end_id)
  new(klass_or_name, sql_or_signed_sql, per_page, start_id, end_id).to_param
end

Instance Method Details

#empty?Boolean

Is the cursor empty

Returns:

  • (Boolean)


33
34
35
# File 'lib/activerecord_cursor_pagination/cursor.rb', line 33

def empty?
  @start_id.nil? || @end_id.nil?
end

#present?Boolean

Is the cursor not empty

Returns:

  • (Boolean)


25
26
27
# File 'lib/activerecord_cursor_pagination/cursor.rb', line 25

def present?
  !empty?
end

#to_hashHash

Gets the hash representation of the cursor

Returns:

  • (Hash)


41
42
43
44
45
46
47
48
49
# File 'lib/activerecord_cursor_pagination/cursor.rb', line 41

def to_hash
  {
    start: @start_id,
    end: @end_id,
    per_page: @per_page,
    model: @klass_name,
    sql: @signed_sql
  }
end

#to_sString Also known as: to_param

Get the string representation of the cursor

Returns:

  • (String)

    The serialized cursor



55
56
57
# File 'lib/activerecord_cursor_pagination/cursor.rb', line 55

def to_s
  serializer.serialize to_hash
end

#validate!(klass, sql, per_page) ⇒ Object

Validates the cursor

Parameters:

  • klass (Class)

    The model class

  • sql (ActiveRecord::Relation)

    The active record SQL relation

  • per_page (Integer)

    The number of records per page

Raises:



69
70
71
# File 'lib/activerecord_cursor_pagination/cursor.rb', line 69

def validate!(klass, sql, per_page)
  raise InvalidCursorError.new('Invalid cursor', self) unless valid?(klass, sql, per_page)
end