Class: Kadmin::Pager

Inherits:
Object
  • Object
show all
Defined in:
lib/kadmin/pager.rb

Overview

Simple Pager structure, used to paginate collections

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(size:, offset:) ⇒ Pager

Returns a new instance of Pager.

Parameters:

  • size (Integer)

    size of a page

  • offset (Integer)

    offset/index for the current page

Raises:



21
22
23
24
25
26
27
28
29
30
# File 'lib/kadmin/pager.rb', line 21

def initialize(size:, offset:)
  @size = size
  @offset = offset

  raise(Kadmin::Error, 'Page size must be greater than 0!') unless @size.positive?
  raise(Kadmin::Error, 'Offset must be at least 0!') unless @offset >= 0

  @current_page = (@offset / @size.to_f).floor
  self.total = @size # assume page size is maximum initially

end

Instance Attribute Details

#current_pageInteger (readonly)

Returns the current page.

Returns:

  • (Integer)

    the current page



14
15
16
# File 'lib/kadmin/pager.rb', line 14

def current_page
  @current_page
end

#offsetInteger (readonly)

Returns current index offset (determines current page).

Returns:

  • (Integer)

    current index offset (determines current page)



8
9
10
# File 'lib/kadmin/pager.rb', line 8

def offset
  @offset
end

#pagesInteger (readonly)

Returns total number of pages.

Returns:

  • (Integer)

    total number of pages



11
12
13
# File 'lib/kadmin/pager.rb', line 11

def pages
  @pages
end

#sizeInteger (readonly)

Returns number of items per page.

Returns:

  • (Integer)

    number of items per page



5
6
7
# File 'lib/kadmin/pager.rb', line 5

def size
  @size
end

#totalInteger

Returns total number of items in the collection.

Returns:

  • (Integer)

    total number of items in the collection



17
18
19
# File 'lib/kadmin/pager.rb', line 17

def total
  @total
end

Instance Method Details

#contains?(page) ⇒ Boolean

Returns true if page exists (i.e. would have any data).

Parameters:

  • page (Integer)

    the page to check for

Returns:

  • (Boolean)

    true if page exists (i.e. would have any data)



58
59
60
# File 'lib/kadmin/pager.rb', line 58

def contains?(page)
  page.in?(0...@pages)
end

#current_page?(page) ⇒ Boolean

Returns true if page is the current page.

Parameters:

  • page (Integer)

    the page to check for

Returns:

  • (Boolean)

    true if page is the current page



52
53
54
# File 'lib/kadmin/pager.rb', line 52

def current_page?(page)
  return page == @current_page
end

#next_page?(page = nil) ⇒ Boolean

Returns true if there is a next page.

Parameters:

  • page (Integer) (defaults to: nil)

    optional; if given, checks if the page after would have any data, otherwise checks based on the current page

Returns:

  • (Boolean)

    true if there is a next page



80
81
82
83
# File 'lib/kadmin/pager.rb', line 80

def next_page?(page = nil)
  page ||= @current_page
  return offset_at(page.to_i + 1) < @total
end

#offset_at(page = nil) ⇒ Integer

Returns start offset for the given page.

Parameters:

  • page (Integer) (defaults to: nil)

    the page to get the offset for; if not given, uses the current page

Returns:

  • (Integer)

    start offset for the given page



45
46
47
48
# File 'lib/kadmin/pager.rb', line 45

def offset_at(page = nil)
  page ||= @current_page
  return @size * page.to_i
end

#page(collection) ⇒ ActiveRecord::Relation

Returns paginated collection.

Parameters:

  • collection (ActiveRecord::Relation)

    relation to paginate

Returns:

  • (ActiveRecord::Relation)

    paginated collection



34
35
36
37
38
39
40
41
# File 'lib/kadmin/pager.rb', line 34

def page(collection)
  self.total = collection.count

  collection = collection.offset(@offset)
  collection = collection.limit(@size)

  return collection
end

#page_size(page = nil) ⇒ Integer

Returns the number of items that are on this page.

Parameters:

  • page (Integer) (defaults to: nil)

    optional; if not given, uses the current page

Returns:

  • (Integer)

    the number of items that are on this page



70
71
72
73
74
75
76
# File 'lib/kadmin/pager.rb', line 70

def page_size(page = nil)
  page ||= @current_page
  page_start = offset_at(page)
  page_end = [offset_at(page.to_i + 1), @total].min

  return page_end - page_start
end

#previous_page?(page = nil) ⇒ Boolean

Returns true if there is a previous page.

Parameters:

  • page (Integer) (defaults to: nil)

    optional; if given, checks if the page after would have any data, otherwise checks based on the current page

Returns:

  • (Boolean)

    true if there is a previous page



87
88
89
90
91
# File 'lib/kadmin/pager.rb', line 87

def previous_page?(page = nil)
  page ||= @current_page

  return page.to_i.positive?
end