Class: Seq::Pager

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

Overview

Takes a list of items and returns them in pages. This can be thought of as doing the opposite of Paged.

Examples:


pages = Seq::Paged.new(5, 1..100)

pages.next     #=> [1,2,3,4,5]
pages.next     #=> [6,7,8,9,10]
pages.prev     #=> [1,2,3,4,5]
pages.page     #=> 0
pages.first?   #=> true
pages.last     #=> [96,97,98,99,100]

Constant Summary

Constants inherited from Seq

VERSION

Instance Method Summary collapse

Methods inherited from Seq

#each, #ended?, #entries, #inc, #infinite?, #method_missing, #reset, #to_a

Constructor Details

#initialize(page_size, start_page = nil, elements) ⇒ Pager

Creates a new instance of Pager.

Parameters:

  • page_size (Integer)

    Size of page to use

  • start_page (Integer) (defaults to: nil)

    Page to start at

  • elements (Enumerable)

    Items to initialize with



26
27
28
29
# File 'lib/seq/pager.rb', line 26

def initialize(page_size, start_page=nil, elements)
  @pages = elements.each_slice(page_size).to_a
  @curr  = start_page || 0
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Seq

Instance Method Details

#currArray

Returns the current page without modifying the current position.

Returns:

  • (Array)

    Returns the current page without modifying the current position.



101
102
103
# File 'lib/seq/pager.rb', line 101

def curr
  @pages[@curr]
end

#firstArray

Moves to the first page, and returns it.

Returns:

  • (Array)


73
74
75
76
# File 'lib/seq/pager.rb', line 73

def first
  @curr = 0
  curr
end

#first!Seq::Pager

Moves to the first page.

Returns:



65
66
67
68
# File 'lib/seq/pager.rb', line 65

def first!
  @curr = 0
  self
end

#first?Boolean

Returns Whether currently at the first page.

Returns:

  • (Boolean)

    Whether currently at the first page.



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

def first?
  page == 0
end

#lastArray

Moves to the last page, and returns it.

Returns:

  • (Array)


94
95
96
97
# File 'lib/seq/pager.rb', line 94

def last
  @curr = pages - 1
  curr
end

#last!Seq::Pager

Moves to the last page.

Returns:



86
87
88
89
# File 'lib/seq/pager.rb', line 86

def last!
  @curr = 0
  self
end

#last?Boolean

Returns Whether currently at the last page.

Returns:

  • (Boolean)

    Whether currently at the last page.



79
80
81
# File 'lib/seq/pager.rb', line 79

def last?
  page == pages - 1
end

#nextArray?

Moves to, and returns the next page. If at the last page it returns nil.

Returns:

  • (Array, nil)


117
118
119
120
121
122
# File 'lib/seq/pager.rb', line 117

def next
  return if last?

  @curr += 1
  curr
end

#next!Seq::Pager

Moves the next page, and returns the Seq::Pager object itself. If at the last page it simply returns self.

Returns:



109
110
111
112
# File 'lib/seq/pager.rb', line 109

def next!
  @curr += 1 unless last?
  self
end

#pageInteger

Returns The index of the current page.

Returns:

  • (Integer)

    The index of the current page.



48
49
50
# File 'lib/seq/pager.rb', line 48

def page
  @curr
end

#page=(num) ⇒ Object

Sets the current page. If the number passed is out of range, it will be set to within the bound of available pages.

Parameters:

  • num (#to_i)


35
36
37
38
39
40
41
42
43
44
45
# File 'lib/seq/pager.rb', line 35

def page=(num)
  unless num.respond_to?(:to_i)
    raise ArgumentError.new("cannot call #to_i on argument")
  end

  num = num.to_i
  num = 0           if num < 0
  num = pages - 1   if num >= pages

  @curr = num
end

#pagesInteger

Returns The total number of pages.

Returns:

  • (Integer)

    The total number of pages.



53
54
55
# File 'lib/seq/pager.rb', line 53

def pages
  @pages.size
end

#prevArray?

Moves to, and returns, the previous page. If at the first page it returns nil.

Returns:

  • (Array, nil)


137
138
139
140
141
142
# File 'lib/seq/pager.rb', line 137

def prev
  return if first?

  @curr -= 1
  curr
end

#prev!Seq::Pager

Moves to the previous page, and returns the Seq::Pager object itself. If at the first page it simply returns self.

Returns:



128
129
130
131
# File 'lib/seq/pager.rb', line 128

def prev!
  @curr -= 1 unless first?
  self
end

#range(left, middle, right) ⇒ Object

Provides a list of page numbers, useful for populating a page selector. It returns an array containing three arrays, some of which may be empty.

Examples:


s = Seq::Pager.new(10, 1..1000)

s.range(2, 2, 2)
#=> [[0, 1, 2, 3, 4, 5, 6, 7], [], [98, 99]]

s.page = 10
s.range(2, 2, 2)
#=> [[0, 1, 2], [48, 49, 50, 51, 52], [98, 99]]

Parameters:

  • left (Integer)

    Maximum number of elements for the left-most array

  • middle (Integer)

    Maximum number of elements for either side of the current page

  • right (Integer)

    Maximum number of elemengts for the right-most array



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/seq/pager.rb', line 163

def range(left, middle, right)
  width = 2 * middle + 1

  if pages < left + width + right
    return [(0..pages-1).to_a, [], []]
  end

  if page < left + middle + 1
    left   = (0..left+width-1).to_a
    middle = []
    right  = (pages-right..pages-1).to_a

  elsif page > (pages - 1) - right - middle - 1
    left   = (0..left-1).to_a
    middle = []
    right  = (pages-right-width..pages-1).to_a

  else
    left   = (0..left-1).to_a
    middle = (page-middle..page+middle).to_a
    right  = (pages-right..pages-1).to_a
  end

  [left, middle, right]
end