Class: LHS::Pagination

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

Overview

Pagination is used to navigate paginateable collections

Direct Known Subclasses

OffsetPagination, PagePagination, StartPagination

Constant Summary collapse

DEFAULT_LIMIT =
100

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Pagination

Returns a new instance of Pagination.



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

def initialize(data)
  self.data = data
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



7
8
9
# File 'lib/lhs/pagination.rb', line 7

def data
  @data
end

Class Method Details

.page_to_offset(page, _limit) ⇒ Object



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

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

Instance Method Details

#countObject

as standard in Rails’ ActiveRecord count is not summing up, but using the number provided from data source



14
15
16
# File 'lib/lhs/pagination.rb', line 14

def count
  total
end

#first_pageObject



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

def first_page
  1
end

#last_pageObject



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

def last_page
  total_pages
end

#limitObject



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

def limit
  data._raw[_record.limit_key.to_sym] || LHS::Pagination::DEFAULT_LIMIT
end

#limit_valueObject



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

def limit_value
  limit
end

#next_offsetObject



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

def next_offset
  raise 'to be implemented in subclass'
end

#next_pageObject



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

def next_page
  current_page + 1
end

#offsetObject Also known as: current_page, start



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

def offset
  data._raw[_record.pagination_key.to_sym].presence || 0
end

#pages_leftObject



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

def pages_left
  total_pages - current_page
end

#prev_pageObject



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

def prev_page
  current_page - 1
end

#totalObject



18
19
20
# File 'lib/lhs/pagination.rb', line 18

def total
  data._raw[_record.total_key.to_sym]
end

#total_pagesObject



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

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