Skip to content

Commit 5384ed6

Browse files
committed
Add basic documentation to paginator methods
1 parent cf408d1 commit 5384ed6

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

lib/pliny/helpers/paginator.rb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,28 @@
11
module 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

lib/pliny/helpers/paginator/paginator.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,18 @@ def run(*args, &block)
1818
end
1919
end
2020

21+
# Initializes an instance of Paginator
22+
#
23+
# @param [Sinatra::Base] the controller calling the paginator
24+
# @param [Integer] count the count of resources
25+
# @param [Hash] options for the paginator
26+
# @option options [Array<Symbol>] :accepted_ranges ([:id]) fields allowed to sort the listing
27+
# @option options [Symbol] :sort_by (:id) field to sort the listing
28+
# @option options [String] :first ID or name of the first element of the current page
29+
# @option options [String] :last ID or name of the last element of the current page
30+
# @option options [String] :next_first ID or name of the first element of the next page
31+
# @option options [String] :next_last ID or name of the last element of the next page
32+
# @option options [Hash] :args ({max: 200}) arguments for the HTTP Range header
2133
def initialize(sinatra, count, options = {})
2234
@sinatra = sinatra
2335
@count = count
@@ -34,6 +46,11 @@ def initialize(sinatra, count, options = {})
3446
.merge(options)
3547
end
3648

49+
# executes the paginator and sets the HTTP headers if necessary
50+
#
51+
# @yieldparam paginator [Paginator]
52+
# @yieldreturn [Object]
53+
# @return [Object] the result of the block yielded
3754
def run
3855
options.merge!(request_options)
3956

0 commit comments

Comments
 (0)