11module Pliny ::Helpers
22 module Paginator
3+
4+ # Sets the HTTP Range header for pagination if necessary
5+ #
6+ # @see uuid_paginator
7+ # @see integer_paginator
8+ # @see Pliny::Helpers::Paginator::Paginator
39 def paginator ( count , options = { } , &block )
410 Paginator . run ( self , count , options , &block )
511 end
612
13+ # paginator for UUID columns
14+ #
15+ # @example call in the Endpoint
16+ # articles = uuid_paginator(Article, args: { max: 10 })
17+ #
18+ # @example HTTP header returned
19+ # Content-Range: id 01234567-89ab-cdef-0123-456789abcdef..01234567-89ab-cdef-0123-456789abcdef/400; max=10
20+ # Next-Range: id 76543210-89ab-cdef-0123-456789abcdef..76543210-89ab-cdef-0123-456789abcdef/400; max=10
21+ #
22+ # @param [Object] resource the resource to paginate
23+ # @param [Hash] options
24+ # @return [Object] modified resource (by order, limit and offset)
25+ # @see paginator
726 def uuid_paginator ( resource , options = { } )
827 paginator ( resource . count , options ) do |paginator |
928 sort_by_conversion = { id : :uuid }
@@ -27,6 +46,21 @@ def uuid_paginator(resource, options = {})
2746 end
2847 end
2948
49+ # paginator for integer columns
50+ #
51+ # @example call in the Endpoint
52+ # paginator = integer_paginator(User.count)
53+ # users = User.order(paginator[:order_by]).limit(paginator[:limit]).offset(paginator[:offset])
54+ #
55+ # @example HTTP header returned
56+ # Content-Range: id 0..199/400; max=200
57+ # Next-Range: id 200..399/400; max=200
58+ #
59+ # @param [Integer] count the count of resources
60+ # @param [Hash] options
61+ # @return [Hash] with :order_by and calculated :offset and :limit
62+ # @see paginator
63+ # @see Pliny::Helpers::Paginator::IntegerPaginator
3064 def integer_paginator ( count , options = { } )
3165 IntegerPaginator . run ( self , count , options )
3266 end
0 commit comments