Class: PackAPI::Pagination::Paginator

Inherits:
Object
  • Object
show all
Defined in:
lib/pack_api/pagination/paginator.rb

Constant Summary collapse

DEFAULT_PER_PAGE =
20

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#metadataObject

Parameters:

  • query (Hash)

    The query parameters used to define the recordset.

  • sort (String|Symbol|Hash|Arel)

    The ordering used to define the recordset

  • per_page (Integer|:all)

    The count of items to include on each result page, or :all for single page results

  • offset (Integer)

    The starting index of the next result page

  • total_items (Integer)

    The count of items in the result set

  • metadata (Any|nil)

    optional, extra data structure to be passed along with the cursor



32
33
34
# File 'lib/pack_api/pagination/paginator.rb', line 32

def 
  @metadata
end

#offsetObject

Parameters:

  • query (Hash)

    The query parameters used to define the recordset.

  • sort (String|Symbol|Hash|Arel)

    The ordering used to define the recordset

  • per_page (Integer|:all)

    The count of items to include on each result page, or :all for single page results

  • offset (Integer)

    The starting index of the next result page

  • total_items (Integer)

    The count of items in the result set

  • metadata (Any|nil)

    optional, extra data structure to be passed along with the cursor



32
33
34
# File 'lib/pack_api/pagination/paginator.rb', line 32

def offset
  @offset
end

#per_pageObject

Parameters:

  • query (Hash)

    The query parameters used to define the recordset.

  • sort (String|Symbol|Hash|Arel)

    The ordering used to define the recordset

  • per_page (Integer|:all)

    The count of items to include on each result page, or :all for single page results

  • offset (Integer)

    The starting index of the next result page

  • total_items (Integer)

    The count of items in the result set

  • metadata (Any|nil)

    optional, extra data structure to be passed along with the cursor



32
33
34
# File 'lib/pack_api/pagination/paginator.rb', line 32

def per_page
  @per_page
end

#queryObject

Parameters:

  • query (Hash)

    The query parameters used to define the recordset.

  • sort (String|Symbol|Hash|Arel)

    The ordering used to define the recordset

  • per_page (Integer|:all)

    The count of items to include on each result page, or :all for single page results

  • offset (Integer)

    The starting index of the next result page

  • total_items (Integer)

    The count of items in the result set

  • metadata (Any|nil)

    optional, extra data structure to be passed along with the cursor



32
33
34
# File 'lib/pack_api/pagination/paginator.rb', line 32

def query
  @query
end

#sortObject

Parameters:

  • query (Hash)

    The query parameters used to define the recordset.

  • sort (String|Symbol|Hash|Arel)

    The ordering used to define the recordset

  • per_page (Integer|:all)

    The count of items to include on each result page, or :all for single page results

  • offset (Integer)

    The starting index of the next result page

  • total_items (Integer)

    The count of items in the result set

  • metadata (Any|nil)

    optional, extra data structure to be passed along with the cursor



32
33
34
# File 'lib/pack_api/pagination/paginator.rb', line 32

def sort
  @sort
end

#total_itemsObject

Parameters:

  • query (Hash)

    The query parameters used to define the recordset.

  • sort (String|Symbol|Hash|Arel)

    The ordering used to define the recordset

  • per_page (Integer|:all)

    The count of items to include on each result page, or :all for single page results

  • offset (Integer)

    The starting index of the next result page

  • total_items (Integer)

    The count of items in the result set

  • metadata (Any|nil)

    optional, extra data structure to be passed along with the cursor



32
33
34
# File 'lib/pack_api/pagination/paginator.rb', line 32

def total_items
  @total_items
end

Instance Method Details

#current_page_cursorObject

Represents a single page of results in the current record set– the “current” page



56
57
58
# File 'lib/pack_api/pagination/paginator.rb', line 56

def current_page_cursor
  make_cursor(current_page_cursor_params)
end

#first_page_cursorObject

Represents a single page of results in the current record set– the “first” N results



74
75
76
# File 'lib/pack_api/pagination/paginator.rb', line 74

def first_page_cursor
  make_cursor(first_page_params)
end

#item_rangeObject

The range of items included in the results.



38
39
40
41
42
43
44
45
46
# File 'lib/pack_api/pagination/paginator.rb', line 38

def item_range
  return 0..0 if per_page != :all && per_page.zero?

  lower_bound = offset + 1
  upper_bound = per_page == :all ?
                  total_items :
                  [(offset + per_page), total_items].min
  lower_bound..upper_bound
end

#last_page_cursorObject

Represents a single page of results in the current record set– the “last” N results



80
81
82
# File 'lib/pack_api/pagination/paginator.rb', line 80

def last_page_cursor
  make_cursor(last_page_params)
end

#limitObject



84
85
86
87
88
# File 'lib/pack_api/pagination/paginator.rb', line 84

def limit
  return nil if per_page == :all

  per_page
end

#next_page_cursorObject

Represents a single page of results in the current record set– the “next” N results



62
63
64
# File 'lib/pack_api/pagination/paginator.rb', line 62

def next_page_cursor
  make_cursor(next_page_params)
end

#previous_page_cursorObject

Represents a single page of results in the current record set– the “previous” N results



68
69
70
# File 'lib/pack_api/pagination/paginator.rb', line 68

def previous_page_cursor
  make_cursor(previous_page_params)
end

#recordset_cursorObject

Represent the record set as a cursor– this captures the current search criteria (filters, sort, etc.)



50
51
52
# File 'lib/pack_api/pagination/paginator.rb', line 50

def recordset_cursor
  make_cursor(recordset_cursor_params)
end