Class: DHS::Pagination::Base

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

Direct Known Subclasses

Link, NextOffset, NextParameter, 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.



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

def initialize(data)
  self.data = data
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



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

def data
  @data
end

Class Method Details

.page_to_offset(page, _limit) ⇒ Object



86
87
88
# File 'lib/dhs/pagination/base.rb', line 86

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

Instance Method Details

#current_pageObject



46
47
48
# File 'lib/dhs/pagination/base.rb', line 46

def current_page
  # should be implemented in subclass (optional)
end

#first_pageObject



50
51
52
# File 'lib/dhs/pagination/base.rb', line 50

def first_page
  1
end

#last_pageObject



54
55
56
# File 'lib/dhs/pagination/base.rb', line 54

def last_page
  total_pages
end

#limitObject



25
26
27
# File 'lib/dhs/pagination/base.rb', line 25

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

#limit_valueObject



78
79
80
# File 'lib/dhs/pagination/base.rb', line 78

def limit_value
  limit
end

#next?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/dhs/pagination/base.rb', line 62

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

#next_offset(_step = 1) ⇒ Object



42
43
44
# File 'lib/dhs/pagination/base.rb', line 42

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

#next_pageObject



74
75
76
# File 'lib/dhs/pagination/base.rb', line 74

def next_page
  current_page + 1
end

#offsetObject Also known as: start



29
30
31
# File 'lib/dhs/pagination/base.rb', line 29

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

#pages_leftObject



34
35
36
# File 'lib/dhs/pagination/base.rb', line 34

def pages_left
  total_pages - current_page
end

#pages_left?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/dhs/pagination/base.rb', line 38

def pages_left?
  pages_left > 0
end

#parallel?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/dhs/pagination/base.rb', line 58

def parallel?
  true
end

#prev_pageObject



70
71
72
# File 'lib/dhs/pagination/base.rb', line 70

def prev_page
  current_page - 1
end

#previous?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/dhs/pagination/base.rb', line 66

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

#totalObject Also known as: count



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

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

#total_pagesObject



82
83
84
# File 'lib/dhs/pagination/base.rb', line 82

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