Class: LHS::Pagination::Base
- Inherits:
-
Object
- Object
- LHS::Pagination::Base
- Defined in:
- lib/lhs/pagination/base.rb
Constant Summary collapse
- DEFAULT_LIMIT =
100
Instance Attribute Summary collapse
-
#data ⇒ Object
Returns the value of attribute data.
Class Method Summary collapse
Instance Method Summary collapse
- #current_page ⇒ Object
- #first_page ⇒ Object
-
#initialize(data) ⇒ Base
constructor
A new instance of Base.
- #last_page ⇒ Object
- #limit ⇒ Object
- #limit_value ⇒ Object
- #next? ⇒ Boolean
- #next_offset(_step = 1) ⇒ Object
- #next_page ⇒ Object
- #offset ⇒ Object (also: #start)
- #pages_left ⇒ Object
- #prev_page ⇒ Object
- #previous? ⇒ Boolean
- #total ⇒ Object (also: #count)
- #total_pages ⇒ Object
Constructor Details
#initialize(data) ⇒ Base
Returns a new instance of Base.
14 15 16 |
# File 'lib/lhs/pagination/base.rb', line 14 def initialize(data) self.data = data end |
Instance Attribute Details
#data ⇒ Object
Returns the value of attribute data.
12 13 14 |
# File 'lib/lhs/pagination/base.rb', line 12 def data @data end |
Class Method Details
.page_to_offset(page, _limit) ⇒ Object
78 79 80 |
# File 'lib/lhs/pagination/base.rb', line 78 def self.page_to_offset(page, _limit) page.to_i end |
Instance Method Details
#current_page ⇒ Object
42 43 44 |
# File 'lib/lhs/pagination/base.rb', line 42 def current_page raise 'to be implemented in subclass' end |
#first_page ⇒ Object
46 47 48 |
# File 'lib/lhs/pagination/base.rb', line 46 def first_page 1 end |
#last_page ⇒ Object
50 51 52 |
# File 'lib/lhs/pagination/base.rb', line 50 def last_page total_pages end |
#limit ⇒ Object
25 26 27 |
# File 'lib/lhs/pagination/base.rb', line 25 def limit data._raw.dig(*_record.limit_key(:body)) || DEFAULT_LIMIT end |
#limit_value ⇒ Object
70 71 72 |
# File 'lib/lhs/pagination/base.rb', line 70 def limit_value limit end |
#next? ⇒ Boolean
54 55 56 |
# File 'lib/lhs/pagination/base.rb', line 54 def next? data._raw[:next].present? end |
#next_offset(_step = 1) ⇒ Object
38 39 40 |
# File 'lib/lhs/pagination/base.rb', line 38 def next_offset(_step = 1) raise 'to be implemented in subclass' end |
#next_page ⇒ Object
66 67 68 |
# File 'lib/lhs/pagination/base.rb', line 66 def next_page current_page + 1 end |
#offset ⇒ Object Also known as: start
29 30 31 |
# File 'lib/lhs/pagination/base.rb', line 29 def offset data._raw.dig(*_record.pagination_key(:body)) || self.class::DEFAULT_OFFSET end |
#pages_left ⇒ Object
34 35 36 |
# File 'lib/lhs/pagination/base.rb', line 34 def pages_left total_pages - current_page end |
#prev_page ⇒ Object
62 63 64 |
# File 'lib/lhs/pagination/base.rb', line 62 def prev_page current_page - 1 end |
#previous? ⇒ Boolean
58 59 60 |
# File 'lib/lhs/pagination/base.rb', line 58 def previous? data._raw[:previous].present? end |
#total ⇒ Object Also known as: count
18 19 20 |
# File 'lib/lhs/pagination/base.rb', line 18 def total data._raw.dig(*_record.total_key) || 0 end |
#total_pages ⇒ Object
74 75 76 |
# File 'lib/lhs/pagination/base.rb', line 74 def total_pages (total.to_f / limit).ceil end |