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.



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

def initialize(data)
  self.data = data
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



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

def data
  @data
end

Class Method Details

.page_to_offset(page, _limit) ⇒ Object



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

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



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

def count
  total
end

#first_pageObject



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

def first_page
  1
end

#last_pageObject



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

def last_page
  total_pages
end

#limitObject



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

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

#limit_valueObject



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

def limit_value
  limit
end

#next?Boolean

Returns:

  • (Boolean)


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

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

#next_offset(_step = 1) ⇒ Object



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

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

#next_pageObject



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

def next_page
  current_page + 1
end

#offsetObject Also known as: current_page, start



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

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

#pages_leftObject



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

def pages_left
  total_pages - current_page
end

#prev_pageObject



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

def prev_page
  current_page - 1
end

#previous?Boolean

Returns:

  • (Boolean)


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

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

#totalObject



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

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

#total_pagesObject



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

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