Class: WscSdk::Pagination

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

Overview

A class for handling the pagination data returned by endpoint lists.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pagination_data = {}) ⇒ Pagination

Get a new instance of the pagination class.

Parameters:

  • pagination_data (Hash) (defaults to: {})

    A hash of pagination data



15
16
17
18
19
20
21
22
# File 'lib/wsc_sdk/pagination.rb', line 15

def initialize(pagination_data={})
  @page               = pagination_data.fetch(:page, 1)
  @per_page           = pagination_data.fetch(:per_page, 1000)
  @total_pages        = pagination_data.fetch(:total_pages, 1)
  @total_records      = pagination_data.fetch(:total_records, -1)
  @page_first_index   = pagination_data.fetch(:page_first_index, -1)
  @page_last_index    = pagination_data.fetch(:page_last_index, -1)
end

Instance Attribute Details

#pageObject (readonly)

Returns the value of attribute page



9
10
11
# File 'lib/wsc_sdk/pagination.rb', line 9

def page
  @page
end

#page_first_indexObject (readonly)

Returns the value of attribute page_first_index



9
10
11
# File 'lib/wsc_sdk/pagination.rb', line 9

def page_first_index
  @page_first_index
end

#page_last_indexObject (readonly)

Returns the value of attribute page_last_index



9
10
11
# File 'lib/wsc_sdk/pagination.rb', line 9

def page_last_index
  @page_last_index
end

#per_pageObject (readonly)

Returns the value of attribute per_page



9
10
11
# File 'lib/wsc_sdk/pagination.rb', line 9

def per_page
  @per_page
end

#total_pagesObject (readonly)

Returns the value of attribute total_pages



9
10
11
# File 'lib/wsc_sdk/pagination.rb', line 9

def total_pages
  @total_pages
end

#total_recordsObject (readonly)

Returns the value of attribute total_records



9
10
11
# File 'lib/wsc_sdk/pagination.rb', line 9

def total_records
  @total_records
end

Instance Method Details

#first_pageInteger, Nil

Get the number of the first page

Returns:

  • (Integer)

    The number of the first page.

  • (Nil)

    If there is no first page.



39
40
41
42
# File 'lib/wsc_sdk/pagination.rb', line 39

def first_page
  return nil if total_pages < 1
  return 1
end

#first_page?Boolean

Determine if the current page is the first page

Returns:

  • (Boolean)

    An indication if the current page is the first page.



28
29
30
31
# File 'lib/wsc_sdk/pagination.rb', line 28

def first_page?
  return true if total_pages < 1
  return (page == 1)
end

#last_pageInteger, Nil

Get the number of the last page

Returns:

  • (Integer)

    The number of the first page.

  • (Nil)

    If there is no first page.



74
75
76
77
# File 'lib/wsc_sdk/pagination.rb', line 74

def last_page
  return nil if total_pages < 1
  return total_pages
end

#last_page?Boolean

Determine if the current page is the last page

Returns:

  • (Boolean)

    An indication if the current page is the last page.



83
84
85
86
# File 'lib/wsc_sdk/pagination.rb', line 83

def last_page?
  return true if total_pages < 1
  return (page == total_pages)
end

#next_pageInteger, Nil

Get the number of the next page.

Returns:

  • (Integer)

    The number of the next page

  • (Nil)

    If there are no remaining pages



50
51
52
53
54
# File 'lib/wsc_sdk/pagination.rb', line 50

def next_page
  return nil if total_pages == -1
  return nil if page == total_pages
  return page + 1
end

#previous_pageInteger, Nil

Get the number of the previous page.

Returns:

  • (Integer)

    The number of the previous page

  • (Nil)

    If there are no previous pages



62
63
64
65
66
# File 'lib/wsc_sdk/pagination.rb', line 62

def previous_page
  return nil if total_pages < 1
  return nil if page == 1
  return page - 1
end