Class: LHS::Pagination::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/lhs/pagination/base.rb

Direct Known Subclasses

Offset, Page, Start

Constant Summary collapse

DEFAULT_LIMIT =
100

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Base

Returns a new instance of Base.



12
13
14
# File 'lib/lhs/pagination/base.rb', line 12

def initialize(data)
  self.data = data
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



10
11
12
# File 'lib/lhs/pagination/base.rb', line 10

def data
  @data
end

Class Method Details

.page_to_offset(page, _limit) ⇒ Object



76
77
78
# File 'lib/lhs/pagination/base.rb', line 76

def self.page_to_offset(page, _limit)
  page.to_i
end

Instance Method Details

#current_pageObject



40
41
42
# File 'lib/lhs/pagination/base.rb', line 40

def current_page
  raise 'to be implemented in subclass'
end

#first_pageObject



44
45
46
# File 'lib/lhs/pagination/base.rb', line 44

def first_page
  1
end

#last_pageObject



48
49
50
# File 'lib/lhs/pagination/base.rb', line 48

def last_page
  total_pages
end

#limitObject



23
24
25
# File 'lib/lhs/pagination/base.rb', line 23

def limit
  data._raw.dig(*_record.limit_key(:body)) || DEFAULT_LIMIT
end

#limit_valueObject



68
69
70
# File 'lib/lhs/pagination/base.rb', line 68

def limit_value
  limit
end

#next?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/lhs/pagination/base.rb', line 52

def next?
  data._raw[:next].present?
end

#next_offset(_step = 1) ⇒ Object



36
37
38
# File 'lib/lhs/pagination/base.rb', line 36

def next_offset(_step = 1)
  raise 'to be implemented in subclass'
end

#next_pageObject



64
65
66
# File 'lib/lhs/pagination/base.rb', line 64

def next_page
  current_page + 1
end

#offsetObject Also known as: start



27
28
29
# File 'lib/lhs/pagination/base.rb', line 27

def offset
  data._raw.dig(*_record.pagination_key(:body)) || self.class::DEFAULT_OFFSET
end

#pages_leftObject



32
33
34
# File 'lib/lhs/pagination/base.rb', line 32

def pages_left
  total_pages - current_page
end

#prev_pageObject



60
61
62
# File 'lib/lhs/pagination/base.rb', line 60

def prev_page
  current_page - 1
end

#previous?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/lhs/pagination/base.rb', line 56

def previous?
  data._raw[:previous].present?
end

#totalObject Also known as: count



16
17
18
# File 'lib/lhs/pagination/base.rb', line 16

def total
  data._raw.dig(*_record.total_key) || 0
end

#total_pagesObject



72
73
74
# File 'lib/lhs/pagination/base.rb', line 72

def total_pages
  (total.to_f / limit).ceil
end