Class: GearedPagination::Cursor

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(page_number: 1, values: {}) ⇒ Cursor

Returns a new instance of Cursor.



26
27
28
# File 'lib/geared_pagination/cursor.rb', line 26

def initialize(page_number: 1, values: {})
  @page_number, @values = page_number, values
end

Instance Attribute Details

#valuesObject (readonly)

Returns the value of attribute values.



24
25
26
# File 'lib/geared_pagination/cursor.rb', line 24

def values
  @values
end

Class Method Details

.decode(key) ⇒ Object



13
14
15
16
17
# File 'lib/geared_pagination/cursor.rb', line 13

def decode(key)
  if attributes = ActiveSupport::JSON.decode(Base64.urlsafe_decode64(key))
    new **attributes.deep_symbolize_keys
  end
end

.encode(page_number: 1, values: {}) ⇒ Object



19
20
21
# File 'lib/geared_pagination/cursor.rb', line 19

def encode(page_number: 1, values: {})
  Base64.urlsafe_encode64 ActiveSupport::JSON.encode(page_number: page_number, values: values)
end

.from_param(key) ⇒ Object



7
8
9
10
11
# File 'lib/geared_pagination/cursor.rb', line 7

def from_param(key)
  key.present? ? decode(key) : new
rescue ArgumentError, JSON::ParserError
  new
end

Instance Method Details

#fetch(attribute) ⇒ Object



34
35
36
# File 'lib/geared_pagination/cursor.rb', line 34

def fetch(attribute)
  values.fetch(attribute)
end

#include?(attribute) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/geared_pagination/cursor.rb', line 38

def include?(attribute)
  values.include?(attribute)
end

#page_numberObject



30
31
32
# File 'lib/geared_pagination/cursor.rb', line 30

def page_number
  @page_number > 0 ? @page_number : 1
end