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.
12 13 14 |
# File 'lib/lhs/pagination/base.rb', line 12 def initialize(data) self.data = data end |
Instance Attribute Details
#data ⇒ Object
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_page ⇒ Object
40 41 42 |
# File 'lib/lhs/pagination/base.rb', line 40 def current_page raise 'to be implemented in subclass' end |
#first_page ⇒ Object
44 45 46 |
# File 'lib/lhs/pagination/base.rb', line 44 def first_page 1 end |
#last_page ⇒ Object
48 49 50 |
# File 'lib/lhs/pagination/base.rb', line 48 def last_page total_pages end |
#limit ⇒ Object
23 24 25 |
# File 'lib/lhs/pagination/base.rb', line 23 def limit data._raw.dig(*_record.limit_key(:body)) || DEFAULT_LIMIT end |
#limit_value ⇒ Object
68 69 70 |
# File 'lib/lhs/pagination/base.rb', line 68 def limit_value limit end |
#next? ⇒ 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_page ⇒ Object
64 65 66 |
# File 'lib/lhs/pagination/base.rb', line 64 def next_page current_page + 1 end |
#offset ⇒ Object 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_left ⇒ Object
32 33 34 |
# File 'lib/lhs/pagination/base.rb', line 32 def pages_left total_pages - current_page end |
#prev_page ⇒ Object
60 61 62 |
# File 'lib/lhs/pagination/base.rb', line 60 def prev_page current_page - 1 end |
#previous? ⇒ Boolean
56 57 58 |
# File 'lib/lhs/pagination/base.rb', line 56 def previous? data._raw[:previous].present? end |
#total ⇒ Object 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_pages ⇒ Object
72 73 74 |
# File 'lib/lhs/pagination/base.rb', line 72 def total_pages (total.to_f / limit).ceil end |