Class: ActiverecordCursorPagination::Cursor
- Inherits:
-
Object
- Object
- ActiverecordCursorPagination::Cursor
- Defined in:
- lib/activerecord_cursor_pagination/cursor.rb
Instance Attribute Summary collapse
-
#end_id ⇒ Object
readonly
Returns the value of attribute end_id.
-
#klass_name ⇒ Object
readonly
Returns the value of attribute klass_name.
-
#per_page ⇒ Object
readonly
Returns the value of attribute per_page.
-
#signed_sql ⇒ Object
readonly
Returns the value of attribute signed_sql.
-
#start_id ⇒ Object
readonly
Returns the value of attribute start_id.
Class Method Summary collapse
-
.class_formatter ⇒ ClassFormatter
Get class formatter.
-
.parse(str) ⇒ Cursor, EmptyCursor
Parse the cursor string.
-
.serializer ⇒ Serializer
Get cursor serializer instance.
-
.sql_signer ⇒ SqlSigner
Get sql signer instance.
-
.to_param(klass_or_name, sql_or_signed_sql, per_page, start_id, end_id) ⇒ String
Serialize the cursor.
Instance Method Summary collapse
-
#empty? ⇒ Boolean
Is the cursor empty.
-
#initialize(klass_or_name, sql_or_signed_sql, per_page, start_id, end_id) ⇒ Cursor
constructor
Initialize a cursor.
-
#present? ⇒ Boolean
Is the cursor not empty.
-
#to_hash ⇒ Hash
Gets the hash representation of the cursor.
-
#to_s ⇒ String
(also: #to_param)
Get the string representation of the cursor.
-
#validate!(klass, sql, per_page) ⇒ Object
Validates the cursor.
Constructor Details
#initialize(klass_or_name, sql_or_signed_sql, per_page, start_id, end_id) ⇒ Cursor
Initialize a cursor
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_id ⇒ Object (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_name ⇒ Object (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_page ⇒ Object (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_sql ⇒ Object (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_id ⇒ Object (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_formatter ⇒ ClassFormatter
Get class formatter
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
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 |
.serializer ⇒ Serializer
Get cursor serializer instance
107 108 109 |
# File 'lib/activerecord_cursor_pagination/cursor.rb', line 107 def serializer ActiverecordCursorPagination.configuration.serializer_instance end |
.sql_signer ⇒ SqlSigner
Get sql signer instance
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
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
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
25 26 27 |
# File 'lib/activerecord_cursor_pagination/cursor.rb', line 25 def present? !empty? end |
#to_hash ⇒ Hash
Gets the hash representation of the cursor
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_s ⇒ String Also known as: to_param
Get the string representation of the 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
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 |