Class: OffsetPaginator
- Inherits:
-
JSONAPI::Paginator
- Object
- JSONAPI::Paginator
- OffsetPaginator
- Defined in:
- lib/jsonapi/paginator.rb
Instance Attribute Summary collapse
-
#limit ⇒ Object
readonly
Returns the value of attribute limit.
-
#offset ⇒ Object
readonly
Returns the value of attribute offset.
Class Method Summary collapse
Instance Method Summary collapse
- #apply(relation, _order_options) ⇒ Object
-
#initialize(params) ⇒ OffsetPaginator
constructor
A new instance of OffsetPaginator.
- #links_page_params(options = {}) ⇒ Object
Methods inherited from JSONAPI::Paginator
Constructor Details
#initialize(params) ⇒ OffsetPaginator
Returns a new instance of OffsetPaginator.
36 37 38 39 |
# File 'lib/jsonapi/paginator.rb', line 36 def initialize(params) parse_pagination_params(params) verify_pagination_params end |
Instance Attribute Details
#limit ⇒ Object (readonly)
Returns the value of attribute limit.
34 35 36 |
# File 'lib/jsonapi/paginator.rb', line 34 def limit @limit end |
#offset ⇒ Object (readonly)
Returns the value of attribute offset.
34 35 36 |
# File 'lib/jsonapi/paginator.rb', line 34 def offset @offset end |
Class Method Details
.requires_record_count ⇒ Object
41 42 43 |
# File 'lib/jsonapi/paginator.rb', line 41 def self.requires_record_count true end |
Instance Method Details
#apply(relation, _order_options) ⇒ Object
45 46 47 |
# File 'lib/jsonapi/paginator.rb', line 45 def apply(relation, ) relation.offset(@offset).limit(@limit) end |
#links_page_params(options = {}) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/jsonapi/paginator.rb', line 49 def links_page_params( = {}) record_count = [:record_count] links_page_params = {} links_page_params['first'] = { 'offset' => 0, 'limit' => @limit } if @offset > 0 previous_offset = @offset - @limit previous_offset = 0 if previous_offset < 0 links_page_params['prev'] = { 'offset' => previous_offset, 'limit' => @limit } end next_offset = @offset + @limit unless next_offset >= record_count links_page_params['next'] = { 'offset' => next_offset, 'limit' => @limit } end if record_count last_offset = record_count - @limit last_offset = 0 if last_offset < 0 links_page_params['last'] = { 'offset' => last_offset, 'limit' => @limit } end links_page_params end |